在用作ServiceStack的IoC时在autofac中配置生存期作用域 [英] Configuring lifetime scopes in autofac when used as ServiceStack's IoC

查看:72
本文介绍了在用作ServiceStack的IoC时在autofac中配置生存期作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用AutoFac作为ServiceStack Web服务应用程序的DI容器.我可以配置布线和所有内容,但是在阅读了示波器"部分后,我不知所措,在注册组件时最好使用哪种示波器.在我们的特殊情况下,我认为PerHttpRequest范围是可以的,因为(如果我做错了,请纠正我)我想在请求结束后立即处理依赖项.

I'm currently using AutoFac as the DI container for our ServiceStack web services app. I'm able to configure the wiring and everything, but after reading the section on Scopes, I'm at a loss at which scope would be best to use when registering my components. In our particular case, I think a PerHttpRequest scope would be OK since (please correct me if im wrong) I would want to dispose the dependencies as soon as the request ends.

我的问题是,如何在容器中进行设置?我似乎在autofac包含的方法中找不到"PerHttpRequest"生存期范围.我也不确定ServiceStack是否会在后台为我执行某种自动操作.

My question is, how do I set this up in the container? I can't seem to find the "PerHttpRequest" lifetime scope within the included methods in autofac. I'm also unsure if ServiceStack does some kind of automagic to do this for me behind the scenes.

我正在.Net 4的ServiceStack 3.9.35上使用Autofac 3.0.1(作为常规ASP主机运行,而不是MVC运行).我还将此处中描述的类用作IContainer适配器./p>

I'm using Autofac 3.0.1 on ServiceStack 3.9.35 on .Net 4 (running as a regular ASP host, not MVC). I'm also using the class described here as the IContainer adapter.

推荐答案

我想我已经弄清楚了如何使它工作(使用我现在使用的Autofac 2.6.)它涉及到使用以下适配器和Autofac.Mvc3软件包:

I think I have figured out how to make this work (using Autofac 2.6, which I am stuck on right now.) It involves using the following adapter and the Autofac.Mvc3 package:

public class AutofacIocAdapter : IContainerAdapter
{
    private readonly IContainer _autofacRootContainer;
    private readonly Container _funqContainer;

    public AutofacIocAdapter(IContainer autofacRootContainer, Container funqContainer)
    {
        // Register a RequestLifetimeScopeProvider (from Autofac.Integration.Mvc) with Funq
        var lifetimeScopeProvider = new RequestLifetimeScopeProvider(autofacRootContainer,null);
        funqContainer.Register<ILifetimeScopeProvider>(x => lifetimeScopeProvider);
        // Store the autofac application (root) container, and the funq container for later use
        _autofacRootContainer = autofacRootContainer;
        _funqContainer = funqContainer;
    }

    public T Resolve<T>()
    {           
        return ActiveScope.Resolve<T>();
    }

    public T TryResolve<T>()
    {
        T result;
        if (ActiveScope.TryResolve(out result))
        {
            return result;
        }
        return default(T);
    }

    private ILifetimeScope ActiveScope
    {
        get
        {
            // If there is an active HttpContext, retrieve the lifetime scope by resolving
            // the ILifetimeScopeProvider from Funq.  Otherwise, use the application (root) container.
            return HttpContext.Current == null
                        ? _autofacRootContainer
                        : _funqContainer.Resolve<ILifetimeScopeProvider>().GetLifetimeScope();
        }
    }
}

实施步骤:

  1. 将Autofac.Mvc3 NuGet程序包添加到您的Web项目中(注意:无关紧要,因为您的项目没有使用MVC.该解决方案可能与无法使用Mvc3集成的Autofac 3有所不同.)
  2. 按照 ServiceStack IoC 页连接自定义IContainerAdapter对于Autofac,请使用以下实现
  1. Add the Autofac.Mvc3 NuGet package to your web project (NOTE: does not matter that your project isn't using MVC. The solution might be slightly different with Autofac 3, which cannot use Mvc3 integration.)
  2. Follow the ServiceStack IoC page in hooking up a custom IContainerAdapter for Autofac, using the following implementation

这篇关于在用作ServiceStack的IoC时在autofac中配置生存期作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆