1

我使用此代码在 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();
}

在谷歌搜索什么。有什么想法吗?

4

0 回答 0