Reputation: 9871
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
Upvotes: 7
Views: 5042
Reputation: 6361
[I just read your answer and realized that you were asking specifically about parameters to service methods. I'll leave my comments here in case they're still helpful.]
What I've done on projects where I know I have WCF on both sides of the wire, is something like:
A library of only the shared interfaces, eg:
namespace SharedInterfaces {
public interface ICompositeType {
bool BoolValue { get; set; }
string StringValue { get; set; }
}
}
The WCF service library, where the [DataContract]s (POCOs) implement the shared 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 proxy POCO can be "compelled" to implement the shared, deployed, interface using a partial class (it will anyway if svcutil did it's job correctly), and you'll be able to program to 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 make use of (eg. Presenter or ViewModel concepts in MVP or MVVM patterns).
Upvotes: 0
Reputation: 9871
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)
Upvotes: 7
Reputation: 1062492
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).
Upvotes: 1