Share via


DatacontractSerializer and ASMX service using types with Namespaces

When we try to generate a proxy out of an ASMX service with svcutil, there might be some interesting scenarios. One that I came across is when the service exposes types that have a root namespace. Now if you had only that type as the parameter then these doesnt seem to be any issue. As soon as you mix a primitive type with them then you get this error.

 

E:\Sajay\Visual Studio 2005\WebSites\AsmxDC>svcutil https://localhost:2665/AsmxD

C/Service.asmx?WSDL

Microsoft (R) Service Model Metadata Tool

[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4312.0]

Copyright (c) Microsoft Corporation. All rights reserved.

Attempting to download metadata from 'https://localhost:2665/AsmxDC/Service.asmx?

WSDL' using WS-Metadata Exchange or DISCO.

Error: An error occurred in the tool.

Error: Wrapper type for message HelloWorldRequest cannot be projected as a data

contract type since it has multiple namespaces. Consider using the XmlSerializer

Now if we really wanted to use the Datacontract serializer then its quite simple. Provided we didnt get this exception before we added primitive type parameter and the type itself can use the DatacontractSerializer then we can add the XML namespace attribute to the parameter.

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Serialization;

[XmlRoot(Namespace="https://mywebSite/Types")]

public class Item

{

    public string Name;

    public string Description;

}

[WebService(Namespace = "https://myWebsite/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    [WebMethod]

    public string HelloWorld ([XmlElement(Namespace = "https://mywebSite/Types")] string name,

        Item item)

    {

        return name+" says Hello World";

    }

   

}