C# web-service client: multiple web-service methods with same (complex) return type?

Bernard picture Bernard · Feb 24, 2009 · Viewed 14.9k times · Source

I am chipping away at building a client for a Java B2B web-service at the moment and I think I have identified the cause of a problem we have been having for quite some time. Unfortunately I'm unable to post the WSDL.

Apparently my auto-generated proxy code (via wsdl.exe: have to use WSE 3.0 due to WCF not supporting password digest) is not able to handle the web-service's WSDL having multiple web-methods with the same complex return type.

Take for example - a web-service that defines the following methods:

Public ComplexTypeX Blah();
Public ComplexTypeX Blue();
Public ComplexTypeX Foo();
Public ComplexTypeY Bar();

In my Reference.cs file, if I comment out all code that calls any two of Blah(), Blue() or Foo(), then the remaining uncommented method can be called no problem. However, if I have more than one of these three methods not commented out (say, Blah() and Foo()), then I get the following error message upon instantiation of the web-service client code:

"Method Blah can not be reflected." "The XML element 'ComplexTypeX' from namespace 'http://some.url' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute."

Now, there is definitely no ComplexTypeX method defined as part of the web-service, so I can only assume that .NET (or at least wsdl.exe) does not allow you to use a web-service that returns complex (user-defined) types of the same type across multiple methods ... right?

Answer

Ryan Morlok picture Ryan Morlok · Jul 2, 2010

I ran into a similar problem, and here is what I found:

I had defined a complex type to return as a response:

public class FooResponse {...}

[WebMethod]
public FooResponse Foo() {...}

Note that here the exact name pairing of Foo/Foo+Response is important. When I changed the method name as follows, the problem went away:

public class FooResponse {...}

[WebMethod]
public FooResponse Fooxxx() {...}

What I believe is happening is .NET is attempting to automatically wrap the response coming from the Foo method with an element named FooResponse. The use of that same name as the object you want to return creates ambiguity. Try changing the name of your response object, or the name of your method to avoid this collision.