views:

619

answers:

3

I have a Windows forms project and a Web Service project in my solution, and I'm trying to call the web service and return a customer object as the result. The problem is that when I try to receive the return object, I get an error that it can't convert it. For example, here is the signature for my webservice:

Public Function GetDriverByID(ByVal DriverID As Integer) As Driver

And here is the code I'm using to call it:

    Dim d As Driver = mywebserviceinstance.GetDriverByID(1)

But I receive this compile-time error (wsDrivers is the name of the web reference I've added to my form project): "Value of type ProjectNamespace.Common.wsDrivers.Driver cannot be converted to ProjectNamespace.Common.Driver"

This "Common" namespace contains the Driver class, and I'm not sure why the return class from the web service isn't just a generic "Driver", but is instead a "wsDrivers.Driver", and I can't convert it back. Anybody know how I can deal with this type mismatch?

EDIT: Thanks for the explanations - this actually makes it clear what it's doing. However, is there any way that I can force it to use the actual type instead of the proxy (or, rather, is there any way to convert between the "real" instance and the "proxy" instance), or do I have to serialize the properties before I send them over the wire, and then manually de-serialize the return values?

A: 

The web service reference in a VB.NET or C# project can reference any type of web service and is not limited to those provided by ASP.NET. That is why Visual Studio creates proxy classes for each object which can be retrieved from the web service.

hangy
In some test I did with web services, I used a class which copied every public property of one class to the other, but this gets complex with deeper structures … Knowing a way to use actual .NET classes instead of the web service reference generated ones would be interesting for me, too!
hangy
+2  A: 

This is actually pretty common. What's happening is that the Web Service has defined in it the definitions of all the types used in the web service. When you add a reference to that web service, it auto-generates a proxy type in a sub namespace of your namespace. That is what is being returned by your web service when you call it.

However, you probably are also referencing the same library that the web service does seperately that contains the same type. That is the type that is expected when you Dim Driver. That's why there is a mismatch.

Nick
Is there any way around this? Since I'm referencing the same DLL in both projects (the webservice and the project with the web reference), can I force the "proxy class" to be a copy of its actual class? Or is there an easy way to map them between each other?
rwmnau