0

如何将我的类上的重载构造函数传递给 WCF 客户端/消费者?

基本上,WCF 认为我的 B 类只有默认的、没有参数/空的构造函数。如何使客户端能够调用重载的构造函数?

public class A
{
    public string MyField { get; set; }

}

public class B : A
{
    public List<C> MyList { get; set; }

    // when called on the WCF client side, MyList is null (so this constructor is not being called)
    public B()
    {
        MyList = new List<C>();
    }

    // not available on WCF client side
    public B(A a) : this()
    {
        base.MyField = a.MyField;
    }

    // not available on WCF client side
    public void DoSomething()
    {
        // do stuff
    }
}
4

1 回答 1

2

构造函数是 wcf 客户端不知道的仅类构造。界线不同。

客户端只知道代理类。WCF 基础结构使用默认构造函数创建代理类。独立于服务器端的构造函数,因为从 WCF 的角度来看,它们没有任何意义。

只有服务合同、运营合同和数据合同很重要。

如果你想在你的代理类中添加额外的功能,你总是可以在你的客户端添加一个同名的部分类并向它添加代码。(重载的构造函数等)请注意服务器并不真正关心这一点。

于 2014-04-01T00:32:19.133 回答