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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:33:26+00:00 2026-05-16T22:33:26+00:00

I’m using a configuration within the global.asax.cs to register the components but it looks

  • 0

I’m using a configuration within the global.asax.cs to register the components but it looks the container hasn’t been initialized yet at the first http request (HomeController > Index action) and it gives me a “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.” error.

I can’t find a solution for this and is driving me mad!

Extract of my global.asax.cs:

protected void Application_Start()
{
    InitializeContainer();
    InitializeDatabase();
    RegisterRoutes(RouteTable.Routes);
}

private void InitializeContainer()
{
    _container = new WindsorContainer();

    ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));

    // Register context manager.
    _container.Register(
        Component.For<IContextManager>()
        .ImplementedBy<CoursesContextManager>()
        .LifeStyle.Singleton
        .Parameters(
    Parameter.ForKey("connectionString").Eq(ConfigurationManager.ConnectionStrings["CoursesConnection"].ConnectionString)   
        )
    );
    // Register specifc repository implementations (can we do this more generic?)
    _container.Register(
        Component.For<ICourseRepository>()
        .ImplementedBy<CourseRepository>()
        .LifeStyle.Singleton
    );

    [...other interfaces and controllers registered...]
}

Controller where the exception is thrown at first http request:

public class HomeController : Controller
{
    private ICourseRepository _courseRepository;

    public HomeController(ICourseRepository courseRepository)
    {
        _courseRepository = courseRepository;
    }

    public ActionResult Index()
    {
        var courses = _courseRepository.Find(); //here is where it fails
        return View(courses);
    }

}

Repository/interfaces:

Generic interface:

public interface IRepository<T>
{
    IQueryable<T> Find();
}

Generic repository:

public class MyRepository<T> : IRepository<T> where T : class
{
    private IContextManager _contextManager;
    private string _qualifiedEntitySetName;
    private string _keyName;

    protected ObjectContext CurrentObjectContext
    {
        get { return _contextManager.GetContext(); }
    }

    protected ObjectSet<T> ObjectSet
    {
        get { return CurrentObjectContext.CreateObjectSet<T>(); }
    }

    public MyRepository(IContextManager contextManager)
    {
        this._contextManager = contextManager;
        this._qualifiedEntitySetName = string.Format("{0}.{1}"
            , this.ObjectSet.EntitySet.EntityContainer.Name
            , this.ObjectSet.EntitySet.Name);
        this._keyName = this.ObjectSet.EntitySet.ElementType.KeyMembers.Single().Name;
    }

    public IQueryable<T> Find()
    {
        return ObjectSet;
    }
}

Interface course based on generic repository:

public interface ICourseRepository : IRepository<Course>
{
}
  • 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-16T22:33:27+00:00Added an answer on May 16, 2026 at 10:33 pm

    I found a way to handle with this at least momentarily. Because the problem happens on the first request, I’ve just added another action in my controller and redirect the index action to it. Probably not the best solution but can’t spend more time on this issue!

    public class HomeController : Controller
    {
        private ICourseRepository _courseRepository;
    
        public HomeController(ICourseRepository courseRepository)
        {
            _courseRepository = courseRepository;
        }
    
        public ActionResult Index() // Default action in the controller, first hit
        {
           return RedirectToAction("Home");
        }
    
        public ActionResult Home() //The repository is available here, no exception thrown
        {
            var courses = _courseRepository.Find(); //here is where it fails
            return View(courses);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

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.