首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在WCF 3.0中获取客户端IP地址

在WCF 3.0中获取客户端IP地址
EN

Stack Overflow用户
提问于 2008-09-18 14:40:59
回答 3查看 64.1K关注 0票数 82

显然,您可以在WCF 3.5中轻松获得客户端IP地址,但在WCF 3.0中则不然。有人知道怎么做吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-10-09 16:34:44

事实证明您可以,只要(a)您的服务托管在Web service中(显然),并且(b)您启用了AspNetCompatibility模式,如下所示:

代码语言:javascript
复制
    <system.serviceModel>
            <!-- this enables WCF services to access ASP.Net http context -->
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
    </system.serviceModel>

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

代码语言:javascript
复制
HttpContext.Current.Request.UserHostAddress
票数 36
EN

Stack Overflow用户

发布于 2008-09-18 15:11:11

这在3.0中对你没有帮助,但我可以看到人们发现了这个问题,并感到沮丧,因为他们试图在3.5中获取客户端IP地址。因此,下面是一些应该可以工作的代码:

代码语言:javascript
复制
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;
票数 152
EN

Stack Overflow用户

发布于 2008-10-10 08:26:25

如果您的目标是.NET 3.0 SP1,则可以。

代码语言:javascript
复制
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

参考:http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/93162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档