Naming collision in ASMX web service

by Warlock on Jul.02, 2010, under .NET, C#, xml

I ran into an interesting problem today while working on an ASMX web service.

Everything compiled fine, but when I went to view the ASXM in the browser to manually invoke the methods to test it, the normal automatically generated info page didn’t come up. Trying to grab the WSDL also gave me a blank page.

I then attached the debugger to IIS to observe what was happening when I tried to view the ASMX info page. By breaking on exceptions, I was able to uncover the following error:

The XML element ‘XXX’ from namespace ‘http://tempuri.org/’ references a method and a type. Change the method’s message name using WebMethodAttribute or change the type’s root element using the XmlRootAttribute.

With a little more thinking, I was able uncover what I believe was going on.

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. By changing the name of my response object, or the name of my method I was able to avoid this collision.

I have also posted this information in response to a question on StackOverflow.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...