据说在WCF 3.5中可以很容易地获得客户端IP地址,但在WCF 3.0中不行。有人知道怎么做吗?
这在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
来获得它。 - Jader Dias如果你的目标是 .NET 3.0 SP1,那么是可以的。
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
Credits: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx
OperationContext.Current
时第一行返回 null,那么你目前遇到的问题是什么?你可以怎么做? - Franck