views:

554

answers:

2

I have the following class in Class Library: Artist, which is a POCO

Now I have a method in a web-service (which has a reference to the mentioned-above library) with a signature like this:

[WebMethod]
public int Artist_AddArtist(Artist a) {
 //
}

When I try to consume this service from an application (that also has a reference to the mentioned-above Class library), the expected parameter of the Artist_AddArtist method is not Artist, but a new type of Artist that is being generated in Reference.cs which is a partial class that is auto-generated.

Thus since in my application I use, supposedly the same Artist class from the library and now the Web Service method expects this new auto generated type, I cannot pass an instance of it to the web-service.

How can I fix this issue?

+2  A: 

You cannot, and should not, fix the problem.

Some others will tell you to do things like edit the generated file, but that's not a good practice (as the changes will go away as soon as the Web Reference is updated).

What you're seeing is by design. See Basics: How Web Services Work.

Briefly, when you use "Add Web Reference", Visual Studio downloads the WSDL file from the service, and uses the XML Schemas from the WSDL to create some proxy classes to represent the XML described by the schema. It also creates a proxy class for the service itself, having methods for each operation in the service.

The proxy data classes can serialize to the XML that the service is expecting to receive, and can be deserialized back from the XML that the server sends in reply.

One way to think of it is that you only have this problem because both client and service are .NET. If your client were written in Java, then you wouldn't be thinking of sharing classes.


Note that WCF can do this, if necessary. It introduces a dependency between the client and service (they both have to use compatible versions of the assembly containing the classes), but when you need to do it, the option is there. It's useful when there is behavior in these classes that must be used both by the client and by the service.

John Saunders
So then, what is the best way to change an instance of an 'original' class to an instance of the 'auto-generated' class? reflecting through the property values of the original and adding each one to the expected type instance?
Andreas Grech
You don't need to. Send the auto-generated type.
John Saunders
But that means coupling the application with the particular web-service, no? And thus, nulling the use of the shared library.
Andreas Grech
I'm saying don't use the shared library on the client. The client will use and send the auto-generated proxy class - the stuff in Reference.cs.
John Saunders
+3  A: 

Maybe switching to WCF services is an option for you. As far as I remember, with a WCF service, you can reuse the same types on ther server and client side.

This article explains how to migrate an ASMX web service to a WCF service.

M4N
Yup, I now switched to WCF
Andreas Grech