开发者

Generating WCF Proxy to Server Interface

I'm generating a WCF Proxy to my contract. The contract contain classes that are implementing an Interface. on the Generated proxy the interface is gone. so I can't use it.

Is there a way to mark the interface in some way to be 开发者_开发百科generated on the proxy file ?

Here is a contact example:

public interface IMyGeneralInterface
{    
   int? ID { get; set; }
}

[DataContract]
public abstract class EntityBase : IMyGeneralInterface
{        
  [DataMember]
  public virtual int? ID { get; set; }
}

The Generated proxy will not contain any definition, of IMyGeneralInterface which will not enable me to use it one the client side.

What should I do ?

Thanks, Dani


The messages being exchanged between the client and the server must be concrete classes that can be serialized and deserialized between sender and receiver. This makes it virtually impossible to send back and forth interfaces. The important thing is: you're exchanging messages between client and server - NOT objects with information and behaviors.

All the client sees when he imports the service description is a data contract of type "EntityBase" - he doesn't see anything else. The data is described in XML schema, and XML schema does not have a concept of interfaces.

Remember: WCF is interoperable - your client could be a PHP or Java client, and they might not support the notion of interfaces.

The only thing you could do if you control both ends of the service (both server and client) is to put your service and data contracts into their own assembly and then share that assembly between the client and the server. That way, you can "transport" more information about your service and your data contracts between your client and server, BUT you loose any chance of interoperability in the process.

See these blog posts for more info on sharing assemblies between WCF client and server:

  • Share your interfaces - avoid static proxies
  • Sharing WCF collection types between Service and Client
  • WCF Primer 7 - creating client
  • WCF and sharing data contracts

Marc


An alternative if you really need to implement an interface on your generated proxy types is by partial classing the generated type. I believe the standard generated proxy classes are normally marked as partial and public.

public partial class GeneratedType : IMyGeneralInterface {}

Naturally you'd still end up having to share your interface with both the client and server side assemblies so this wouldn't solve your interoperability issue as voiced in the previous answer and wont be generated for free, but sometimes it can be useful.

I always prefer to keep shared interfaces in a separate assembly to that of any server side objects to avoid sharing types from service to client (I find this confusing otherwise).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜