WCF - 如何获取一些通道标识符?

发布于 2024-10-03 08:47:47 字数 199 浏览 2 评论 0原文

我需要识别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

潜移默化 2024-10-10 08:47:47

由于没有其他方法可以解决这个问题,所以我像这样“欺骗”它:

在客户端添加 MessageHeader:

using (OperationContextScope scope = new OperationContextScope(cli.InnerChannel))
{
   OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("MyHeader", Guid.NewGuid().ToString(), ""));

   string ret = cli.GetData(1);
}

在标头的“Name”属性中,我有要传递的标头的名称,并且我正在使用命名空间作为值持有者(因为我似乎无法获取标头的“值” - 它没有作为属性公开?!)。每次创建服务实例时,我都会在客户端执行此操作。

在服务中,我读到的标题如下:

var head = OperationContext.Current.IncomingMessageHeaders.FirstOrDefault(h => h.Name == "MyHeader");
string channelId = head.Namespace;

这绝对是一个黑客,但我没有时间创建更优雅的东西,这使我能够以我可以控制它的方式维护“通道ID”......这是一个丑陋的解决方案我不喜欢它,所以每当有人找到更好的东西时,我都会很感激...

编辑:我尝试使用 Outgoing/IncomingMessageProperties 但这似乎不起作用 - 无处可寻在服务器端......我可能错过了一些东西......

Since nothing else does the trick, I 'tricked' it like this:

Add MessageHeader on the client side:

using (OperationContextScope scope = new OperationContextScope(cli.InnerChannel))
{
   OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("MyHeader", Guid.NewGuid().ToString(), ""));

   string ret = cli.GetData(1);
}

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:

var head = OperationContext.Current.IncomingMessageHeaders.FirstOrDefault(h => h.Name == "MyHeader");
string channelId = head.Namespace;

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...

我要还你自由 2024-10-10 08:47:47

您是否尝试过string sessionID = OperationContext.Current.SessionId;

Did you try string sessionID = OperationContext.Current.SessionId;?

神也荒唐 2024-10-10 08:47:47

听起来 OperationContext.Current.Channel.GetHashCode() 可能适合您的目的。

It sounds like OperationContext.Current.Channel.GetHashCode() might suit your purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文