tags:

views:

57

answers:

2

I have two applications that are communicating through WCF.

On the server the following object exists:

public class MyObject<T>
{
    ...
    public Entry<T> GetValue()
}

Where Entry<T> is another object with T Data as a public property. T could be any number of types (string, double, etc)

On the client I have ClientObject<T> that needs to get the value of Data from the server (same type).

Since I'm using WCF, I have to define my ServiceContract as an interface, and I can't have ClientObject<T> call Entry<T> GetMyObjectValue (string Name) which calls GetValue on the correct MyObject<T> because my interface isn't aware of the type information.

I've tried implementing separate GetValue functions (GetMyObjectValueDouble, GetMyObjectValueString) in the interface and then have ClientObject determine the correct one to call. However, Entry<T> val = (Entry<T>)GetMyObjectValueDouble(...); doesn't work because it's not sure about the type information.

How can I go about getting a generic object over WCF with the correct type information? Let me know if there are other details I can provide.

Thanks!


I used a combination of methods to get this to work. I implemented several Entry<double> GetMyObjectValueDouble(...), Entry<string> GetMyObjectValueString(...) methods on the server. On the client I check the type of the object and then call the appropriate function:

Entry<T> Data = (Entry<T>)Convert.ChangeType(Client.GetMyObjectValueDouble(...),typeof(Entry<T>));

Hope that helps somebody

+2  A: 

SOAP Web Services are based on WSDL and XML Schema. The WSDL describes the services and operations, and XML Schema defines the data that the operations work with.

XML Schema cannot describe generics. Therefore, there are no generics in web services.

John Saunders
+1 yes, lots of folks still don't realize that SOA/SOAP is **not** the same as OOP - SOAP and SOA does have its limitations....
marc_s
+1  A: 

You can not user generics in WCF. The service must know the type of object during transmission. But is not a specific type. What you can do is you can define a custom class that contains the definition of all other entities in your app. And you can set a specific entity what ever you need and then transmit that class. By this way you can transfer multiple objects as well.

Johnny
I'm giving you the credit because you gave a suggestion that put me in the right direction and didn't just tell me I couldn't do it, which I already knew. Thanks!
Aaron
Welcome. any time..
Johnny