相关资源下载一篇学习Autofac控制作用域和生命周期很好的文章,生命周期作用域等同于你应用中的一个工作单元,一个工作单元将会在开始时启动生命周期作用域,然后需要该工作单元的服务被从生命周期作用域中解析出。
Lifetime Scopes
创建Lifetme Scopes
手动创建作用域,并Disposal。 Lifetime scopes are disposable and they track component disposal, so make sure you always call “Dispose()”or wrap them in “using” statements.
给Lifetime Scopes打标签
有时候你可能需要在Unit of work内共享一些服务,但是由不希望采用全局的共享便利,如单例模式。例如web应用的per-request生命周期,在这中情况下你可以使用InstancePerMatchingLifetimeScope来标识你的生命周期和服务。
举例如下,有个发邮件的组件,事务逻辑中需要发送多次邮件,所以可以在每个逻辑事务片中共享邮件服务。然后不希望邮件组件成为全局单例,可以如下设置。
Adding Registrations to a Lifetime Scope
Autofac 允许你在创建生命周期时添加“on the fly”。This can help you when you need to do a sort of “spot weld” limited registration override or if you generally just need some additional stuff in a scope that you don’t want to register globally. You do this by passing a