在ASP.NET后台任务解决Autofac组件 [英] Resolving Autofac components in background Tasks in ASP.NET

查看:1581
本文介绍了在ASP.NET后台任务解决Autofac组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET中使用Autofac与ContainerDisposalModule一起,我怎么能支持消防和忘记,有需要解决的问题组件的依赖电话?我遇到的问题是,ASP.NET请求完成并部署前的任务就是跑请求的生命周期范围,因此需要在新线程得到解决的任何组件出现故障消息实例不能得到解决和嵌套寿命不能因为它已经被置于这个LifetimeScope创建。什么是支持火与ASP.NET中使用Autofac忘记调用的最好方法?我不希望延迟请求来执行它可以在后台线程完成某些任务。

Using Autofac in ASP.NET along with the ContainerDisposalModule, how can i support fire and forget calls that have component dependencies that need to be resolved? The problem I'm running into is that the ASP.NET request completes and disposes the lifetime scope of the request before the Task is ran, so any components that need to be resolved in the new thread fail with the message "Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed". What's the best way to support fire and forget call using Autofac in ASP.NET? I don't want to delay the request to perform certain tasks which can be done on background threads.

推荐答案

您需要创建一个新的生命周期范围是独立的请求生命周期范围。下面的博客文章显示如何做到这一点使用MVC但相同的概念可以应用于WebForms的一个例子。

You need to create a new lifetime scope that is independent of the request lifetime scope. The blog post below shows an example of how to do this using MVC but the same concept can be applied to WebForms.

http://about$c$c.net/2010/11/01/start-background-tasks-from-mvc-actions-using-autofac.html

如果您需要确保异步工作请求完成后,那么这是不是一个好办法肯定执行。在这种情况下,我会建议的要求,允许一个单独的进程把它捡起来,并执行工作中发布一条消息到一个队列。

If you need to ensure that the async work is definitely performed after the request is finished then this is not a good approach. In such cases I would recommend posting a message onto a queue during the request allowing a separate process to pick it up and perform the work.

这篇关于在ASP.NET后台任务解决Autofac组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆