以编程方式在soap请求中进行ws-addressing

时间:2013-10-31 08:39:41

标签: c# web-services soap

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

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

代码库位于c#

1 个答案:

答案 0 :(得分:1)

在SoapUI中选中“启用/禁用WS-A地址”是否有所不同是默认情况下向SOAP标头添加的<wsa:Action>标记。

如果您使用WCF,您可以添加How do I use WS-Addressing in WCF and set the wsa:replyto header?中指定的WS-Addressing标头,我将从添加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>