Autofac 解析具有混合作用域的组件

Dan*_*elg 5 .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>();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

    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))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

只需在创建容器时应用它,当在根范围内解析生命周期范围内的服务时,您将得到抛出的异常。

container.NoLifetimeResolutionAtRootScope();
Run Code Online (Sandbox Code Playgroud)

Nic*_*rdt 3

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

// Replace `A` registration with:
builder.RegisterType<A>().InstancePerMatchingLifetimeScope("child");
Run Code Online (Sandbox Code Playgroud)

和...

// Replace scope creation with:
using (var lifetime = container.BeginLifetimeScope("child")) {
Run Code Online (Sandbox Code Playgroud)