2011-03-24 15 views
5

Bir WCF hizmetini her aradığımda bir olayı tetiklemek istiyorum.Bir WCF hizmetini çağırdığımda bir olay (istemci tarafı) nasıl tetiklenir

denedim aşağıdadır:

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

yukarıdaki sorun vekil açıldığında olay sadece denir, ama bir çağrı yalak bu yapılır olayı istiyorum Proxy, sadece açıldığında değil. IContextChannel'in istediğimi yapabileceği bir olay olmadığını biliyorum, bu yüzden bir çözüm bulmak istiyorum.

cevap

6

Hem IDispatchMessageInspector (gönderirken) hem de IClientMessageInspector (alırken) arabirimleri uygulayan bir denetçi sınıfı oluşturarak başlarsınız.

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; 
    } 
} 

Denetçi sınıfınızı aldıktan sonra, bunu bir davranışla kaydetmeniz gerekir. http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

de

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

Sen WCF uzanan hakkında daha fazla bilgi edinebilirsiniz:

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

Son olarak, servis açıklamasına davranışı kayıt var

İlgili konular