WCF and interfaces on data contracts
Asked Answered
C

2

13

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?

Crapulous answered 18/1, 2011 at 5:19 Comment(0)
D
21

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.

Dorser answered 18/1, 2011 at 6:2 Comment(2)
Thanks marc_s I will try this approach.Crapulous
Thanks for the explanation marc_s, I assume ganeshran is considering a similar case to myself. The goal is not to pass an instance of foo that implements ISometype to the server via service methods, what I'd like to do is avoid concrete dependencies in the consumer of the service; without having to replicate all of the objects in another layer and have endless mapping (automapper or otherwise), the service I am consuming has hundreds of types and thousands of attributes.Euglena
P
2

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?

Proficient answered 18/1, 2011 at 5:48 Comment(1)
Thanks Artem, but my interest is in having the generated types also implement the interface ISomeType. I dont think KnownTypes will provide that functionality.Crapulous

© 2022 - 2024 — McMap. All rights reserved.