Acrotygma
Acrotygma

Reputation: 2569

Why does no one disposes DbContext after WebApi controller operation?

I am aware of various tutorials as well as complete examples targeting WebApi & Entity Framework (even from Microsoft) that have WebApi controller like this:

public HttpResponseMessage GetInternet(int id) {
    var context = new InternetDbContext();
    var result =
       (from internet in context.Internets
        where internet.Id.Equals(id)
        select internet).FirstOrDefault();
    if(result != null)
       Request.CreateResponse(HttpStatusCode.OK, result);
}

But when I learnt about Entity Framework like 2 years ago, every resource I found about the framework pointed out how extremely important it is to DISPOSE the DbContex in the SHORTEST possible lifespan, e.g. with 'using'. And nowadays, people seem to don't give a shit about disposing anything (their managers, repositories, DI containers...).

Am I missing something here? Does the end of the API call automatically disposes the context? Or do I have to use something like HttpRequestMessageExtensions.RegisterForDispose() from http://msdn.microsoft.com/en-us/library/dn153859(v=vs.118).aspx?

Upvotes: 12

Views: 7927

Answers (6)

Tanato
Tanato

Reputation: 1173

You should call Dispose() or use using when working with DbContext but you don't have to.

If you want to play safe, always wrap in a using or Dispose() but if you want to have a better understanding of EF DbContext implementation, keep reading.

Long story short, EF usually knows when is the time to Close a connection, so in the majority of the cases, call or not call Dispose() does the same result and doesn't have any impact in memory usage or performance because garbage collector will handle properly, unlike most of the IDisposable classes.

But, there are two main reasons that you should wrap in using or call Dispose() specifically for EF DbContext.

First is when someone manually open a connection with ObjectContext from the DbContext, if you don't call Dispose()/using you could leave open connections as those connections will not be managed automatically by EF.

The second reason is that the derived class of DbContextthat you are instantiating could be overriting the default behavior of dispose for example to aggregate the disposal of other unmanaged resources into the lifetime of the context, so if not properly disposed, it'll leave those resources alive.

This article is a must read.

Upvotes: -1

John Henckel
John Henckel

Reputation: 11357

Sometimes it is a bad idea to dispose the context. For example, I have a WebAPI2 controller method like this,

    [Route("Questionnaires")]
    public IEnumerable<Questionnaire> GetAllQuestionnaires()
    {
        NMQContext context = new NMQContext();
        return context.Questionnaires.AsEnumerable();
    }

The result data is a JSON list, and the Questionnaire is a compound object -- it contains entities from multiple database tables. If I wrap this with "using" I get an error, such as

   "Message": "An error has occurred.",
   "ExceptionMessage": "The operation cannot be completed because the DbContext has been disposed.",

If you are trying to serialize compound objects, then it is better NOT to dispose the connection. It is better to let the EF handle it for you. You could probably fix it by explicit eager loading, but that is painful. Don't do it.

Upvotes: 2

Rob Lyndon
Rob Lyndon

Reputation: 12661

It's because they're wrong. Don't trust other people's code, especially when it's part of an online tutorial.

Upvotes: 0

volpav
volpav

Reputation: 5128

Personally, whenever I see the type implements IDisposable, I'm almost certain that I'm going to use a using statement when working with new instances of this type.

When the variable goes out of scope (like in your case with the context variable going out of scope when the execution returns from GetInternet method), its memory is eventually going to be reclaimed by garbage collector but this doesn't mean that any native handlers (e.g. file handlers or database connections) are going to be closed which can have a very serious negative impact on your application.

So, consider always wrapping an IDisposable into the using construct:

using (var context = new InternetDbContext())
{
  // Your code goes here
}

Hope this helps.

Upvotes: 9

Joachim Isaksson
Joachim Isaksson

Reputation: 180927

Prime case for using, no matter how the method is exited, your DbContext will be disposed;

public HttpResponseMessage GetInternet(int id) {
    using(var context = new InternetDbContext()) {
        var result =
           (from internet in context.Internets
            where internet.Id.Equals(id)
            select internet).FirstOrDefault();
        if(result != null)
           Request.CreateResponse(HttpStatusCode.OK, result);
    }
}

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33139

You are supposed to Dispose() of your context class, so use the using construct:

using (var context = new InternetDbContext())
{    
    // your code here, try/catch is auto-generated by the compiler
}

Upvotes: 0

Related Questions