1

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

4

3 回答 3

2

与您的问题没有真正的关系,但在 asp.net 应用程序中执行后台任务很糟糕,我从经验中说。

在 ASP.NET 中实现重复后台任务的危险

于 2012-04-05T16:28:24.777 回答
1

由于您的对象需要跨进程和内存空间,因此我将创建一个 WCF 服务,将您的工厂作为单例实例托管。这样,MVC 应用程序可以调用它,WF 应用程序可以调用它。

于 2012-04-05T16:29:58.003 回答
0

我知道这很旧,但我认为这也会有所帮助。

在拦截器上检查时,HttpContext.Current 为空

在运行工作流服务 (.xamlx) 时,您希望将 OperationContext 与您的实体对象一起使用。

于 2012-08-13T20:39:47.577 回答