Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing correlation token to WCF service?

Having a lot of similar calls, maybe in parallel, to some web services, I'm wondering what is the best way to implement a correlation token mechanism.

The idea is to have, from the client to the server, the ability to log information for a single request with an identifier that helps, later, to get a log for a specific request.

I'd like to avoid adding a parameter on each operation, because I think this is plumbing, not business.

PS: I'm controlling both client and server side in my solution. I can change whatever is needed. MY services are running with .Net V4, and I'm using log4net to create the logs. I've wrapped log4net is some utility methods I can change if required.

like image 911
Steve B Avatar asked Sep 26 '11 09:09

Steve B


1 Answers

So do you want only information about request and its response? If you use message version with WS-Addressing you should have it automatically because each messages will contain its autogenerated ID (guid) and each response will contain ID of the request as well. You can access these headers through OperationContext:

Server side:

UniqueId id = OperationContext.Current.IncomingMessageHeaders.MessageId;

Client side:

using (var scope = new OperationContextScope())
{
    // Do call to server and after that access headers

    OperationContext context = OperationContext.Current;
    UniqueId responseId = context.IncomingMessageHeaders.MessageId;
    UniqueId requestId = context.IncomingMessageHeaders.RelatesTo;
}

To use WS-Addressing you must use SOAP service with WsHttpBinding or CustomBinding using correct MessageVersion in TextMessageEncodingBindingElement.

If you want to have correlation for all requests from the same client you need WCF session and use session Id (OperationContext.Current.SessionId).

like image 182
Ladislav Mrnka Avatar answered Sep 23 '22 08:09

Ladislav Mrnka