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

时间:2013-09-12 14:16:23

标签: c# wcf web-services soap

我需要设置一个系统来完整地接收一条肥皂信息(该信息然后由第三方应用程序处理)并且无法理解如何使用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>

2 个答案:

答案 0 :(得分:1)

看看这种方法,看看它是否有帮助。在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>

答案 1 :(得分:1)

您可以使用WCF通用合约:

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