我有一个用 C# 编写的 Web 服务,我正在尝试在 Mono 上运行它(Mono JIT 编译器版本 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.2))
在 Web 服务中有一个帮助函数来确定调用者的 IP 地址:
public static System.Net.IPAddress GetCallerIP()
{
System.ServiceModel.OperationContext context = System.ServiceModel.OperationContext.Current;
System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
System.ServiceModel.Channels.RemoteEndpointMessageProperty callerEndpoint =
prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
System.Net.IPAddress ip= System.Net.IPAddress.Parse(callerEndpoint.Address);
return ip;
}
在 Windows 上,prop 中有 6 个属性:Via, httpRequest, System.ServiceModel.Channels.RemoteEndpointMessageProperty, UriTemplateMatchResults, UriMatched, HttpOperationName
在 Ubuntu 上,我只看到 3 个属性:Via, httpRequest, Urimatched
因此,我的辅助函数在 Ubuntu 上运行时无法确定调用者的 IP 地址。怎么了?附加信息
Mono 从 2.10.3 开始支持 RemoteEndpointMessageProperty,所以我很确定我的 Mono 版本支持它。
关于 RemoteEndpointMessageProperty,MSDN在备注部分说以下内容:
The property is added to each incoming message to a Windows Communication Foundation (WCF) service through both the HTTP and TCP transports.
The property is not present on messages received through either a named pipes or MSMQ transport.
我想知道这是否与我的问题有关?但是我该如何验证呢?
最佳答案
mono 是开源的,因此您可以随时查看代码和提交历史。此外,您甚至可以调试运行时本身,例如在 monodevelop
中。通过取消选中“仅调试项目代码;不要进入框架代码”选项。
快速浏览一下,对于 HTTP POST 消息,自提交 fd8ae35d 以来正在设置相关属性.虽然这早于 2.10.8.1
的发布。 ,不幸的是,该版本来自不同的分支,因此不包括此更改。包含它的第一个版本是2.11.0
.
2.x 系列现在已经很老了,无论如何升级是个好主意。
关于c# - 单声道/WCF : Cannot determine caller IP on Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919145/