Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 734
Bernard Avatar asked Feb 24 '09 00:02

Bernard


3 Answers

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.

like image 131
Ryan Morlok Avatar answered Oct 04 '22 03:10

Ryan Morlok


I just searched for "references a method and a type" and found a Connect bug report "System.InvalidOperationException: The XML element * from namespace * references a references a method and a type". In this case, there is an operation and an element with the exact same name (both local name and namespace).


It's worth noting part of the response from Microsoft:

We're no longer making enhancements to ASMX; we continue to support its existing functionality, but where possible, we recommend using WCF instead.

like image 34
John Saunders Avatar answered Oct 04 '22 03:10

John Saunders


I found another case that raise the error! Here's my code:

[WebMethod]
public CheckUpdateResponse CheckUpdate()
{
...
}

Ok, let me explain: the return type CheckUpdateResponse is a structure, CheckUpdate() is the method. So, in the WSDL .NET add automatically a "Response" suffix to the method name CheckUpdate in one of the XML element to describe the return value of the method.

Et voilà: it found a duplicate element and give the error "Change the method's message name using WebMethodAttribute..."

Solution? I renamed the return type to "CheckUpdateResult" and now everything works well!

I hope this will help someone!

like image 34
Seraphim's Avatar answered Oct 04 '22 01:10

Seraphim's