Serialize generic type through WCF service

I have a C # class defined as follows:

public class GenericItem<T> { public List<T> Items { get; set; } public DateTime TimeStamp { get; set; } } 

I am instantiating this class on my server. Then I try to pass it through the wire through the WCF service, as shown here:

 [OperationContract] public GenericItem<MyCustomType> GetResult() { GenericItem<MyCustomType> result = BuildGenericItem(); return result; } 

At this point, everything compiles just fine. When I "update the service link" in my Silverlight application, recompile, I get a compile-time error similar to the following:

MyNamespace.GenericItemOfMyCustomType [optional characters] does not contain a public definition for 'GetEnumerator'

I have no idea why:

  • Additional characters appear. It seems to change every time I update the service link.
  • How to fix it.

What am I doing wrong?

+8
c # wcf
source share
4 answers

Slimane is right, but you can use Bounded Generics, as described in this article , and you can achieve what you want. This allows you to create a common type within the service and expose it. But the consumer will not consider it as general, since the type is specified in the service operation.

+7
source share

You cannot define WCF contracts that rely on type parameters. Generics are specific to .NET, and their use will disrupt the service-oriented nature of WCF. However, a data contract may include a collection as a data element, since WCF offers special marshaling rules for collections.

+5
source share

As sleiman noted, Generics are not supported in SOAP.

WCF and generics β†’ http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/79585667-6b97-4ce4-93fa-3a4dcc7a9b86

related question β†’ WCF. General Service Methods

+1
source share

You can use the following on the client side instead of using servicereference:

 var myBinding = new BasicHttpBinding(); var myEndpoint = new EndpointAddress(""); var myChannelFactory = new ChannelFactory<IService>(myBinding, myEndpoint); IService gks = myChannelFactory.CreateChannel(); 

Works for generics.

-3
source share

All Articles