Call to service with System.Net.WebClient, adding a contract behavior, is it possible? RRS feed

  • Question

  • Hello!

    Is it possible, while calling a service using System.Net.WebClient, to add a IDispatchMessageInspector behavior, just like with a real channel?

    I made a winform tester for all our webservices, and all the testers are listed in a XML file, with the attributes, no contracts no proxy, it allowed much more flexibility for testing and for generating fields corresponding to the attributes.

    So I generate the envelope in a string and call the services using WebClient class. So far, so good. It works. I'm happy.

    The problem:

    In our organization,

    a header is added with some informations about the caller,

    and a behavior is added so that information can be validated before the service is actually called,

    Dimcontexte AsNewUtiliseContexteSessionWCFAttribute()


    UtiliseContexteSessionWCFAttribute which implements IContractBehavior, and adds a messageinspector on  Then, AfterReceiveRequest on the server side, takes the customheader and validates it against tokens previously generated, and that allows access to the service.

    Is it possible to hack my way to put a behavior in a WebClient or HttpWebRequest?  Thank you!!!

    • Edited by Azumaria Wednesday, April 15, 2015 3:29 PM
    Wednesday, April 15, 2015 3:26 PM


  • Hi Azumaria,
       As per your case, I shared the corresponding details below :
    Any service (dispatcher) message inspector must implement the two IDispatchMessageInspector methods AfterReceiveRequest and BeforeSendReply(Message, Object).

    Add this in you config file :

          type="CustomHeaderInspector , MyDLL, Version=, Culture=neutral, PublicKeyToken=null"

    Then the extension will be handled in your WCF stack. The service itself has no notion of the serviceInterceptors.So the WCF stack will inject you Inspector.

    For more information about IDispatchMessageInspector Interface, refer the following link :

    Thursday, April 16, 2015 8:57 AM