使用代码而不是配置文件将Message Inspector添加到WCF服务

dcr*_*rer 3 .net c# service wcf web-services

我通过Microsoft培训工具包(WCF)中的示例进行研究。它需要将“消息检查”添加到服务。

到目前为止,我已经创建了检查实现类,消息行为类和消息行为类扩展。

与其通过配置文件添加行为,不如将其添加到服务主机文件中。以下是实现类...

 public class MessageTrace : IDispatchMessageInspector
    {
        private Message TraceMessage(MessageBuffer buffer)
        {
            Message msg = buffer.CreateMessage();
            StringBuilder sb = new StringBuilder("Message content");
            sb.Append(msg.ToString());
            Console.WriteLine(sb.ToString());
            return buffer.CreateMessage();
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            request = TraceMessage(request.CreateBufferedCopy(Int32.MaxValue));
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            reply = TraceMessage(reply.CreateBufferedCopy(Int32.MaxValue));
        }
    }

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

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {}

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

        public void Validate(ServiceEndpoint endpoint)
        {}
    }

 public class TraceMessageBehaviorExtension : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(TraceMessageBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new TraceMessageBehavior();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Abh*_*dha 5

您只能通过以下方式使用代码来实现。

  1. 首先在服务类上使用属性。创建一个继承自IServiceBehavior的新属性。

    [AttributeUsage(AttributeTargets.Class)]
    public class TraceServiceBehavior : Attribute, IServiceBehavior
    {
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }
    
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
            {
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new MessageTrace());
            }
        }
    }
    
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)

然后使用此属性装饰您的服务类

[TraceServiceBehavior]
public class Service1 : IService1
{
     // Methods
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建从IServiceBehavior扩展的ServiceBehavior,与上面的代码相同,仅删除属性。

    公共类TraceServiceBehavior:IServiceBehavior {

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }
    
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
            {
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new MessageTrace());
            }
        }
    }
    
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)

然后在ServiceHost中,以编程方式添加“行为”。

ServiceHost host = new ServiceHost(typeof(WcfService1.Service1));
            host.Description.Behaviors.Add(new WcfService1.TraceServiceBehavior());
Run Code Online (Sandbox Code Playgroud)