![](/img/trans.png)
[英]Retrieve destination IP address from OperationContext MessageProperties
[英]WCF operationContext returns gateway instead of client 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;
}
在我的本地主机中,它工作正常,但是当我在服务器上安装我的服务进行测试时,得到的回报是客户端的网关,对此原因的任何见解都可以?
请尝试以下一种方法:首先启用AspNetCompatibility模式,如下所示:
<system.serviceModel>
<!-- this enables WCF services to access ASP.Net http context -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
然后您可以通过以下方式获取IP地址:
HttpContext.Current.Request.UserHostAddress
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.