如何识别调用操作的WCF端点?

时间:2009-11-18 23:40:35

标签: wcf

如果您的服务器在不同的地址托管多个相同类型的端点,是否可以识别特定请求所来自的地址?说是为了记录目的?

受挫的例子:

_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?
    }
}

我宁愿不创建单例实例并使用id传递它。

1 个答案:

答案 0 :(得分:4)

当然,您可以从OperationContext获取此信息,如下所示:

EndpointAddress address = OperationContext.Current.EndpointDispatcher.EndpointAddress;

Debug.WriteLine(address.Uri);