【问题标题】:WCF service not generating classes for run time polymorphismWCF 服务不为运行时多态性生成类
【发布时间】:2013-09-07 17:46:27
【问题描述】:

我用几个继承的类 A1、A2、A3 等创建了我的基类 A。

现在,使用 WCF 服务,我尝试返回包含 A1、A2 甚至 A 类型的对象的 List。

WCF 支持这个吗?我不断收到连接关闭错误。

例如。

class A{
//do something
}

class B:A{
//do something
}

class C:A{
//do something
}

WCF Service calling GetAll method which returns

public List<A> GetAll()
{
var obj= new List<A>();
obj.Add(new B());
obj.Add(new C());
return obj;
}

现在 WCF 服务只知道 A 而不是 B 和 C。我怎么还能返回这个对象。哎呀,这是有效的,但我不知道服务

【问题讨论】:

    标签: c# .net wcf oop


    【解决方案1】:

    我可以通过向服务公开的类添加 KnownType 属性来解决此问题。

    http://msdn.microsoft.com/en-us/library/ms730167.aspx

    KnownType 在运行时解析序列化

    [KnownType(typeof(B))]
    [KnownType(typeof(C))]
    class A{
    //do something
    }
    
    class B:A{
    //do something
    }
    
    class C:A{
    //do something
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多