wcfwcftestclient

WcfTestClient method take base class as param


I have the following method signature being exposed via WCF:

public void MethodA(RequestBase request)
{

}

public class RequestA : RequestBase
{

}

There are some concrete classes derived from the RequestBase Class. During the service call using WcfTestClient.exe, how do i pass the actual concrete class (RequestA) to the RequestBase in methodA ?


Solution

  • You're looking for the KnownType attribute for your data contracts:

    [DataContract]
    public class RequestBase
    {
    }
    
    [DataContract]
    [KnownType(typeof(RequestBase))]
    public class RequestA : RequestBase
    {
    }
    

    Then you can pass in a RequestA object where RequestBase is the expected type of the service operation:

    var requestA = new RequestA();
    serviceClient.MethodA(requestA);