小编Hug*_*cey的帖子

如何使用Autofac来解析Nancy创建的子生命周期范围中的类型的每个请求依赖项的实例

我们在Windows服务中托管了多个应用程序,这些应用程序自托管Nancy端点,以便公开有关应用程序操作的检测.

我们使用Autofac作为我们的IOC.几个存储库在所有应用程序共享的核心DLL中注册到根容器中; 然后使用从中导出的引导程序将此容器作为其容器传递给Nancy Nancy.Autofac.Bootstrapper.

我们发现,当Nancy收到Web请求时,它会从根容器中解析对存储库的请求,这导致内存被非垃圾收集器消耗,IDisposable因为根容器不会超出范围(它具有Windows服务的生命周期).这导致服务"泄漏"内存.

然后我们切换到一个模型,我们在Nancy bootstrapper中使用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<SystemDateTimeProvider>().InstancePerRequest().As<IDateTimeProvider>();

    // Repository
    builder.RegisterType<BookmarkRepository>().InstancePerRequest().As<IBookmarkRepository>();

    return builder;
}
Run Code Online (Sandbox Code Playgroud)

我们还覆盖了CreateRequestContainer()使用标记创建请求容器的方法MatchingScopeLifetimeTags.RequestLifetimeScopeTag.

protected override ILifetimeScope CreateRequestContainer(NancyContext context)
{
     return ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
}
Run Code Online (Sandbox Code Playgroud)

这似乎已经解决了IDisposable没有被处理的问题- 子请求容器被放置在Web请求管道的末端,并且由它解析的对象也被处理并最终被垃圾收集.

我们的问题是,这似乎是将存储库的实现细节泄漏到服务中,因为我们不仅需要注册存储库,ConfigureRequestContainer()还要注册存储库所需的任何其他对象,即如果我们想要更改存储库的实现,我们必须"遍历依赖链"以使用它来注册每个服务中的所需对象 - 这似乎是错误的.

有没有办法让我们可以让Autofac从根容器中解析存储库的支持对象,但是将注册信息保存在Web请求容器的范围内?或者有没有办法在创建根容器时自动将现有注册从子容器复制到子容器中?

c# dependency-injection autofac nancy

4
推荐指数
1
解决办法
8912
查看次数

标签 统计

autofac ×1

c# ×1

dependency-injection ×1

nancy ×1