将消息检查器通过代码而不是配置文件添加到WCF服务中

问题描述 投票:3回答:2

我正在研究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();
        }
    }
c# .net web-services wcf service
2个回答
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)
    {
    }
    }
    

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

[TraceServiceBehavior]
public class Service1 : IService1
{
     // Methods
}
  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)
    {
    }
    }
    

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

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

0
投票

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

ServiceEndpoint endpoint;
endpoint = host.AddServiceEndpoint(typeof(ISimpleService), httpBinding, "");
endpoint.EndpointBehaviors.Add(new TraceMessageBehavior());

所以我在应用程序配置中定义了扩展名和endpointBehavior,但是端点是在源文件中定义的。因此,我也想在源文件中添加endpointBehavior。

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