wcfmessagecontract

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


I am new to WCF. I have a sample WCF server and a client consuming the service. I have a OperationContract called getEmployer4 which accepts a EmployerRequestBO and returns a EmployerResponseBO, both these 2 types are decorated as MessageContract

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

my EmployerRequestBO looks like:

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

Problem is, when i tried to call the operaiton in client with below code:

        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);

The getEmployer4 doesnot expect an EmployerRequestBO argument, Error looks like below

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)'.

Can anyone explain why it is asking for primitive types instead of a MessageContract type? Thanks!


Solution

  • It took quite a bit of time before I learned that, if your Operation communicate through MessageContract, you need to create the proxy like:

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

    whereas if you Operation communicate through DataContract, you need to create the proxy like:

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