Wcf and interfaces as parameters

I have a library with some objects that use the same interface. clients and services share this assembly. now I am wondering if there is a way to have this interface type as a parameter in my service contracts so that I can use the same method for all classes implementing the interface.

themselve entities are all decorated with the datacontract attribute and its members with the datamember attributes.

Is it possible at all? perhaps using a NetDataContractSerializer? I know that I can do this with a base class (for example, with an abstract class) and with a known attribute type, but I definitely prefer the interface as an identifier for objects, because it is widely used in the client application and makes development easier.

thanks

+6
c # web-services wcf
source share
3 answers

I solved the problem using the ServiceKnownType attribute in the OperationContracts implementations.

When you tell your classes that implement the interface as ServiceKnownType, you can use the interface as a parameter, and therefore, you can use all the classes that implement your interface as long as they are serializable. (see "Programming WCF Services" by Juval LΓΆwy, p. 100)

+6
source share

Of course, this is not possible with regular mex. It may be possible with sharing, but I would not recommend it - you are struggling with WCF: it will be fragile, etc. Of course, you can always mask it in your object model, that is, instead of calling the [OperationContract] method, abstractly it is a wrapper method that hides WCF details (possibly using different objects to transfer data than it actually returns).

+1
source share

[I just read your answer and realized that you are specifically setting parameters for the service methods. I will leave my comments here if they are still useful.]

What I did in projects where I know that I have WCF on both sides of the wire, there is something like:

The library is only for common interfaces, for example:

namespace SharedInterfaces { public interface ICompositeType { bool BoolValue { get; set; } string StringValue { get; set; } } } 

WCF Services Library, where [DataContract] s (POCOs) implement common interfaces.

 [DataContract] public class CompositeType : ICompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } 

In the service client, each POCO proxy server may be β€œforced” to implement a common, deployed interface using a partial class (in any case, if svcutil did this correctly), and you can program the interface in the rest of your client code:

 namespace ServiceClient.ServiceReference1 { public partial class CompositeType : ICompositeType { } } 

This partial is also useful if you want to add some additional properties or methods that the client can use (for example, Presenter or ViewModel concepts in MVP or MVVM templates).

0
source share

All Articles