Web Services 将消息检查器添加到代码中的WCF服务,而不是配置文件

rqqzpn5f  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(163)

我通过一个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();
        }
    }
p3rjfoxz

p3rjfoxz1#

你可以通过以下方式使用代码来实现。
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,代码与上面相同,只删除属性。

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());
7xllpg7q

7xllpg7q2#

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

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

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

相关问题