Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which wcf endpoint is being used on the server

I have a wcf service thats exposing a service using two endpoints. One endpoint is used for web service calls while the other is using rest.

Is there a way to determine from which endpoint the server functions are being called?

like image 407
Marcom Avatar asked Feb 08 '11 14:02

Marcom


People also ask

Where we define endpoints in WCF?

Endpoints provide clients access to the functionality offered by a WCF service. Each endpoint consists of four properties: An address that indicates where the endpoint can be found. A binding that specifies how a client can communicate with the endpoint.

How do you call a WCF service reference in C#?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.


2 Answers

Actually, contrary to what I thought - it's actually pretty easy to find out what endpoint the service was called on. In your service method, add these lines of code:

OperationContext oc = OperationContext.Current;

if(oc != null)
{
    string wasCalledOn = oc.EndpointDispatcher.EndpointAddress.Uri.ToString();
}

But as I said : I would use this very wisely and "defensively" - don't start changing service behavior based on what endpoint your method was called on - that would be very bad design!

I tried this with several SOAP endpoints - not 100% sure how REST will be handled (but most likely the same or very similarly) - try it out!

But for logging, this should work just fine!

like image 132
marc_s Avatar answered Oct 13 '22 22:10

marc_s


Lifted from the blog entry here:

You just need to look at the LocalAddress on the Channel of the current Operation Context in the method:

OperationContext.Current.Channel.LocalAddress
like image 25
Matt Davis Avatar answered Oct 13 '22 22:10

Matt Davis