Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: Is serialization of a generic interfaces possible?

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.

like image 866
bwerks Avatar asked Jul 26 '10 17:07

bwerks


2 Answers

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.

like image 129
Thorarin Avatar answered Sep 19 '22 12:09

Thorarin


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.

like image 21
marc_s Avatar answered Sep 22 '22 12:09

marc_s