【发布时间】:2010-09-10 17:25:34
【问题描述】:
显然,您可以在 WCF 3.5 中轻松获取客户端 IP 地址,但在 WCF 3.0 中则不行。有人知道怎么做吗?
【问题讨论】:
标签: wcf
显然,您可以在 WCF 3.5 中轻松获取客户端 IP 地址,但在 WCF 3.0 中则不行。有人知道怎么做吗?
【问题讨论】:
标签: wcf
这在 3.0 中对您没有帮助,但我只能看到人们发现这个问题并感到沮丧,因为他们试图在 3.5 中获取客户端 IP 地址。所以,这里有一些应该可以工作的代码:
using System.ServiceModel;
using System.ServiceModel.Channels;
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
【讨论】:
事实证明您可以,只要 (a) 您的服务托管在 Web 服务中(显然)并且 (b) 您启用 AspNetCompatibility 模式,如下所示:
<system.serviceModel>
<!-- this enables WCF services to access ASP.Net http context -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
</system.serviceModel>
然后你可以通过以下方式获取IP地址:
HttpContext.Current.Request.UserHostAddress
【讨论】:
HttpContext.Current.Request.UserHostAddress得到它
如果您的目标是 .NET 3.0 SP1,则可以。
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
学分: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx
【讨论】: