WCF and interfaces on data contracts

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.

Leave a Comment