【问题标题】:Modify namespace prefix Web Services C#修改命名空间前缀 Web 服务 C#
【发布时间】:2015-06-05 12:14:40
【问题描述】:

我们正在接收一个 SOAP 请求,但我们无法对其进行全部修改。所以现在我们必须改变我们的网络服务以适应呼叫。我几乎在那里,期望它们的命名空间的前缀与生成的前缀不同,除非它相同,否则它不会命中方法:

这是我的网络服务:

[WebService(Namespace = "http://domain.co.za/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
public class MyWebservice : System.Web.Services.WebService
{   
    [WebMethod]        
    public string PushMessage(object payload)
    {
    }
}

还有他们发送的 SOAP:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://domain.co.za/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
    <ns0:username soapenv:actor=""></ns0:username>
    <ns0:password soapenv:actor=""></ns0:password>
</soapenv:Header>
<soapenv:Body>
    <ns0:PushMessage>
    <type>confirmation</type>
    <autoRelease>true</autoRelease>
        <payload>
            <Confirmation>
                <MessageEnvelope>
                .
                .
                </MessageEnvelope>
            </Confirmation>
        </payload>
    </ns0:PushMessage>
</soapenv:Body>
</soapenv:Envelope>

正如您所看到的,他们使用的是 ns0,WDSL 生成的是 web,无论如何强制我的命名空间生成为 ns0: 而不是默认的 web:

有没有办法从有效负载元素中删除 ns0/web ?他们没有发送带有有效负载的命名空间,但 WDSL 将其生成为 web:payload

谢谢。

【问题讨论】:

    标签: c# xml web-services soap


    【解决方案1】:

    试试正则表达式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using System.Text.RegularExpressions;
    
    namespace Calendar
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input =
                            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://domain.co.za/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                  "<soapenv:Header>" +
                    "<ns0:username soapenv:actor=\"\">            </ns0:username>" +
                    "<ns0:password soapenv:actor=\"\">            </ns0:password>" +
                  "</soapenv:Header>" +
                  "<soapenv:Body>" +
                    "<ns0:PushMessage>" +
                      "<type>confirmation            </type>" +
                "<autoRelease>true            </autoRelease>" +
                      "<payload>" +
                        "<Confirmation>" +
                          "<MessageEnvelope>" +
                          "</MessageEnvelope>" +
                        "</Confirmation>" +
                      "</payload>" +
                    "</ns0:PushMessage>" +
                  "</soapenv:Body>" +
                "</soapenv:Envelope>";
    
                string pattern = "(</?)(\\w:)(\\w)";
                Regex expr = new Regex(pattern);
                input = expr.Replace(input, "$1$3");
                XDocument doc = XDocument.Parse(input);
            }
        }
    
    
    
    }
    

    【讨论】:

    • 问题是我没有发出请求,也无法控制它,所以我必须完全按照上面的消息形成我的 webservice 方法。
    • 您要求删除 ns,这就是我所做的。因此,您可以在处理消息之前以任何方式修改接收消息。
    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多