WCF and interfaces on data contracts

While creating the WCF proxy using svcutil, is it possible to include the interfaces as well from which the data contracts inherit, e.g.:

public class SomeType: ISometype
{
   public string Name { get; set; }
}

public interface ISometype
{
   public string Name { get; set; }
}

When I create the proxy using this, the SomeType type is created at the client but the interface isn't created and there is no inheritance either. I tried marking the interface as DataContract but that attribute isnt allowed.

Is it possible to do what I am trying to do?

Answers


WCF uses serialized messaging, and all those messages need to be able to be serialized using a DataContractSerializer or an XmlSerializer. And those messages going between the client and the server needs to be expressible in XML schema.

Now, XML schema doesn't know anything about interfaces - it's all about concrete, actual types. For a regular scenario where your clients can be anything from .NET to PHP to Ruby to (whatever), you need to make sure to express everything you want to send between client and server in a way that can be represented in XML schema - interfaces cannot. So there's really no way to support this in a general purpose scenario.

If you're controlling both ends of the wire, e.g. you write both the client and the server, and both in .NET, then you can do this:

  • put your DataContracts (and your ServiceContracts and OperationContracts and FaultContracts) all into a separate MyServiceContracts assembly

  • reference that assembly from both your service-side code, as well as the client. In that case, when you go about to create the client proxy, those types you mention are already present and WCF will happily reuse those types from that assembly. And since that's a .NET assembly you're referencing, you can have anything in there that .NET supports - including interfaces.


You can inherit from the class implementing ISometype and add it to serialization using KnownType attribute:

Can I force svcutil.exe to generate data contracts for a WCF service?


Need Your Help

Need help understanding E8 asm call instruction x86

assembly x86 disassembly

I need a helping hand in order to understand the following assembly instruction. It seems to me that I am calling a address at someUnknownValue += 20994A?