Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF and interfaces on data contracts

Tags:

c#

.net

wcf

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?

like image 644
ganeshran Avatar asked Jan 18 '11 05:01

ganeshran


People also ask

What is WCF data contract?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

What type of contract is applicable to interface in WCF?

The Service Contract declares an interface in the WCF service for the client to get access to the interface. The Operation Contract declares a function inside the interface, the client will call this function.

What can be used to define a WCF service contract?

A service contract can be declared using the [ServiceContract] attribute. It allows defining an Operation Contract under it to expose the service outside the world. It maps the interface and methods of your service to a platform-independent description.

Which type of contract is applicable to interface in?

There are three types of contract clauses associated with interfaces: preconditions, postconditions, and class invariants.


1 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.

like image 95
marc_s Avatar answered Sep 19 '22 17:09

marc_s