How can I address Entity Framework context disposal issues within IIS Application Pool on an Azure VM hosting an MVC application?

Enzo Gomes 0 Reputation points
2024-03-13T21:39:56.05+00:00

I am encountering an issue with the automatic disposal of the Entity Framework context within the IIS App Pool on my Azure Virtual Machine that is hosting an MVC application. The problem manifests as errors occurring on nearly all pages after a certain period of usage, which ceases after manually recycling the application pool. Previously, when the application was hosted on an EMBRATEL cloud server, this problem did not occur.

The application uses the following code to instantiate the database context:

private static ApolloDBEntities _instance;

public static ApolloDBEntities Instance
{
    get
    {
        if (_instance == null)
        {
            _instance = new ApolloDBEntities();
        }
        return _instance;
    }
}

However, it appears that at some point, this LINQ instance is being disposed unexpectedly. I have already scheduled automatic recycling of the application pool every day at 1 AM. Despite this, the issue persists.

I am seeking guidance on identifying the root cause of this problem and exploring potential solutions. Could it be related to IIS configuration, the garbage collector, or any other factors specific to the Azure environment? Any insights or recommendations on how to troubleshoot and mitigate this issue would be greatly appreciated.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,150 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,531 Reputation points
    2024-03-14T01:43:49.32+00:00

    you app probably fails when multiple users use the site, or if one page does not read all the results of a query.

    While a dbcontext can be shared between threads, it does not support two concurrent queries. A second query can not be started until the first query has returned all its results. If you are using a singleton dbcontext, then you need to add locking around the queries. The lock needs to be taken before the query and released after all the entities have been read. You can use a .ToList() to read all the rows.

    Be sure tracking is disabled, or you will get memory issues. You probably will anyway as dbcontext was designed to be disposed after each transaction was completed.

    0 comments No comments

  2. SurferOnWww 1,911 Reputation points
    2024-03-14T02:24:04.77+00:00

    I cannot imagine the causes other than the IIS application pool recycle or use of static (i.e., singleton).

    It is not appropriate to use static in the ASP.NET Web app unless it is absolutely necessary. The context class is probably not the case. The ASP.NET is multi-thread app where different thread is used to process the requests from a number of users. It means that multiple threads access singleton at the same time. You said "which ceases after manually recycling the application pool." Therefore, I guess that the use of static (i.e., singleton) is doubtful.

    0 comments No comments