5

I am coding an MVC 5 internet application with a web api 2 web service. Do I need a dispose method for the DbContext class in a web service? It is not there as default.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Simon
  • 7,991
  • 21
  • 83
  • 163
  • What would you do if you _did_ need such a method? Were you planning to rewrite the DbContext class? – John Saunders Jan 12 '15 at 05:12
  • Hi , Check this http://stackoverflow.com/questions/20836837/why-does-no-one-disposes-dbcontext-after-webapi-controller-operation, It might be useful for you. – Morteza Azizi Jan 12 '15 at 05:16
  • @JohnSaunders: My DbContext class already has the dispose method, I am wondering if I need to add the dispose method to the web api controller. – Simon Jan 12 '15 at 05:27
  • If your web api controller class holds an instance of the DbContext class (or any other IDisposable class), then yes, you need the Web API controller to implement IDisposable. If it does not hold any instances of an IDisposable-implementing class, then you don't need to implement IDisposable. – John Saunders Jan 12 '15 at 05:56

2 Answers2

23

Actually, System.Web.Http.ApiController already implements IDisposable:

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the  project root for license information.
// ...
public abstract class ApiController : IHttpController, IDisposable
{
// ...
    #region IDisposable

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
    }

    #endregion IDisposable
}

So, if your controller holds a DbContext, do the following:

public class ValuesController : ApiController
{
    private Model1Container _model1 = new Model1Container();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_model1 != null)
            {
                _model1.Dispose();
            }
        }
        base.Dispose(disposing);
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • This was exactly what I was looking for. I already had an implemented code similar as yours in MCV, didn't know the same was for Web API – benjamingranados Nov 25 '21 at 15:54
3

In Web Api 2, you can register a component for disposal when the request goes out of scope. The method is called "RegisterForDispose" and it's part of the Request. The component being disposed must implement IDisposable.

The best approach is to create your own extension method as below...

       public static T RegisterForDispose<T>(this T toDispose, HttpRequestMessage request) where T : IDisposable
   {
       request.RegisterForDispose(toDispose); //register object for disposal when request is complete
      return toDispose; //return the object
   }

Now (in your api controller) you can register objects you want to dispose when request finalize...

    var myContext = new myDbContext().RegisterForDispose(Request);

Links... https://www.strathweb.com/2015/08/disposing-resources-at-the-end-of-web-api-request/