简体   繁体   English

如何通过WCF获取类的重载构造函数?

[英]How to get overloaded constructors of a class through WCF?

How can I get the overloaded constructors on my classes passed on to the WCF client/consumer? 如何将我的类上的重载构造函数传递给WCF客户端/使用者?

Basically, WCF thinks there is only the default, no argument/empty constructor for my B class. 基本上,WCF认为我的B类只有默认的,没有参数/空构造函数。 How can I enable the client to call the overloaded constructors? 如何让客户端调用重载的构造函数?

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
    }
}

A constructor is a class only construct that a wcf client is not aware of. 构造函数是wcf客户端不知道的仅类构造。 The boundaries are different. 边界是不同的。

The client just knows about a proxy class. 客户端只知道代理类。 And the WCF infrastructure creates the proxy class with the default constructors. WCF基础结构使用默认构造函数创建代理类。 Independent of the constructors at the server side, because they don't mean anything from the WCF perspective. 独立于服务器端的构造函数,因为它们并不意味着从WCF的角度来看。

Only Service Contracts, Operation COntracts and Data Contracts matter. 只有服务合同,操作合同和数据合同才有意义。

if you want additional functionality in your proxy class, you can always add a partial class with the same name on your client side and add code to it. 如果您希望在代理类中使用其他功能,则可以始终在客户端添加具有相同名称的部分类,并向其添加代码。 (overloaded constructors etc.) be aware that the server doesn't really care about this. (重载的构造函数等)请注意服务器并不真正关心这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM