【问题标题】:How to get client IP in WCF service in Linux?如何在 Linux 的 WCF 服务中获取客户端 IP?
【发布时间】:2017-09-30 10:56:53
【问题描述】:

我有一个 WCF 服务,它在 Debian 的 Mono 下运行。
我的目标是在服务中获取客户端 IP 地址。
我已经阅读了大量类似的主题,这些主题表明主要的解决方案是使用RemoteEndpointMessageProperty

OperationContext oOperationContext = OperationContext.Current;
MessageProperties oMessageProperties = oOperationContext.IncomingMessageProperties;

var prop = oMessageProperties[RemoteEndpointMessageProperty.Name];
RemoteEndpointMessageProperty oRemoteEndpointMessageProperty = prop as RemoteEndpointMessageProperty;

string szAddress = oRemoteEndpointMessageProperty.Address;
int nPort = oRemoteEndpointMessageProperty.Port;

我做了与上图相同的操作。
但问题是oMessageProperties 不包含键RemoteEndpointMessageProperty.Name,它只有值System.ServiceModel.Channels.BinaryMessageEncoder 的“编码器”。

有人知道为什么会发生这种情况以及如何解决这个问题吗?

我在 Mono 3.2.8 下使用 NetTcpBinding

【问题讨论】:

    标签: c# wcf mono


    【解决方案1】:

    请试试这个:

    var iPAddress = HttpContext.Current.Request.UserHostAddress;
    

    【讨论】:

    • HttpContext.Current 在我的服务中是 null。我添加了ASP.NET Compatibility Mode,但也没有用
    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 2013-07-23
    • 2014-01-13
    • 2017-12-11
    • 2011-07-15
    • 1970-01-01
    • 2010-09-10
    • 2010-11-16
    相关资源
    最近更新 更多