【问题标题】:How to fire an event (client side) when I call a WCF service当我调用 WCF 服务时如何触发事件(客户端)
【发布时间】:2011-03-24 17:14:31
【问题描述】:

我想在每次调用 WCF 服务时触发一个事件。

我尝试了以下方法:

var factory = new ChannelFactory<TService>(binding, endPointAdress);

factory.Credentials.UserName.UserName = username;
factory.Credentials.UserName.Password = password;

var proxy = factory.CreateChannel();

((IContextChannel)this.Proxy).Opened += new EventHandler(FactoryOpeningEventHandler);
this.Factory.Opened += new EventHandler(FactoryOpeningEventHandler);

上面的问题是该事件仅在打开代理时调用,但我想在通过此代理进行调用时触发该事件,而不仅仅是在它打开时。我知道 IContextChannel 没有可以做我想做的事情,所以我想要一个解决方法。

【问题讨论】:

    标签: c# wcf service proxy channelfactory


    【解决方案1】:

    首先创建一个实现IDispatchMessageInspector(发送时)和IClientMessageInspector(接收时)接口的检查器类。

    public class SendReceiveInspector : IDispatchMessageInspector, IClientMessageInspector
    {
    
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // TODO: Fire your event here if needed
            return null;
        }
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            // TODO: Fire your event here if needed
        }
    
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // TODO: Fire your event here if needed
        }
    
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            // TODO: Fire your event here if needed
            return null;
        }
    }
    

    在你有了你的检查器类之后,你必须通过一个行为来注册它。

    public class SendReceiveBehavior : IEndpointBehavior, IServiceBehavior
    {
        void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new SendReceiveInspector());
        }
    
        void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
        }
    
        void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            // Leave empty
        }
    
        void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
        {
            // Leave empty
        }
    
        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
        {
            foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers)
            {
                foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
                {
                    eDispatcher.DispatchRuntime.MessageInspectors.Add(new SendReceiveInspector());
                }
            }
        }
    
        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
            // Leave empty
        }
    
        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            // Leave empty
        }
    }
    

    最后,您必须将该行为注册到您的服务描述中:

    host.Description.Behaviors.Add(new SendReceiveBehavior ());
    foreach (ServiceEndpoint se in host.Description.Endpoints)
        se.Behaviors.Add(new SendReceiveBehavior ());
    

    您可以在http://msdn.microsoft.com/en-us/magazine/cc163302.aspx了解更多关于扩展 WCF 的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2019-03-29
      • 1970-01-01
      相关资源
      最近更新 更多