Autofac可以解析具有混合范围的组件

时间:2011-10-24 22:01:42

标签: .net dependency-injection autofac

我在asp.net中使用Autofac 2.5而且我遇到的问题是,生命周期范围组件被解析为单实例组件的依赖关系,从而破坏了我的线程安全性。这是注册的问题,但我认为Autofac将此视为违规,并会抛出异常。

    private class A{}

    private class B
    {
        public B(A a){}
    }

    [Test]
    [ExpectedException()]
    public void SingleInstanceCannotResolveLifetimeDependency()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<A>()
            .InstancePerLifetimeScope();
        builder.RegisterType<B>()
            .SingleInstance();

        using (var container = builder.Build())
        {
            using (var lifetime = container.BeginLifetimeScope())
            {
                //should throw an exception
                //because B is scoped singleton but A is only scoped for the lifetime
                var b = lifetime.Resolve<B>();
            }
        }
    }

如果发生这种情况,有没有办法让Autofac抛出依赖性解决异常?

更新 即使这是Autofac的正确行为 - SingleInstance只是Root生命周期范围 - 它在Web环境中可能存在潜在危险。确保所有开发人员都能获得正确的注册信息可能会很麻烦。以下是Autofac的一个小扩展方法,它检查实例查找以确保在根范围内不解析生存期范围的实例。我知道它帮助我们从我们的网络项目中解决了生命周期问题。

    public static class NoLifetimeResolutionAtRootScopeExtensions
{
    /// <summary>
    /// Prevents instances that are lifetime registration from being resolved in the root scope
    /// </summary>
    public static void NoLifetimeResolutionAtRootScope(this IContainer container)
    {
        LifetimeScopeBeginning(null, new LifetimeScopeBeginningEventArgs(container));
    }

    private static void LifetimeScopeBeginning(object sender, LifetimeScopeBeginningEventArgs e)
    {
        e.LifetimeScope.ResolveOperationBeginning += ResolveOperationBeginning;
        e.LifetimeScope.ChildLifetimeScopeBeginning += LifetimeScopeBeginning;
    }

    private static void ResolveOperationBeginning(object sender, ResolveOperationBeginningEventArgs e)
    {
        e.ResolveOperation.InstanceLookupBeginning += InstanceLookupBeginning;
    }

    private static void InstanceLookupBeginning(object sender, InstanceLookupBeginningEventArgs e)
    {
        var registration = e.InstanceLookup.ComponentRegistration;
        var activationScope = e.InstanceLookup.ActivationScope;

        if (registration.Ownership != InstanceOwnership.ExternallyOwned
            && registration.Sharing == InstanceSharing.Shared
            && !(registration.Lifetime is RootScopeLifetime)
            && activationScope.Tag.Equals("root"))
        {
            //would be really nice to be able to get a resolution stack here
            throw new DependencyResolutionException(string.Format(
                "Cannot resolve a lifetime instance of {0} at the root scope.", registration.Target))
        }
    }
}

只需在创建容器时应用此选项,并且在根范围内解析生命周期范围的服务时将引发异常。

container.NoLifetimeResolutionAtRootScope();

1 个答案:

答案 0 :(得分:3)

是 - 您需要为子范围命名,并明确地将组件A与其关联。否则,正如您所观察到的,在根(容器)范围中创建了A实例。

// Replace `A` registration with:
builder.RegisterType<A>().InstancePerMatchingLifetimeScope("child");

和...

// Replace scope creation with:
using (var lifetime = container.BeginLifetimeScope("child")) {