Autofac and IDisposable interface

后端 未结 2 1416
野性不改
野性不改 2021-02-05 12:54

Assuming that I have the following interface and class:

public interface IFooRepo : IDisposable { 

    //...
}

public FooRepo : IFooRepo { 

    //Methods here         


        
相关标签:
2条回答
  • 2021-02-05 13:37

    This portion comes into lifetime management in IOC or DI Container.

    As you are using AutoFac following link may help you. http://autofac.readthedocs.io/en/latest/lifetime/disposal.html

    Also look at section of "Controlling scope and lifetime" for autofac.

    0 讨论(0)
  • 2021-02-05 13:45

    Autofac calls Dispose for all instances of components implementing IDisposable once their parent lifetime scope ends. You don't need to do any additional work here.

    To get familiar with options provided by Autofac for managing lifetime scopes, follow @dotnetstep's links.

    Managing lifetime scopes is a strategy that depends on your specific application not only its type (MVC or plain ASP.NET or whatever). This article about lifetimes by the Autofac's creator gives a deep explanation of the topic.

    As for MVC3 project, I'd recommend you follow the MVC3 integration guidelines. This will make all individual HTTP requests have separate lifetime scopes created for them. Once a HTTP request is finished, Autofac will finish the associated lifetime scope and dispose all disposable resources created in that scope.

    The same effect can be achieved for an ASP.NET WebForms project by following the corresponding guidelines

    0 讨论(0)
提交回复
热议问题