0
votes

I am using an interface as input parameter in OperationContract. But when I generate the proxy class at client side, the client method is seen as: GetDat(object value) instead of GetData(IMyObj value)

  1. Service Interface
[ServiceContract]
[ServiceKnownType(typeof(MyObj))]
public interface IService
{
    [OperationContract]
    string GetData(IMyObj value);
}
  1. Service class
public class Service : IService
{
    public string GetData(IMyObj value)
    {
        return string.Format("You entered: {0}", value.MyValue);
    }

}
  1. Interface
public interface IMyObj
{
    int MyValue { get; set; }
}
  1. Data Contract
[DataContract]
[KnownType(typeof(IMyObj))]
public class MyObj : IMyObj
{
    [DataMember]
    public int MyValue { get; set; }
}

Note: There are a lot of similar questions on stackoverflow regarding to interface parameters and wcf. But they all tell to use the ServiceKnownType attribute and KnownTypeAttribute (Which I did). But it still gives the calling method on the client side an object as parameter type instead of my interface type.

1

1 Answers

0
votes

For other people who are running into the same problem. I've found this answer from Ladislav on https://social.msdn.microsoft.com/Forums/vstudio/en-US/2c52251b-af7f-4529-a2ac-14418ca4b19d/wcf-service-reference-does-not-add-the-interface-definition-of-a-datacontract-of-a-class?forum=wcf

Hello,

you can't do that. DataContract represents definition of data transfered between client and service. This definition is transformed into XSD which describes format of exchanged XML (serialized data contract object). XML can transfer only data not logic and it doesn't transfer any inormation about data contract implementation = no inheritance and no interface implementation. If you want to use datacontract with interface on the client you have to share that data contract in assembly (not only the interface) and reuse it on the client when service proxy is generated.

Best regards, Ladislav