Reputation: 8512
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?
Upvotes: 2
Views: 3360
Reputation: 8512
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...
Upvotes: 2
Reputation: 12135
It sounds like OperationContext.Current.Channel.GetHashCode()
might suit your purpose.
Upvotes: 0
Reputation: 16216
Did you try string sessionID = OperationContext.Current.SessionId;
?
Upvotes: 0