开发者

How do you identify the WCF endpoint that called your operation?

开发者 https://www.devze.com 2022-12-12 01:40 出处:网络
If you have a server that host multiple endpoints of the same type at different addresses, is it possible to identify the address from which a particular request came?Say for logging purposes?

If you have a server that host multiple endpoints of the same type at different addresses, is it possible to identify the address from which a particular request came? Say for logging purposes?

Contrived example:

_serviceHost = new ServiceHost(
    typeof(Service),
    new Uri("http://localhost:8000/SomeAddress"));

_serviceHost.AddServiceEndpoint(
    typeof(IService),
开发者_运维问答    new BasicHttpBinding(),
    string.Empty);

_serviceHost2 = new ServiceHost(
    typeof(Service),
    new Uri("http://localhost:8000/SomeOtherAddress"));

_serviceHost2.AddServiceEndpoint(
    typeof(IService),
    new BasicHttpBinding(),
    string.Empty); 

[ServiceContract()]
public interface IService
{
    [OperationContract]
    void Operation();
}

public class Service
{

    void Operation()
    {        
        //Which endpoint made this call?
    }
}

I would rather not create a singleton instance and pass it with an id.


Sure, you can get this information from the OperationContext like so:

EndpointAddress address = OperationContext.Current.EndpointDispatcher.EndpointAddress;

Debug.WriteLine(address.Uri);
0

精彩评论

暂无评论...
验证码 换一张
取 消