客户端中的WCF操作需要与服务器中定义的参数集不同的参数集

时间:2017-05-02 06:06:03

标签: wcf messagecontract

我是WCF的新手。我有一个示例WCF服务器和一个使用该服务的客户端。 我有一个名为getEmployer4的OperationContract接受一个EmployerRequestBO并返回一个EmployerResponseBO,这两个类型都被装饰为MessageContract

    public EmployerResponseBO getEmployer4(EmployerRequestBO rqst)
    {
        return new EmployerResponseBO
        { CompanyName = "Apple", CompanyAddress = "US" };
    }

我的EmployerRequestBO看起来像:

[MessageContract(IsWrapped = true, WrapperName = "EmployerRequest", WrapperNamespace ="http://mycompany.com/services")]
public class EmployerRequestBO
{
    [MessageHeader(Namespace = "http://mycompany.com/services")]
    public string LicenseKey
    {
        get; set;
    }

    private int _regID;
    [MessageBodyMember(Order = 1, Name = "CompanyRegistrationID", Namespace = "http://mycompany.com/services")]
    public int RegistrationID
    {
        get
        {
            return _regID;
        }
        set
        {
            _regID = value;
        }
    }

问题是,当我试图用以下代码在客户端中调用operaiton时:

        ServiceReference_EmployerService.EmployerClient client = new ServiceReference_EmployerService.EmployerClient("BasicHttpBinding_IEmployer");
        ServiceReference_EmployerService.EmployerRequestBO request = new ServiceReference_EmployerService.EmployerRequestBO("ABC123", 123);

        ServiceReference_EmployerService.EmployerResponseBO response= client.getEmployer4(request);

getEmployer4不期望EmployerRequestBO参数,Error如下所示

Click to see attachment

  

没有任何论据符合所要求的形式   参数'CompanyRegistrationID'的   'EmployerClient.GetEmployer4(string,ref int,out string)'。

任何人都可以解释为什么要求原始类型而不是MessageContract类型?谢谢!

1 个答案:

答案 0 :(得分:1)

我花了很多时间才知道,如果您的操作通过 MessageContract 进行通信,则需要创建代理,如:

ServiceReference_EmployerService.**IEmployer** client =
new ServiceReference_EmployerService.EmployerClient("BasicHttpBinding_IEmployer");

如果您通过 DataContract 进行操作,则需要创建代理,如:

ServiceReference_EmployerService.**EmployerClient** client2 = 
new ServiceReference_EmployerService.EmployerClient("BasicHttpBinding_IEmployer");