【问题标题】:C# SOAP-Service Missing namespace on Method Parameters方法参数上的 C# SOAP-Service 缺少命名空间
【发布时间】:2018-08-03 10:16:35
【问题描述】:

我在使用 C# 编写的 Web 服务时遇到问题。在我想用参数调用方法之前它工作正常,使用我的 wsdl 从外部供应商生成的 SOAP 请求如下所示:

<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>

这是我的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);

使用 SoapUI 或通过服务参考在 C# 中设置测试客户端,它可以正常工作。

唯一的问题是登录方法或任何其他带有参数的方法的参数,在传递参数的节点中缺少名称空间前缀 (nsl)。例如,当我在 SOAPUi 中手动添加它们时,它就像魅力一样。有没有办法在不检查每个传入请求的情况下添加命名空间前缀?

非常感谢!

【问题讨论】:

    标签: c# wcf soap namespaces


    【解决方案1】:

    我有点不确定为什么必须提供 Action 属性,因为 SOAP 操作可以在没有它的情况下工作。或许有线索……?

    或者,如果您确实希望保留它并希望为每个请求添加命名空间,那么您可以探索 XmlDocument 类和 XmlNode 类。这里有一个简单的代码片段,可以在特定节点添加命名空间 -

    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/"); 
    

    【讨论】:

    • 我必须向服务用户提供 URN。我不知道为什么,但它只适用于 Action 属性。在我上面提到的 SOAP 消息中,如何获取 userName 和 userPasswordNode?就像那个 XmlDocument req = new XmlDocument(); //加载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);
    • 要访问您需要这样做的子节点 - XmlNode 节点; // 将 Xml 解析成 XmlDoc node = xdoc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns1:login", nsmgr); // 这将为您提供一个“节点”,其中包含对子节点用户名和密码的引用。这将为您提供带有子节点的节点引用。为每个循环使用一个 -
    • foreach (XmlNode n in nodes) { if (n.Name == "a_sDMSUserName") { // Do something } } 如果对您有用,请标记为答案或投票。谢谢。
    • 谢谢伙计,这很有帮助!!我现在可以更改元素的前缀,但它只更改名称而不更改本地名称。例如,当我将我的 xml 打印到控制台时,没有添加前缀。不过还是谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多