获取WCF中回调通道的IP地址

我在双工通道上有一个WCF服务,带有回调契约。 当客户端在服务上调用SubscribeMe()方法时,服务通过将OperationContext.Current.GetCallbackChannel()的结果存储在列表中来跟踪客户端。 该服务将定期ping这些回调通道以跟踪其有效性,并使那些关闭或超时的回叫到期。

我的问题是:当远程主机没有主动向我的服务发出请求时,如何在调用MyCallbackContract.Ping()时,如何获取有关远程主机的信息?

我尝试将回调通道对象转换为IContextChannel ,并访问IContextChannel::RemoteAddress属性,但此属性包含某种与回调通道上的实际远程主机无关的命名空间URI。

没有测试(需要一段时间来设置所有配置!),但我认为你正在寻找这样的东西:

 OperationContext context = OperationContext.Current; MessageProperties messageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; var address = endpointProperty.Address;