WCF - 如何获取一些通道标识符?
我需要识别 WCF 服务中的通道。
一种方法是使用 Session.SessionID,但我似乎无法获得与会话一起使用的绑定,并且会话对于我想要实现的目标来说似乎太多了。我只是想写下通道的历史记录 - 正在调用哪些方法,并保留当前活动的“通道 ID”的哈希值。
我如何获得“频道 ID”之类的信息?我知道“频道 ID”并不明确存在,但是有哪些解决方法?
I need to identify a channel in my WCF service.
One way is to use Session.SessionID, but I can't seem to get the binding to work with sessions, and the session seems too much for what I'm trying to achieve. I'm just trying to write down history of a channel - what methods are being called, and to keep a hash of "channel ID's" that are currently active.
How can I get something like 'channel ID'? I know that the 'channel id' doesn't exist explicitly, but what are the workarounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于没有其他方法可以解决这个问题,所以我像这样“欺骗”它:
在客户端添加 MessageHeader:
在标头的“Name”属性中,我有要传递的标头的名称,并且我正在使用命名空间作为值持有者(因为我似乎无法获取标头的“值” - 它没有作为属性公开?!)。每次创建服务实例时,我都会在客户端执行此操作。
在服务中,我读到的标题如下:
这绝对是一个黑客,但我没有时间创建更优雅的东西,这使我能够以我可以控制它的方式维护“通道ID”......这是一个丑陋的解决方案我不喜欢它,所以每当有人找到更好的东西时,我都会很感激...
编辑:我尝试使用 Outgoing/IncomingMessageProperties 但这似乎不起作用 - 无处可寻在服务器端......我可能错过了一些东西......
Since nothing else does the trick, I 'tricked' it like this:
Add MessageHeader on the client side:
In the "Name" property of the header I have the name of the header I want to pass, and I'm using the Namespace as the value-holder (since I can't seem to get to that 'value' of the header - it's not exposed as property?!). I do this on client side each time I create a service instance.
On service I read the header like:
It's definitely a hack, but I'm out of time to create something more elegant, and this allows me to maintain 'channel id' the way I can control it... it's an ugly solution and I don't like it, so whenever someone finds something better I'd appreciate it...
edit: I tried using Outgoing/IncomingMessageProperties but that doesn't seem to work - it's nowhere to be found on server side... I'm probably missing something...
您是否尝试过
string sessionID = OperationContext.Current.SessionId;
?Did you try
string sessionID = OperationContext.Current.SessionId;
?听起来
OperationContext.Current.Channel.GetHashCode()
可能适合您的目的。It sounds like
OperationContext.Current.Channel.GetHashCode()
might suit your purpose.