views:

119

answers:

2

Background

I have made a Web Service in Visual Studio, and I'm trying to consume it using the automatically generated proxy class. The Web Service returns a class that I have implemented, containing a List.

Question

The proxy class has automatically generated methods to send the SOAP to the web service. It uses the Invoke() method to execute the call, and then casts the result as a DataSet. How can I get this object back into the class I know it is?

I know that I can hand-edit the auto-generated file, but that's not very maintainable, so I don't want to go down that route (any time the Web Service is rebuilt, the changes would have to be made again).

Is there a way to tell the generated class to be more specific, and actually use the correct data type? Or do I have to write a clunky set of deserialisers to get my data back into the correct shape?

Example

One method in my Web Service class:

[WebMethod]
public UpdateList RetrieveUpdates(long sessionID, string configurationVersion, string coreVersion, string researcherDBVersion)
{ ... }

Adding the class as a Web Reference generates the following proxy method:

public DataSet RetrieveUpdates(long sessionID, string configurationVersion, string coreVersion, string researcherDBVersion) {
    object[] results = this.Invoke("RetrieveUpdates", new object[] {
        sessionID,
        configurationVersion,
        coreVersion,
        researcherDBVersion});
   return ((DataSet)(results[0]));
}

The DataSet I receive from this method is always empty (because you can't cast from my class to a DataSet).

Thanks in advance

A: 

Unless your client code knows about your custom class (e.g. has a reference to the assembly) you will not be able to retrieve an object of that type from the service.

It sounds like what you are looking to do is share types across a service layer. In order to do that you will either have to give your client app a copy of the assembly that has the UpdateList type or you will need to look at something like WCF.

Andrew Hare
My client code does have a reference to the project defining the class; I can manually change the auto-generated Web Service proxy class to return the UpdateList class, and everything works fine.
Symmetric
The problem is that I don't want to have to manually add in changes to the generated file every time the web service changes.
Symmetric
+1  A: 

Since Web References generate partial classes, you should be able to add to your project a partial class extension to the proxy class that reimplements just the method in question (just copy and paste it) but changes the return type (and the name, of course). If the method signature changes, you'll have to update your extension, but at least if that doesn't happen and you regenerate the proxy you won't have to reapply any changes (and you can still use any other generated classes/methods as is).

I've used this approach before to "fix" proxy classes (for instance, to add SOAP headers that aren't defined in the WSDL), and while not ideal, it does work.

Eric Rosenberger
I like this solution - as you say, it's not ideal (the generated file should not be putting us in this position), but it's definitely the best solution to the problem that I can see. Thanks!
Symmetric