Classes in different sub-namespaces appear at the top level in the WSDL

I am creating a web service with many classes, many of which may contain references to other classes. Sometimes these "subclasses" have the same name (for example, client and client), but are actually different, so I put them in different namespaces (for example, MyRoot.Sub1, MyRoot.Sub2, etc.).

When I create a service, the generated WSDL puts all of these classes at the top level without specifying any subnamespace delimitation. Therefore, it will define two versions of the "Customer". Curiously, he doesn't complain about the build process.

When I host a service and try to link to it in the browser, I get the following error:

The top XML element "Customer" from the namespace 'RE: http: // ...' links the different types MyRoot.Sub1.Customer and MyRoot.Sub2.Customer. Use XML attributes to specify a different XML name or namespace for the element or types.

It looks like I could use the XmlTypeAttribute TypeName to provide a unique class name, but that makes the documentation rather complicated as everything will have two names. Considering differentiation is already specified by namespaces, is there a way to use this information? I haven't tried using TypeName = "Sub1.Customer" etc., but none of the examples I've seen have used this notation, so I'm skeptical.

Is there something else obvious that I'm missing?

+2


source to share


2 answers


SOAP doesn't support hierarchical namespaces, so I figured there were only two solutions:

  • Make sure different classes are uniquely named across all methods of the web service.
  • Split a single web service into multiple services so that each one has its own implicit namespace.


Since the latter option requires more administration (both in code and in IIS), I decided to give the classes unique names. I can still end up splitting the service into multiples (for organizing or loading), but that's not necessary if the class names are unique.

+4


source


You will need to use the XmlType attribute to disambiguate the two Customer classes. The only value you need to set is the namespace, so each client is in its own XML namespace.

See the following example:



namespace NS1
{
    [XmlType(Namespace="com.me.ns1")]
    public class Customer
    {
        public string FirstName;
        public string LastName;
    }
}

namespace NS2
{
    [XmlType(Namespace = "com.me.ns2")]
    public class Customer
    {
        public string Name;
    }
}

namespace WebService1
{
    [WebService(Namespace = "http://tempuri.org/")]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public NS2.Customer HelloWorld(NS1.Customer customer)
        {
            return new NS2.Customer() { Name = customer.FirstName + " " + customer.LastName } ;
        }
    }
}

      

Disclaimer: just an example, not a recommended coding practice. :)

0


source







All Articles