WCF: Is serialization of a generic interfaces possible?
Asked Answered
T

4

6

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.

Thalassa answered 26/7, 2010 at 17:22 Comment(0)
C
12

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.

Carreon answered 26/7, 2010 at 19:19 Comment(1)
Yeah, I edited in the platform I'm using when I thought about covariance in C# 4.0. Oh, to upgrade.Thalassa
C
2

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.

Crafton answered 26/7, 2010 at 17:25 Comment(5)
WCF does understand generics enough to serialize them (I've done it). Your point on Interfaces is correct.Domino
@Domino do you have a place to show that? A blog, a CodeProject article or something? Love to see that!Crafton
@RQDQ: Actually, it's not entirely correct, imo. While the interface itself cannot be serialized, the concrete type used can be. The 'tricky' part is deserialization, because it needs to known which concrete type to instantiate. However, enough information is included in the serialized data to do exactly that.Carreon
I just posted some code that serializes a generic glass over the wire.Domino
@Carreon - you are correct. I just set up a repro (granted in framework 4.0) that serializes an interface correctly both ways. Granted, the VS generated client exposes the operations as type Object instead of an interface or the base type.Domino
C
1

You cannot serialize an interface. An interface just defines the contract, not the object. I guess that the one exception to this is the ISerializable interface.

Contexture answered 26/7, 2010 at 18:22 Comment(2)
When you're "serializing ISerializable", you aren't, really. ISerializable is an interface used to serialize something else.Ghiberti
@John. You are correct. You still not really serializing the interface, just agreeing on a contract used to serialize the objects that implement ISerializable. I just added this for completeness.Contexture
D
1

Generics can be serialized, but with certain limitations. For example, given the data contract:

[DataContract]
public class Foo<T>
{
     [DataMember]
     public T Value { get; set; }
}

And the service contract:

[ServiceContract]
public interface IService1
{
     [OperationContract]
     Foo<String> GetData();
}

And the service implementation:

public class Service1 : IService1
{
   public Foo<string> GetData()
   {
       return new Foo<string>() { Value = "My test string" };
   }
}

After setting a Service Reference to the above service, this code can be run:

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

ServiceReference1.FooOfstring temp = client.GetData();

MessageBox.Show(temp.Value);

And the message box with "My test string" is displayed.

Note that the service itself is not generic, but the data contract used is. Further, the data contract generated on the client side is not generic, but rather a "flattened" class that has a property value of type string:

[System.Runtime.Serialization.DataMemberAttribute()]
public string Value 
{ 
   get {...} 
   set {...} 
}
Domino answered 27/7, 2010 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.