我使用此代码在WCF服务中获取客户端IP:
public string GetClientIP()
{
var context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
var endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
return ip;
}
当你尝试运行时 - 抛出异常" System.ServiceModel.Channels.MessageProperties对象已被处理"。
此代码在一个WCF服务中不起作用。它的web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
<identity impersonate="false" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<directoryBrowse enabled="true" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
是休息WCF服务。呼叫者服务方法签名如下:
[WebInvoke(Method = "POST", UriTemplate = "{someParameter}/MyMethod")]
public string MyMethod(string someParameter, Stream stream)
{
var ip = GetClientIP();
}
在Google中搜索没有任何内容。有什么想法吗?