tags:

views:

213

answers:

3

Hello, I have the following problem: I wrote this code:

[OperationContract]
void AddItem<T>(T item);

But i recieve the following Error:

Type 'T' cannot be exported as a schema type because it is an open generic type. You can only export a generic type if all its generic parameter types are actual types.

How can I use generic methods in wcf service ?

+2  A: 

You simply can't. It's not possibly to do that, as soap does not support this. See this article, which mentions how to get around it by creating an intermediate local object that is called and that casts the object before calling the wcf operation.

Simon
+1  A: 

You shouldn't be trying to do this. In a SOAP enabled web service all types need to be known when the WSDL is published so that clients would be capable of generating a proxy. Generics simply don't exist in the SOAP specification. SOAP is intended to be interoperable and generics don't exist in all languages.

Darin Dimitrov
A: 

As all the others have already mentioned, WCF and SOAP do not support this. The issue is: anything you pass back and forth between client and server must be expressible in a XML schema document.

XML schema supports all the usual atomic types, like string, int, datetime - and it supports complex types made up of those atomic types, and it supports inheritance.

But XML schema has no support for generics - and thus, anything you exchange via WCF and SOAP cannot be generic - you need to use concrete, non-generic types only.

I don't know of any way around this, either. It's a limitation and you have to live with it for now.

marc_s