Search code examples
c#wcfsoapnamespaces

C# SOAP-Service Missing namespace on Method Parameters


I am facing a problem with my webservice written in C#. It works fine until I want to call method with parameters, the SOAP Request generated from an external supplier using my wsdl looks like the following:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://127.0.0.1/AService</To>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">ANameSpace/login</Action>
  </s:Header>
  <soapenv:Body>
    <ns1:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="ANameSpace">
      <a_sDMSUserName xsi:type="xsd:string">test</a_sDMSUserName>
      <a_sDMSUserPassword xsi:type="xsd:string">oscar</a_sDMSUserPassword>
    </ns1:login>
  </soapenv:Body>
</soapenv:Envelope>

This is a part of the interface of my Service:

[ServiceContract (Namespace ="ANameSpace")]
    public interface IFordEcat
    {
        [OperationContract (Action ="ANameSpace/connect")]
        int connect();

        [OperationContract(Action = "ANameSpace/disconnect")]
        int disconnect();

        [OperationContract(Action = "ANameSpace/login")]
        string login(string a_sDMSUserName, string a_sDMSUserPassword);

        [OperationContract(Action = "ANameSpace/logout")]
        string logout(string a_sSessionID, string a_sDMSUserName, string a_sDMSUserPassword);

With SoapUI or a test client setup in C# via Service Reference it works fine.

The only problem is that the parameters of the login Method or any other Method with parameters, are missing the namespace prefix (nsl) in the nodes where are the parameters are passed. When I add them manually for example in SOAPUi it works like charm. Is there a way to add the namespace prefix without inspecting every incoming request?

Many thanks in advance!


Solution

  • I am a bit unsure why you have to provide the Action attribute, because SOAP operations can work without it. Perhaps there lies the clue ...?

    Alternatively, if you do wish to keep it and wish to add namespace for every request then you could explore XmlDocument class and XmlNode classes. A simple code snip is here which can add namespace at a specific node -

    XmlDocument xdoc = new XmlDocument();
    
    // Get Request Xml for each of the case
    xdoc.LoadXml(xmlContent);
    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
    nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
    nsmgr.AddNamespace("sc", "http://tempuri.org/");