在ASP.NET中使用Autofac和ContainerDisposalModule一樣,我如何支持需要解決組件依賴關係的fire和forget調用?我遇到的問題是ASP.NET請求在任務運行之前完成並部署了請求的生命週期範圍,因此需要在新線程中解析的任何組件都會失敗,並顯示「實例無法解析並且從此LifetimeScope中不能創建嵌套壽命,因爲它已經被處置「。在ASP.NET中使用Autofac支持火和忘記呼叫的最佳方式是什麼?我不想延遲執行某些可以在後臺線程上完成的任務的請求。在後臺解析Autofac組件在ASP.NET中的任務
2
A
回答
2
您需要創建一個獨立於請求生存期範圍的新生命週期範圍。下面的博客文章展示了一個如何使用MVC來實現這個功能的例子,但是可以將相同的概念應用於WebForms。
http://aboutcode.net/2010/11/01/start-background-tasks-from-mvc-actions-using-autofac.html
如果您需要確保異步工作請求完成後,那麼這是不是一個好方法絕對執行。在這種情況下,我會建議在請求期間將消息發佈到隊列中,以允許單獨的進程提取並執行工作。
2
答案張貼由Alex適應當前Autofac和MVC版本:
- 使用
InstancePerRequest
的數據庫上下文 - 添加
ILifetimeScope
的依賴才能到容器 SingleInstance
確保它的根壽命範圍- 使用
HostingEnvironment.QueueBackgroundWorkItem
至可靠在後臺運行東西 - 使用
MatchingScopeLifetimeTags.RequestLifetimeScopeTag
以避免必須知道的標記名autofac用來PerRequest使用壽命期
https://groups.google.com/forum/#!topic/autofac/gJYDDls981A https://groups.google.com/forum/#!topic/autofac/yGQWjVbPYGM
要點:https://gist.github.com/janv8000/35e6250c8efc00288d21
的Global.asax.cs:
protected void Application_Start() {
//Other registrations
builder.RegisterType<ListingService>();
builder.RegisterType<WebsiteContext>().As<IWebsiteContext>().InstancePerRequest(); //WebsiteContext is a EF DbContext
builder.RegisterType<AsyncRunner>().As<IAsyncRunner>().SingleInstance();
}
AsyncRunner .cs
public interface IAsyncRunner
{
void Run<T>(Action<T> action);
}
public class AsyncRunner : IAsyncRunner
{
public ILifetimeScope LifetimeScope { get; set; }
public AsyncRunner(ILifetimeScope lifetimeScope)
{
Guard.NotNull(() => lifetimeScope, lifetimeScope);
LifetimeScope = lifetimeScope;
}
public void Run<T>(Action<T> action)
{
HostingEnvironment.QueueBackgroundWorkItem(ct =>
{
// Create a nested container which will use the same dependency
// registrations as set for HTTP request scopes.
using (var container = LifetimeScope.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag))
{
var service = container.Resolve<T>();
action(service);
}
});
}
}
控制器
public Controller(IAsyncRunner asyncRunner)
{
Guard.NotNull(() => asyncRunner, asyncRunner);
AsyncRunner = asyncRunner;
}
public ActionResult Index()
{
//Snip
AsyncRunner.Run<ListingService>(listingService => listingService.RenderListing(listingGenerationArguments, Thread.CurrentThread.CurrentCulture));
//Snip
}
ListingService
public class ListingService : IListingService
{
public ListingService(IWebsiteContext context)
{
Guard.NotNull(() => context, context);
Context = context;
}
}
相關問題
- 1. ASP.NET中的後臺任務
- 2. 在asp.net中執行後臺任務MVC4
- 3. ASP.NET/Debug中的後臺任務
- 4. Autofac - 動態解析組件參數
- 5. System.UnauthorizedAccessException的在後臺任務
- 6. 後臺任務/任務/進程,在asp.net MVC 3
- 7. 圍繞後臺線程或任務創建Autofac服務
- 8. Swift在後臺解析XML
- 9. AFNetworking:在後臺解析xml
- 10. Autofac - 解決在單獨的組件
- 11. 在後臺執行任務
- 12. asp.net mvc autofac註冊和解析器
- 13. 如何在ASP.NET中運行後臺任務
- 14. 如何在ASP.NET Web API中排列後臺任務
- 15. 後臺任務
- 16. WinRT中的後臺任務
- 17. 後臺任務中的net_http_client_execution_error
- 18. UWP中的後臺任務
- 19. Powershell中的後臺任務?
- 20. 開始後臺任務ASP.NET MVC 4.5
- 21. 後臺任務不在UWP中運行
- 22. 在後臺任務中安排通知
- 23. 在UWP中觸發後臺任務
- 24. 如何在struts2中做後臺任務?
- 25. 在Rails中旋轉後臺任務
- 26. AutoFac解析參數
- 27. 送在後臺工作/計劃任務的電子郵件在asp.net MVC 3
- 28. 使用ASP.Net核心中間件啓動後臺任務
- 29. 後臺任務中的Sqlite(Windows運行時組件項目)
- 30. 在不註冊服務類型的情況下解析autofac中的服務