I have an .NET 4.0 ASP.NET MVC application, which also hosts a Workflow Foundation 4.0. From within this workflow, some custom workflow activity will execute code to do some database updates using Linq to SQL. The code consists of calling a method, which in turn call some other methods etc... I also have a business layer which has a data access factory, providing access to all of my data access objects containing the methods for database operations.
Now suppose I my WF activity calls method A , which in turn calls method B in another class, which calls method C end D in yet another class. In each of these methods I want to retrieve the same instance of my data accesss factory, so that all database operations are excuted on the same database transaction. How would I design the singleton pattern for my data access factory? Note that methods A, B, C and D also can be called from Asp.Net MVC controllers.
When methods A, B,C and D are called from asp.net mvc controllers, its easy, I can use the HttpContext to store my data access factory singleton, so that within one http request I also get the same instance of my data access factory.
But when these methods are called from the Workflow activity, there is no HttpContext of course. I tried thread static variable, but in web applications you're not sure, method A, B, C en D will be called on the same thread. I also tried CallContext, but I experienced, I'm not always retrieving the same instance, so apparently CallContext is not the solution either.
Basically, the problem can be summarized as 'getting the same instance of an object in a background process running in an asp.net application' (it doesn't matter whether this background task is kicked of by the WF activity or another way of background tasks e.g. using Task<T>)