2011-03-24 14 views
5

पर कॉल करता हूं तो किसी ईवेंट (क्लाइंट साइड) को कैसे आग लगाना है जब भी मैं डब्लूसीएफ सेवा को कॉल करता हूं, मैं हर बार एक ईवेंट को आग लगाना चाहता हूं।जब मैं डब्लूसीएफ सेवा

मैं कोशिश की है निम्नलिखित:

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

आपके इंस्पेक्टर वर्ग के बाद, आपको इसे एक व्यवहार के माध्यम से पंजीकृत करना होगा। http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

पर

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

आप WCF विस्तार के बारे में अधिक सीख सकते हैं:

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

अंत में, आप अपनी सेवा वर्णन करने के लिए है कि व्यवहार रजिस्टर करने के लिए है

संबंधित मुद्दे