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

Joh*_*ove 4 .net c# wcf

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

我的问题是:当远程主机没有主动向我的服务请求时,如何获取有关远程主机的信息,例如当我打电话时MyCallbackContract.Ping()

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

Nat*_*han 5

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

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

var address = endpointProperty.Address;
Run Code Online (Sandbox Code Playgroud)