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

本文关键字:添加 Inspector WCF 服务 Message 配置文件 代码 | 更新日期: 2023-09-27 18:04:16

我通过微软培训工具包(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();
        }
    }

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

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

  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)
     {
     }
     }
    

,然后用这个属性

装饰你的服务类
[TraceServiceBehavior]
public class Service1 : IService1
{
     // Methods
}
  • 创建从IServiceBehavior扩展的ServiceBehavior,与上面的代码相同,只删除属性

    public class 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)
        {
        }
    }
    
  • ,然后在ServiceHost中以编程方式添加Behavior。

    ServiceHost host = new ServiceHost(typeof(WcfService1.Service1));
    host.Description.Behaviors.Add(new WcfService1.TraceServiceBehavior());
                
    

    我可以简单地通过使用以下命令添加端点行为:

    ServiceEndpoint endpoint;
    endpoint = host.AddServiceEndpoint(typeof(ISimpleService), httpBinding, "");
    endpoint.EndpointBehaviors.Add(new TraceMessageBehavior());
    
    所以我在应用配置中定义了扩展名和endpointBehavior,但是端点是在源文件中定义的。因此,我也想在源文件中添加endpointBehavior。