views:

155

answers:

2
+1  Q: 

Set user context

My businesslayer\data access library is being used by both a web application and a wcf service. I need to set the current user context on each request for both of these application which would contain userid, Ip, application type etc etc.

The library is obviously not aware what type of application is using it.

Now i cant keep this information in a static variable since subsequent requests would override older values. And I dont want to pass this variable into each and every class where it might be needed like the logging component.

This might be a dumb question but is there a way to create a thread level static variable like the OperationContext.Current or the HttpContext.Current (Something that will be gone at the end of the current request)

Or some other way that I might be able to achieve this functionality?

+2  A: 

The default approach to this is to use Thread.CurrentPrincipal. It should meet all of your requirements.

In general, IPrincipal is the standard basis for modeling user context in .NET. For example, HttpContext.User is an IPrincipal.

Mark Seemann
A: 

Thanks for all the help, but I used the solution proposed in this link: http://elegantcode.com/2009/01/17/abstracting-request-state/

This way I dont have to worry about thread static variables retaining their values at the end of each request cycle because of thread pooling and still manage to retain certain objects that are accessible anywhere within a thread which includes user credentials

shake
Now /that/ is interesting; thanks. I've removed my incorrect answer.
Jeremy McGee