Autofac, ASP.NET and Dispose Integration

Autofac is new here, but I like what I see so far. I am trying to use the runtime of my resolved objects, and I am having trouble confirming that the deletion actually occurs after the request completes.

I have a one-time object, which I get at the beginning of the page request and put it at the end. I use autofac to get an instance of the object now, and I wanted to see if autofac would do the recycling for me.

I measured the Dispose () method for the object in question, and I see it “fire” when my page controls life. I don’t see any evidence when I don’t dispose of myself, but let the auto fleet do it.

I use these instructions to configure thigns, including changes to web.config and global.asax. I can create an instance of the object just fine, but I can’t say whether it is actually disposed of. Is there another step?

+1
source share
4 answers

I understood that!

I asked the WRONG container for the object instance - I requested the container application for the object, not the request container.

D'o!

+1
source

, Autofac, , . Autofac ContainerDisposalModule , , HttpApplication.EndRequest, .

, Dispose method, , . Dispose?

+1

:

( IoC), , .

.

A → B → C

A 'factory', B "singleton" C "factory", A B, C.

C , A, B be 'factory'.

+1

Dispose - , , "Dispose". , (, Windows). IDisposable , . , , , , , IDisposable.

:

var myClass = MyDisposableClass();

// do stuff with myClass

myClass.Dispose();


Proper usage:

using (var myClass = MyDisposableClass())
{
    // do stuff with myClass
}

, :

MyDisposableClass myClass = MyDisposableClass();
try
{
    // do stuff with myClass
}
finally
{
    myClass.Dispose();
}

The important difference is that no matter what happens, you know that your Dispose will be invoked. Alternatively, you can bind a destructor (which, if one exists, is called by the garbage collector), which you can then bind to call the Dispose method; but if you need to do this for any reason, be sure not to release the same resource twice (set your pointers to zero after releasing).

0
source

Source: https://habr.com/ru/post/1716804/


All Articles