在 ASP.NET 中的后台任务中解析 A​​utofac 组件 [英] Resolving Autofac components in background Tasks in ASP.NET

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

问题描述

在 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://aboutcode.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 中的后台任务中解析 A​​utofac 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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