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

  • Home
  • SEARCH
  • 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 8384225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:21:15+00:00 2026-06-09T17:21:15+00:00

I try to make custom error handling (http://perspectivespace.com/100497697) with error module(Application_Error), HttpModuleMagic.MVC3, Ninject.MVC3, EF

  • 0

I try to make custom error handling (http://perspectivespace.com/100497697) with error module(Application_Error), HttpModuleMagic.MVC3, Ninject.MVC3, EF 4.1. Unfortunately when I want to log the error into the database it gives this error message:
“The operation cannot be completed because the DbContext has been disposed.”

Could somebody help me, please?

public class ErrorHandlerHttpModule : IHttpModule
{
    private const string ErrorControllerRouteName = "Error";
    private IErrorRepository errorRepo;
    private IUserRepository userRepo;

  //  private IUserRepository UserRepository;

    public ErrorHandlerHttpModule(IErrorRepository er, IUserRepository ur)
    {
        errorRepo = er;
        userRepo = ur;
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += Application_Error;
    }

    private void Application_Error(object sender, EventArgs e)
    {
        // Get the last error.

        var exception = HttpContext.Current.Server.GetLastError();
    ...

    error.User = userRepo.GetUserByName(name);

    ...

In the NinjectWebCommon.cs:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IErrorRepository>().To<ErrorRepository>().InRequestScope();
        kernel.Bind<IUserRepository>().To<UserRepository>().InRequestScope();

        kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();

        kernel.Bind<IDisposable>().To<Disposable>().InRequestScope();
        kernel.Bind<IHttpModule>().To<ErrorHandlerHttpModule>();
    } 

… and the UserRepository class:

public class UserRepository : RepositoryBase<User>, IUserRepository
{
    public UserRepository(IDatabaseFactory databaseFactory)
    : base(databaseFactory)
    {
    } 

    public User GetUserByName(string name)
    {
        User us = null;
        try
        {
            us = dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User;
        }
        catch (Exception ex)
        {

        }
        return us;
    }
}

The RepositoryBase:

public abstract class RepositoryBase<T> where T : class
{
    protected TestContext dataContext;
    private readonly IDbSet<T> dbset;
    protected RepositoryBase(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected TestContext DataContext
    {
        get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
    }
    ...

and DatabaseFactory class:

public class DatabaseFactory : Disposable, IDatabaseFactory
{
    private TestContext dataContext;

    public TestContext Get()
    {
        return dataContext ?? (dataContext = new TestContext());
    }

    protected override void DisposeCore()
    {
        if (dataContext != null)
            dataContext.Dispose();
    }
}

This line dives the error:

dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User;

I try to follow this article:
http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx

Thank you for your answer.

  • 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-09T17:21:16+00:00Added an answer on June 9, 2026 at 5:21 pm
    protected override void DisposeCore()
    {
       if (dataContext != null)
       {
          dataContext.Dispose();
          dataContext = null;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed the tutorial at http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module step by step to make my module install
So I created a custom int.tryparse method in c# to try to make things
I try to make an ActiveX by C# with COM-visible. It is a Windows
My understanding for the right way to make a custom Error class in JavaScript
I am trying to make custom error pages for 404, 403, 5xx errors. I
when I try to make use of custom environments in grails by setting the
I try to make my ListBox connected to ObservaleCollection be more efficient so for
I try to make an imageview clickable. Actually it is clickable, so that I
I try to make a table header fixed when scoll down on pages. The
I try to make a apps that need barcode scanner, i already can get

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.