Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I intercept all calls to methods in a WCF .svc service?

Tags:

asp.net

wcf

Ive got a WCF service which has multiple web methods in it. I want to be able to intercept the request on all methods and look at the Ip address. Id rather not put the logic into a method call at the top of each called web method is there a way to intercept all calls to these methods from one place?

If it was a page I would write a base page object but im nout sure if there are events raised on a wcf call?

like image 654
Exitos Avatar asked Sep 20 '11 10:09

Exitos


2 Answers

WCF allows you to implement interceptors that are added to the stack. See this link for an example. I´m not sure whether this allows you the extract the senders IP but I think it´s worth a try.

like image 109
Dominik Avatar answered Oct 04 '22 14:10

Dominik


You can implement IDispatchMessageInspector and do something like this.

public object AfterReceiveRequest(ref Message request, 
IClientChannel channel, InstanceContext instanceContext)
    {
        RemoteEndpointMessageProperty remoteEndpoint = request.Properties
    [RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

        //remoteEndpoint.Address will give you the address.  

        return null;
    }
like image 21
Hari Subramaniam Avatar answered Oct 04 '22 15:10

Hari Subramaniam