Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:11:12+00:00 2026-06-05T12:11:12+00:00

I am building a four-layer system with MVC3 as the main front end, but

  • 0

I am building a four-layer system with MVC3 as the main front end, but containing long-running tasks that are run asynchronously by the service layer and report progress, which MVV will show using Ajax. I am using Autofac for dependency injection (DI) mainly because the interface and documentation is good and because its fast (see this excellent study on DI speed by Philip Mateescu).

My question is about how to set up Autofac to handle the two scopes of the injected items, i.e. the MVC3 dependencies have to be PerHttpRequest but the async task dependencies need to be InstancePerLifetimeScope.

Clearly the service layer will need to use a separate DI to resolve the long-running task’s dependencies. What is the best way of doing that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:11:14+00:00Added an answer on June 5, 2026 at 12:11 pm

    Update – June 2014

    I have updated this answer as the best practice on using AutoFac has moved on, plus changes in MVC. The change is that best practice in AutoFac is now to define any instances that need to last for the whole lifetime of the access, in the case of MVC the HttpRequest, by using the .InstancePerLifetimeScope() suffix. See below for an example:

    builder.RegisterType<MyDbContext>().As<IMyDbContext>().InstancePerLifetimeScope();
    

    Having done that you no longer need to specify the name of the new lifetime scope you are creating inside the task (see original answer, which has been updated, below).

    Here are a few other notes about tasking in MVC that you might find useful:

    • If you are using the new async/await then do don’t need a new lifetime scope. Aysnc/await keeps the current context and simply freeing up the thread to improve the performance of the web under load.
    • If you really do want to run something in the background then be warned – there are some issues. I suggest you read this helpful blog post from the expert on async, Stephen Cleary.
    • One really useful combination is using SignalR in combination with MVC to report progress and allow user cancel. This has worked well for me.

    Original post, but updated (note: you must register lifetime scope instances as shown above)

    I have found how to handle an async task with dependencies though the Google Autofac group. It turns out that you can access the MVC level container and then create a new lifetime scope of the resolve. There are a number of ways of doing it but this answer by Alex Meyer-Gleaves (who is an expert) provides the answer. Alex suggests the following code below for running a task that has a different scope.

    public void Run<T>(Action<T> action)
    {
      Task.Factory.StartNew(delegate
      {
        using (var container = AutofacDependencyResolver.Current
                               .ApplicationContainer.BeginLifetimeScope())
        {
            var service = container.Resolve<T>();
            action(service);
        }
      });
    }
    

    There is a link to a more detailed blog post here on the subject in Alex’s post which is also very useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a system that watches a queue and activates a set of tasks
I'm building a slideshow in jQuery that allows the user to see four images,
I am building a trading portfolio management system that is responsible for production, optimization,
Hey, I'm building a four tier software system (Presentation - JSP, Problem Domain, Application
I am building a UserControl that has four ListBoxes on it that I have
I am building a tool-bar like umbrella system to link four different sites together
I have a piece of code that I can run in four ways Debug
Im building a sample pad type instrument for a project, i have four channels
building a site using PHP and MySQL that needs to store a lot of
Building a website that has English & Japanese speaking users, with the Japanese users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.