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 4078934
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:44:11+00:00 2026-05-20T17:44:11+00:00

I’m using ASP.NET MVC with Castle Windsor as my IoC container with the component

  • 0

I’m using ASP.NET MVC with Castle Windsor as my IoC container with the component lifestyle set to PerWebRequest. My repository (which is the dependency that’s injected) creates an instance of Entity Framework’s ObjectContext in the constructor and I store that in a private instance variable. My repository implements IDisposable and inside my Dispose method, I dispose the ObjectContext. I think all of this is pretty standard and here’s a simplified illustration:

Repository:

 public class Repository : IRepository {

    private MyContext _dc; // MyContext inherits from ObjectContext

    public Repository() {
        _dc = new MyContext();
    }

    public void Dispose() {;
        _dc.Dispose();
    }
}

To ensure that there’s no memory leak and that my Repository’s Dispose() is called, I override DefaultControllerFactory’s ReleaseController method to release Windsor’s container:

public class WindsorControllerFactory : DefaultControllerFactory {
        IWindsorContainer _container;

        public WindsorControllerFactory(IWindsorContainer container) {
            _container = container;
            // Do stuff to register all controller types
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
            // Do stuff to resolve dependency
        }

        public override void ReleaseController(IController controller) {
            // by releasing the container, Windsor will call my Dispose() method on my repository
            _container.Release(controller);
            base.ReleaseController(controller);
        }
    }

I think all of this is pretty standard. However, I’d like to spin off a parallel thread, and inside that parallel thread utilize the IRepository dependency. My problem is that my repository will have already been disposed by the time I use it:

public class HomeController : Controller {

    IRepository _repository;

    public HomeController(IRepository repository) {
        _repository = repository;
    }

    public ActionResult Index() {
        var c = _repository.GetCompany(34);

        new Task(() => {
            System.Threading.Thread.Sleep(2000); // simulate long running task
            // will throw an error because my repository (and therefore, ObjectContext) will have been disposed.
            var c2 = _repository.GetCompany(35);
        }).Start();

        return Content(c.Name, "text/plain");
    }
}

How do other people solve this problem? How do you pass your dependencies to a parallel thread?

Thanks in advance.

  • 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-05-20T17:44:11+00:00Added an answer on May 20, 2026 at 5:44 pm

    Create a new Repository instance. ObjectContexts are inexpensive to construct.

    OR, you could attach the responsibility of disposing the Repository to the long running thread. I messed around with it, I think these alterations to your code will solve your problem:

    In your WindsorControllerFactory

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            IController controller;
            // Do stuff to resolve dependency
            if(controller is LongTaskController)
            {
                ((LongTaskController) controller).CompleteLongTask += (sender, args) => _container.Release(controller);
            }
        }
    
        public override void ReleaseController(IController controller)
        {
            // by releasing the container, Windsor will call my Dispose() method on my repository
            if(!(controller is LongTaskController && ((LongTaskController)controller).HasLongTask)
            {
                _container.Release(controller);
            }
            base.ReleaseController(controller);
        }
    

    In HomeController

    public class HomeController : LongTaskController
    {
        private readonly IRepository _repository;
        public HomeController(IRepository repository)
        {
            _repository = repository;
        }
        public ActionResult Index()
        {
            var c = _repository.GetCompany(34);
            DoLongTask(() =>
            {
                Thread.Sleep(200);
                var c2 = _repository.GetCompany(35);
            });
            return Content(c.Name, "text/plain");
        }
    }
    

    And the new base controller

    public abstract class LongTaskController: Controller,IHasLongTask
    {
        private bool _hasLongTask;
        public bool HasLongTask { get { return _hasLongTask; } }
        public event EventHandler CompleteLongTask;
        void IHasLongTask.DoLongTask(Action action) { DoLongTask(action); }
        protected void DoLongTask(Action action)
        {
            _hasLongTask = true;
            if (CompleteLongTask == null)
            {
                throw new NullReferenceException("Controller.CompleteLongTask cannot be null when Controller does a long running task.");
                action += () => CompleteLongTask(this, EventArgs.Empty);
            }
            new Task(action).Start();
        }
    }
    public interface IHasLongTask
    {
        bool HasLongTask { get; }
        void DoLongTask(Action action);
        event EventHandler CompleteLongTask;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.