如何使用Autofac来解析Nancy创建的子生命周期范围中的类型的每个请求依赖项的实例
我们在Windows服务中托管了多个应用程序,这些应用程序自我托管Nancy端点,以便公开有关应用程序操作的检测。
我们使用Autofac作为我们的IOC。 几个存储库在所有应用程序共享的核心DLL中注册到根容器中; 然后使用从Nancy.Autofac.Bootstrapper
派生的引导程序将此容器作为其容器传递给Nancy。
我们发现,当Nancy收到Web请求时,它会从根容器中解析对存储库的请求,这会导致内存被非垃圾收集的IDisposable
消耗,因为根容器不会超出范围(它有Windows服务的生命周期)。 这导致服务“泄漏”内存。
然后,我们切换到一个模型,在我们的南希引导程序中,我们使用InstancePerRequest
在重写的ConfigureRequestContainer()
方法中添加了存储库的注册:
protected override void ConfigureRequestContainer(ILifetimeScope container, NancyContext context) { base.ConfigureRequestContainer(container, context); PerRequestContainerBuilder().Update(container.ComponentRegistry); } private static ContainerBuilder PerRequestContainerBuilder() { var builder = new ContainerBuilder(); // Dependency for repository builder.RegisterType().InstancePerRequest().As(); // Repository builder.RegisterType().InstancePerRequest().As(); return builder; }
我们还重写CreateRequestContainer()
方法以使用标记MatchingScopeLifetimeTags.RequestLifetimeScopeTag
创建请求容器。
protected override ILifetimeScope CreateRequestContainer(NancyContext context) { return ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); }
这似乎解决了IDisposable
没有被处理的问题 – 子请求容器被放置在Web请求管道的末尾,并且由它解析的对象也被处理并最终被垃圾收集。
我们的问题是,这似乎是将存储库的实现细节泄漏到服务中,因为我们不仅要在ConfigureRequestContainer()
注册存储库,还要注册存储库所需的任何其他对象,即如果我们想要更改实现我们必须“遍历依赖链”以使用它在每个服务中注册所需的对象 – 这似乎是错误的。
有没有办法可以让Autofac从根容器中解析存储库的支持对象,但是将注册信息保留在Web请求容器的范围内? 或者有没有办法在创建根容器时自动将现有注册从子容器复制到子容器中?
Autofac应自动解析“父”生命周期中的实例。 如果使用InstancePerRequest
配置注册,Autofac将使用特殊的生命周期标记MatchingScopeLifetimeTags.RequestLifetimeScopeTag
注册这些服务,以便以后可以在正确的范围内解析。
这意味着不需要使用Nancy bootstrapper的ConfigureRequestContainer
方法来执行请求范围的注册。 你已经完成了! 只要Nancy使用InstancePerRequest
中使用的相同标记创建请求生存期( 默认情况下从Nancy 1.1开始),就应该正确解析服务。
例:
public class Startup { public void Configuration(IAppBuilder app) { var builder = new ContainerBuilder(); // Do request-scoped registrations using InstancePerRequest... var container = builder.Build(); // Pass the pre-built container to the bootstrapper var bootstrapper = new MyAwesomeNancyBootstrapper(container); app.UseNancy(options => options.Bootstrapper = bootstrapper); } }
public class MyAwesomeNancyBootstrapper : AutofacNancyBootstrapper { private readonly ILifetimeScope _lifetimeScope; public MyAwesomeNancyBootstrapper(ILifetimeScope lifetimeScope) { _lifetimeScope = lifetimeScope; } protected override ILifetimeScope GetApplicationContainer() { return _lifetimeScope; // Tell Nancy you've got a container ready to go ;) } }
此设置应该足够(从Nancy 1.1开始。在早期版本中,您还必须覆盖CreateRequestContainer
方法并在创建请求生存期范围时传递请求生命周期标记)。
编辑 :我在https://github.com/khellang/Nancy.AutofacExample为您整理了一个示例
上述就是C#学习教程:如何使用Autofac来解析Nancy创建的子生命周期范围中的类型的每个请求依赖项的实例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1005090.html