IoC and dataContext disposing in asp.net mvc 2 application

后端 未结 3 681
北荒
北荒 2021-01-07 00:34

I have the Global.asax like the code below:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(Rou         


        
3条回答
  •  心在旅途
    2021-01-07 01:09

    A pattern that is sometimes used to dispose db connections is to call Dispose from the finaliser.

    public class db : IDisposable {
       //called by the garbage collector 
       ~db() {
         //Call dispose to make sure the resources are cleaned up
         Dispose(false);
       }
    
       //IDisposable implementation
       public void Dispose() {
         Dispose(true);
       }
       //subclasses of db can override Dispose(bool) and clean up their own fields
       protected virtual void Dispose (bool disposing) {
         if (disposing) {
           //Supress finalization as all resources are released by this method
           //Calling Dispose on IDisposable members should be done here
           GC.SupressFinalize();
         }
         //Clean up unmanaged resources
         //Do not call other objects as they might be already collected if called from the finalizer
       }
    }
    

提交回复
热议问题