获取WCF的客户端IP地址,post操作
我正试图通过此链接确定客户端的IP地址: 在.NET3.0中,没有可靠的方法获取 连接到WCF服务的客户端。在.NET3.5中,创建了一个新属性 引入了名为RemoteEndpointMessageProperty的属性。此属性提供 您可以输入客户端连接进入的IP地址和端口 服务于。获取这些信息是非常直接的。 只需从当前的IncomingMessageProperties中提取它 RemoteEndpointMessageProperty.Name和access指定的OperationContext 地址和端口属性 注意事项:获取WCF的客户端IP地址,post操作,wcf,client,ip-address,webinvoke,Wcf,Client,Ip Address,Webinvoke,我正试图通过此链接确定客户端的IP地址: 在.NET3.0中,没有可靠的方法获取 连接到WCF服务的客户端。在.NET3.5中,创建了一个新属性 引入了名为RemoteEndpointMessageProperty的属性。此属性提供 您可以输入客户端连接进入的IP地址和端口 服务于。获取这些信息是非常直接的。 只需从当前的IncomingMessageProperties中提取它 RemoteEndpointMessageProperty.Name和access指定的OperationConte
[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();
// Code for getting IP
private string getClientIP()
{
//WebOperationContext webContext = WebOperationContext.Current;
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return endpointProperty.Address;
}
public Stream hello_get()
{
string ip = getClientIP();
...
}
public Stream hello_post()
{
string ip = getClientIP();
...
}
您是否尝试过使用HttpContext?并非在所有WCF模式下都可用,但这可能会根据您的环境而起作用:
if (HttpContext.Current != null)
{
Trace.WriteLine(
"Who's calling? IP address: '{0}', Name: '{1}', User Agent: '{2}', URL: '{3}'.",
HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UserHostName,
HttpContext.Current.Request.UserAgent, HttpContext.Current.Request.Url);
}
你可能想展示你尝试过但没有成功的东西。我同意詹姆斯的观点。我不明白为什么这对WebInvoke不起作用。你能提供一些代码(和配置)让我们知道你是如何处理的吗?谢谢。我已经添加了一些如上所示的代码。如果这不起作用,您应该创建一个简单的示例,复制这个问题,并将其发布到Microsoft Connect。对我来说,这是一个bug,因为没有理由认为GET应该工作而POST不工作。链接已经死了,所以很难引用代码现在应该做什么。
if (HttpContext.Current != null)
{
Trace.WriteLine(
"Who's calling? IP address: '{0}', Name: '{1}', User Agent: '{2}', URL: '{3}'.",
HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UserHostName,
HttpContext.Current.Request.UserAgent, HttpContext.Current.Request.Url);
}