I’d like to fire an event every time I call a WCF service.
I’ve tried the following:
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);
The problem with the above is that the event is only called when the proxy is opened, but I want to fire the event when a call is made trough this proxy, not only when it opens. I know there is no event for the IContextChannel that can do what I want, so I would like to have a workaround.
You start by creating an inspector class that implement both
IDispatchMessageInspector(when sending) andIClientMessageInspector(when receiving) interfaces.After you have your inspector class, you have to register it through a behaviour.
Finally, you have to register that behaviour to your service description:
You can learn more about extending WCF at http://msdn.microsoft.com/en-us/magazine/cc163302.aspx