Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4094666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:46:25+00:00 2026-05-20T19:46:25+00:00

I’d like to fire an event every time I call a WCF service. I’ve

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:46:26+00:00Added an answer on May 20, 2026 at 7:46 pm

    You start by creating an inspector class that implement both IDispatchMessageInspector (when sending) and IClientMessageInspector (when receiving) interfaces.

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

    After you have your inspector class, you have to register it through a behaviour.

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

    Finally, you have to register that behaviour to your service description:

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

    You can learn more about extending WCF at http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.