Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...