문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top