0

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!

1 Answer 1

1

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/"); 
Sign up to request clarification or add additional context in comments.

4 Comments

I have to provide a URN to the User of the Service. I dont know why but it works only with the Action attribute. In my above mentioned SOAP Message, how do you get the userName and userPasswordNode? Like that XmlDocument req = new XmlDocument(); //load xml req.LoadXml(request.ToString()); XmlNamespaceManager nsmgr = new XmlNamespaceManager(req.NameTable); nsmgr.AddNamespace("ns1", "EsteamFordEcat"); XmlNode userName = req.SelectSingleNode("//soapenv:Envelope/s:Header/soapenv:Body/ns1:Login/a_sDMSUserName", nsmgr);
To access the child nodes you need to do like this - XmlNode node; // Parse the Xml into XmlDoc node = xdoc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns1:login", nsmgr); // This will give you a 'node' having references to the child nodes UserName & Password. This will give you a node reference with child nodes. Use a for each loop -
foreach (XmlNode n in nodes) { if (n.Name == "a_sDMSUserName") { // Do something } } Please mark as answer or vote up if useful to you. Thanks.
Thanks mate this was very helpful!! I can now change the prefix of my element, but it changes only the name not the local name. When I'll print my xml to the console for example there is no added prefix. But thanks anyway!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.