0

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!

tom redfern
  • 30,562
  • 14
  • 91
  • 126
YJ.Li
  • 11
  • 4
  • Is the service deployed on your server using the same contract as your client code? – lockstock May 02 '17 at 06:16
  • also, try adding a parameter-less constructor to EmployerRequestBO – lockstock May 02 '17 at 06:26
  • Hi @lockstock I am running the service in visual studio built-in IIS in my local pc, the client is running on same pc with another instance of vs. They should use same contract. Adding a parameter-less constructor doesnot seem to resolve the issue – YJ.Li May 02 '17 at 09:24
  • Why are you using message contracts? This is the reason your operation signature is different to what you expect it to be. – tom redfern May 02 '17 at 10:11

1 Answers1

1

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");
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
YJ.Li
  • 11
  • 4