繁体   English   中英

是否可以在WCF方法中接收soap消息?

[英]Is it possible to receive a soap message in a WCF method?

我需要设置一个系统来完整地接收一条肥皂消息(该消息然后由第三方应用程序处理)并且无法让我知道如何使用WCF执行此操作。

传入消息的格式如下:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <s:Header>
      <wsa:MessageID>3AAAF216520F</wsa:MessageID>
      <wsa:Action>SendDocument</wsa:Action>
      <wsa:To>~serviceURI~</wsa:To>
      <wsa:From><wsa:Address>~fromServiceURI~</wsa:Address></wsa:From>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
          <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="D6CD5232-14CF-11DF-9423-1F9A910D4703">
          <wsu:Created>~Created~</wsu:Created>
          <wsu:Expires>~Expires~</wsu:Expires>
          </wsu:Timestamp>
          <wsse:UsernameToken>
              <wsse:Username>ePAQ</wsse:Username>
          </wsse:UsernameToken>
      </wsse:Security>
  </s:Header>
 <s:Body>
    <i:DistributionEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...    </i:DistributionEnvelope>  
  </s:Body>
</s:Envelope>

看看这种方法,看看它是否有用。 在WCF服务项目中实现消息检查器,我将为您提供一个用于登录/注销soap消息的实现示例。

public class LogSoapMessageInterceptor : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage(); //this step is important http://goo.gl/u4eBT
        Message message = buffer.CreateMessage(); //this step is important http://goo.gl/u4eBT

        StringBuilder sb = new StringBuilder();
        using (XmlWriter xw = XmlWriter.Create(sb))
        {
            message.WriteMessage(xw);
            xw.Close();
        }
        Logger.Log(String.Format("Received SOAP Request:\n{0}", sb.ToString()));

        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
        reply = buffer.CreateMessage();
        Logger.Log(String.Format("Sending SOAP Reply:\n{0}", buffer.CreateMessage().ToString()));
    }
}

public abstract class AbstractInterceptionBehavior<T> : IEndpointBehavior where T : IDispatchMessageInspector, new()
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        T inspector = new T();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

public class LogSoapMessageBehavior : AbstractInterceptionBehavior<LogSoapMessageInterceptor>
{
}

public class LogSoapMessageBehaviorExtensionElement : AbstractInterceptionBehaviorExtentionElement<LogSoapMessageBehavior>
{
}

//related configuration settings
<extensions>
  <behaviorExtensions>
    <add name="logSoapMessageBehavior"
         type="xyz.com.Web.Interceptors.LogSoapMessageBehaviorExtensionElement, xyz.com" />
  </behaviorExtensions>
</extensions>

您可以使用WCF通用合同:

[OperationContract(Action="*", ReplyAction="*")]
    System.ServiceModel.Channels.Message ProcessMessage(System.ServiceModel.Channels.Message msg);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM