I'm trying to implement a service contract that contains a method which takes a generic interface, and that generic interface itself is given an interface parameter. I've decorated the service interface with ServiceKnownType, I have decorated the service implementation with regular KnownType, and I have decorated the datacontract implementation with regular KnownType:
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbacks))]
[ServiceKnownType(typeof(Batch<object>))]
[ServiceKnownType(typeof(Command))]
public interface IActions
{
[OperationContract]
IResponse TakeAction(IBatch<ICommand> commands);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
[KnownType(typeof(Batch<object>))]
[KnownType(typeof(Command))]
internal class Actions : IActions
{
}
[DataContract]
[KnownType(typeof(Command))]
public class Batch<T> : IBatch<T>
{
}
For the record, I have Batch there because it seems that you can only express a knowntype for a generic type once--it appears to emit BatchOfanyType, but I'm not sure how to handle this.
The exception I'm getting is "Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."
Is there anything obvious I'm doing wrong? Are generic interfaces of interfaces just not supported? For the record I'm on C# 2.0 and .NET 3.0 for this project.
You can use interfaces in service contract definitions if you really want to, as long as you're including the known types as you are doing (with a slight adjustment, see below).
Apparently, using an interface as the generic type parameter is taking it a bridge too far for C# 3.0. I changed the known type attribute to
[ServiceKnownType(typeof(Batch<Command>))]
public interface IActions
{
}
Which makes it work, to a point. Serialization and deserialization itself will work, but then you're faced with this exception:
Unable to cast object of type 'Batch`1[Command]' to type 'IBatch`1[ICommand]'.
For that cast to work, you need language support for generic type covariance, something that's introduced in C# 4.0. For it to work in C# 4.0 though, you'd need to add a variance modifier:
public interface IBatch<out T>
{
}
Then it works perfectly... unfortunately you're not using C# 4.0.
One last thing about using interfaces in your service contract: if you're generating a service reference from them, it will type all the interface parameters as object
, becase the original interface type isn't part of the metadata. You can share contracts through an assembly reference, or manually refactor the generated proxy to fix it, but all in all, using interfaces with WCF is probably more trouble than it's worth.
WCF is a SOA message-based system - it can send anything across the wire in the serialized XML format that can be expressed in XML schema.
Unfortunately, XML schema doesn't know anything either neither interfaces nor generics, so no - you cannot generically serialize those - you need to use concrete types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With