0

ASP.NET website recommends implementing Disposable on my repository which affects my datacontext.

Is it sufficient for me to initialize the datacontext here, and casually let ASP.NET dispose of the resources?

Under what conditions will it not get called? I am debugging frequently (ending the debug session mid page) and noticed that I needed to reboot to reclaim some memory.

makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

1

MVC will not dispose any IDisposable instances you create. You are in-charge of disposing them. You can either over ride Dispose method of the controller.

public class MyController : Controller
{

      protected override void Dispose(bool disposing) 
      {
          //dispose them here
      }
}

Or use a Dependency Injection/IoC framework to control the lifetime of the IDisposable instances. Many DI/IoC frameworks will automatically call the Dispose method if the framework created those instances.

Eranga
  • 32,181
  • 5
  • 97
  • 96