Autofac not working with background tasks

Autofac not working with background tasks

I recently implemented something similar using help from this answer and this answer. You need to create a new lifetime scope - it sounds like your doing this in a web application, so you need to create the scope via the per-request tag (example below).

Another (non-StackOverflow) answer provides similar advice.

public Task Run<T>(Action<T> action)
{
    Task.Factory.StartNew(() =>
    {
        using (var lifetimeScope = _container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag))
        {
            var service = lifetimeScope.Resolve<T>();
            action(service);
        }
    });
    return Task.FromResult(0);
}

 

评论:

 
 

Resolving Autofac components in background Tasks in ASP.NET

回答1

Answer posted by Alex adapted to current Autofac and MVC versions:

  • Use InstancePerRequest for a database context
  • Add ILifetimeScope as dependency to get to the container
  • SingleInstance ensures it's the root lifetime scope
  • Use HostingEnvironment.QueueBackgroundWorkItem to reliably run something in the background
  • Use MatchingScopeLifetimeTags.RequestLifetimeScopeTag to avoid having to know the tagname autofac uses for PerRequest lifetimes

https://groups.google.com/forum/#!topic/autofac/gJYDDls981A https://groups.google.com/forum/#!topic/autofac/yGQWjVbPYGM

Gist: 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);
            }
        });
    }
}

Controller

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;
  }
}

 

回答2

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.

 

 

Configure Autofac Container for background thread

There are several ways of how you can do that:

  1. The best one in my opinion. You can register the repository as InstancePerLifetimeScope as you said. It works with HttpRequests and LifetimeScopes equally well.

    builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>))
        .InstancePerLifetimeScope();
    
  2. Your registration for HttpRequest may differ from registration for LifetimeScope, then you can have two separate registrations:

    builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>))
        .WithParameter(...)
        .InstancePerHttpRequest(); // will be resolved per HttpRequest
    
    builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>))
        .InstancePerLifetimeScope(); // will be resolved per LifetimeScope
    
  3. You can explicitly create "HttpRequest" scope using its tag. Exposed through MatchingScopeLifetimeTags.RequestLifetimeScopeTag property in new versions.

    using (var httpRequestScope = container.BeginLifetimeScope("httpRequest")) // or "AutofacWebRequest" for MVC4/5 integrations
    {
        var repository = httpRequestScope.Resolve<IRepository<Entity>>();
    }
    

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(134)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-10-20 启动vmware中的虚拟机的时候,提示Failed to lock the file
2016-10-20 筛选或者删除数据表中重复的数据
点击右上角即可分享
微信分享提示