在执行任何合同工作之前,如何在WCF服务中查看SOAP消息?

时间:2019-03-27 20:15:15

标签: c# wcf soap

我有一个WCF服务,该服务是从供应商提供的WSDL派生的。当供应商的客户致电我的服务时,他们会收到错误“ The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher.

在抛出此错误之前,我想查看传入的SOAP消息。

我尝试使用IServiceBehavior构建属性:

 [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
 public sealed class AuditServiceBehavior : Attribute, IServiceBehavior
 {
    public AuditServiceBehavior() {  }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        Trace.TraceInformation("AuditService.AddBindingParameters called.");
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        // Seems like the right place to invoke something?
        Trace.TraceInformation("AuditService.ApplyDispatchBehavior called.");
    }
 }

将此属性添加到我的服务实现中可以查看跟踪消息,但是这些消息在启动服务时发生。我在IOperationBehavior中添加了一个属性,但是似乎所有这些方法都是在合同解决后发生的。

要查看传入的SOAP,我需要做什么?

1 个答案:

答案 0 :(得分:1)

您可以自定义ActionMessageFilter,ActionMessageFilter用于匹配肥皂消息的动作。

public  class MyActionMesageFilter:ActionMessageFilter
{
    public MyActionMesageFilter(params string[] actions):base(actions)
    {

    }

    public override bool Match(Message message)
    {
        string mes = message.ToString();
        bool re = base.Match(message);
       string action = message.Headers.Action;
        return  re;
    }
}

添加一个端点行为。

 public class ReplaceActionFilterEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

                   endpointDispatcher.AddressFilter = new MyActionMesageFilter("http://tempuri.org/IEmployeeService/GetEmployee");

    }

    public void Validate(ServiceEndpoint endpoint)
    {

    }
}

还有主持人。

 using (ServiceHost host = new ServiceHost(typeof(Service.EmployeeService)))
        {
            host.Description.Endpoints[0].EndpointBehaviors.Add(new ReplaceActionFilterEndpointBehavior());
            host.Opened += delegate
            {
                Console.WriteLine("hello");
            };
            host.Open();
              Console.Read();
        }

结果。

enter image description here

有关自定义ActionMessageFilter的更多信息,您可以参考 https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/custom-message-filter