Determine which wcf endpoint is being used on the server

后端 未结 2 665
Happy的楠姐
Happy的楠姐 2021-02-19 08:51

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

2条回答
  •  梦如初夏
    2021-02-19 09:24

    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!

提交回复
热议问题