Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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.

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 {...} 
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top