2011-03-24 150 views
5

我想在每次調用WCF服務時觸發一個事件。如何在調用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沒有可以做我想做的事情,所以我想有一個解決方法。

回答

6

您首先創建一個檢查器類,該類實現了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()); 

您可以瞭解更多有關擴展WCF在http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

相關問題