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

问题描述 投票:1回答:1

我有一个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需要做什么?

c# wcf soap
1个回答
1
投票

您可以自定义ActionMessageFilter,ActionMessageFilter用于匹配soap消息的操作。

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;
    }
}

添加endpointBehavior。

 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

© www.soinside.com 2019 - 2024. All rights reserved.