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

WCF operation in Client expects a different sets of argument than what is defined in server

我是 WCF 新手。我有一个示例 WCF 服务器和一个使用该服务的客户端。 我有一个名为 getEmployer4 的 OperationContract,它接受一个 EmployerRequestBO 和 returns 一个 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;
        }
    }

问题是,当我尝试使用以下代码调用客户端中的操作时:

        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 参数,错误如下所示

Click to see attachment

There is no argument given that corresponds to the required formal parameter 'CompanyRegistrationID' of 'EmployerClient.GetEmployer4(string, ref int, out string)'.

谁能解释为什么它要求原始类型而不是 MessageContract 类型?谢谢!

我花了很多时间才了解到,如果您的操作通过 MessageContract 进行通信,您需要像这样创建代理:

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

而如果您的 Operation 通过 DataContract 进行通信,则需要像这样创建代理:

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