Why is an explicit Dispose() function required in asp.net MVC Controllers? Can somebody clarify the complexities? (special to ASP.NET)

asp.net-mvc c# entity-framework garbage-collection idisposable

Question

I am aware that Ctrash #'s collector can manage resources rather well. What precisely is this for, and why is it required, given that it already has that?

Who can explain why?.Dispose() is essential for Asp.net MVC?

What does it imply to dispose of a link, furthermore? Why is it required? Anyone familiar with the details of why it's crucial to terminate a database connection, such as indb.Dispose() ? Is this an issue with SQL Server or EF? I'm attempting to pinpoint the precise reason.

protected override void Dispose(bool disposing)
{
   db.Dispose();
   base.Dispose(disposing);
}
1
29
4/13/2012 2:38:24 AM

Accepted Answer

Dispose is used to release "unmanaged" resources, such as sockets, file handles, bitmap handles, and so on, and if it's being used outside a finalizer (that's what thedisposing flag denotes, by the way), for discarding further IDisposable objects it now has that are no longer necessary.

The term "unmanaged" resources refers to resources that are not managed by the CLR and are not tampered with or automatically released by the GC; lacking aDispose It will depend on the object's finalizer to clean up. function (and code actually utilizing it!). If the app is healthy and the object is a finalizer, the finalizer will eventually run, and if it does its job, everything will be mostly OK. However, it will take its sweet time, so if you run out of handles in the meanwhile, that's okay. It's a shame since that other thread, procedure, or whatever needs them.

If youDispose However, as soon as the resources are freed, everything functions better as a whole.

(By the way, EF, SQL Server, or any other technologies are not required for this. It is recommended to adopt the Disposable pattern anytime you have an IDisposable that is no longer in use since it is present throughout the.net framework.)

Why, thenIDisposable is implemented so far up the tree, as opposed to you just implementing it case by case, that I'm not really sure. However, supposing you were creating a framework. Think about how difficult it would be to dispose of anything if everything weren't an IDisposable. — if the item is a throwaway, thenDispose the case. However, if you implement IDisposable "just in case," everything is easier since you merely discard. ( An object just doesn't override if there is nothing to clean up.Dispose if so, then its parents'Dispose gets called and does any necessary cleaning, which may be nothing at all.) And even if it isn't the underlying reason, it makes a lot of sense to do so nevertheless because controllers often have things to clean up.

28
4/13/2012 3:43:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow