Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wcf and Interfaces as Parameters

i have a library with some entities that share the same interface. clients and service share this assembly. now i wonder if there is a way to have this Interface-type as Parameter in my service contracts so that i can use the same method for all classes implementing the interface.

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

is it possible at all? probably with the NetDataContractSerializer? i know that i can do it with a base class (some abstract class e.g.) and the knowntype-attribute but i´d definitely prefer the Interface as identificator of the objects cause it is used widely in the client app and would ease development.

thanks

like image 538
Joachim Kerschbaumer Avatar asked Sep 30 '08 16:09

Joachim Kerschbaumer


2 Answers

I solved the problem using the ServiceKnownType attribute at the implementations of the OperationContracts.

When telling your classes that implement the interface as ServiceKnownType's, you can use the interface as parameter and therefore are able to use all classes implementing your interface as long as they are serializable. (look at "Programming WCF Services" from Juval Löwy, page 100)

like image 124
Joachim Kerschbaumer Avatar answered Oct 26 '22 10:10

Joachim Kerschbaumer


It certainly isn't possible under regular "mex". It might be possible with assembly sharing, but I really wouldn't recommend it - you are fighting WCF: it will be brittle, etc. Of course, you can always mask this in your object model - i.e. rather than calling the [OperationContract] method directly, abstract this away into a wrapper method that hides the WCF details (perhaps using different objects for the data transfer than it actually returns).

like image 21
Marc Gravell Avatar answered Oct 26 '22 09:10

Marc Gravell