1

我正在尝试在我的项目中使用基于第三方肥皂的 Web 服务。通过soap-ui发送手动soap请求时,它会超时,但是当我通过soap-ui添加启用ws-addressing的检查时,它可以顺利运行。

现在的问题是,我无法将它集成到我现有的应用程序中,因为在任何地方的代码中都没有启用 ws-addressing。关于如何做到这一点的任何建议?

代码库在 c# 中

4

1 回答 1

1

选中或未选中“启用/禁用 WS-A 寻址”的 SoapUI 的不同之处在于<wsa:Action>默认情况下添加到 SOAP 标头的标记。

如果您使用 WCF,您可以添加 WS-Addressing 标头,如如何在 WCF 中使用 WS-Addressing 并设置 wsa:replyto 标头?我将从添加 Action 标头开始。

编辑:知道SoapExtension使用了该类,这是一个将WS-Addressing Action标头添加到客户端发送的请求的完整示例:

public class WebServiceSOAPExtension : SoapExtension
{
    private Stream inwardStream;
    private Stream outwardStream;

    public override Stream ChainStream(Stream stream)
    {
        outwardStream = stream;
        inwardStream = new MemoryStream();
        return inwardStream;
    }

    public override object GetInitializer(Type serviceType)
    {
        return null;
    }

    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return null;
    }

    public override void Initialize(object initializer)
    {
        return;
    }

    public override void ProcessMessage(System.Web.Services.Protocols.SoapMessage message)
    {
        switch (message.Stage)
        {
            case SoapMessageStage.BeforeSerialize:
                break;
            case SoapMessageStage.AfterDeserialize:
                break;
            case SoapMessageStage.BeforeDeserialize:
                RewriteResponse();
                break;
            case SoapMessageStage.AfterSerialize:
                RewriteRequest();
                break;
        }
    }

    private void RewriteResponse()
    {
        string message;
        var streamReader = new StreamReader(outwardStream);
        var streamWriter = new StreamWriter(inwardStream);
        message = streamReader.ReadToEnd();
        streamWriter.Write(message);
        streamWriter.Flush();
        inwardStream.Position = 0;
    }

    private void RewriteRequest()
    {
        string message;
        XmlDocument xmlDoc = new XmlDocument();
        inwardStream.Position = 0;
        var streamReader = new StreamReader(inwardStream);
        var streamWriter = new StreamWriter(outwardStream);
        message = streamReader.ReadToEnd();

        xmlDoc.LoadXml(message);
        var bodyNode = xmlDoc.GetElementsByTagName("Body", "http://www.w3.org/2003/05/soap-envelope/")[0];
        var headerNode = xmlDoc.CreateElement("s", "Header", "http://www.w3.org/2003/05/soap-envelope/");
        var actionNode = xmlDoc.CreateElement("wsa", "Action", "http://www.w3.org/2004/12/addressing");
        actionNode.InnerText = "http://sampleserver.example/Action";
        headerNode.AppendChild(actionNode);
        bodyNode.ParentNode.InsertBefore(headerNode, bodyNode);

        message = xmlDoc.InnerXml;
        streamWriter.Write(message);
        streamWriter.Flush();
    }
}

此类必须在客户端的 Web 应用程序中注册:

<webServices>
  <soapExtensionTypes>
    <add type="WebApplication1.WebServiceSOAPExtension, WebApplication1" priority="1" group="Low" />
  </soapExtensionTypes>
</webServices>
于 2013-10-31T09:58:35.533 回答