WCF operationContext返回网关而不是客户端IP地址

时间:2014-12-01 14:45:23

标签: c# web-services wcf ip-address

我对开发WCF服务有点新意。我正在使用此代码获取客户端IP地址,

private String getRemoteIP()
{
    OperationContext context = OperationContext.Current;
    MessageProperties prop = context.IncomingMessageProperties;
    RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
    return endpoint.Address;
}

在我的localhost中它工作正常,但是当我在服务器中安装我的服务进行测试时,我得到的回报是客户端的网关,任何洞察原因可能是???

1 个答案:

答案 0 :(得分:1)

尝试这一个: 首先启用AspNetCompatibility模式,如下所示:

<system.serviceModel>
        <!-- this enables WCF services to access ASP.Net http context -->
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

然后您可以通过以下方式获取IP地址:

HttpContext.Current.Request.UserHostAddress