Autofac测试可以解决所有已注册的类型

Sne*_*eal 18 c# autofac

我有一堆在Autofac中注册的类型,而且一些依赖项相当深.是否有内置的方法来测试我可以解决所有注册类型?我希望在应用程序启动时快速失败,而不是在几分钟后部分失败.

这就是我目前所做的,它似乎有效,但我仍然想知道是否有更好的方法.

    public void VerifyAllRegistrations()
    {
        foreach (IComponentRegistration registration in _container.ComponentRegistrations)
        {
            bool isNewInstance;
            registration.ResolveInstance(_container, new Parameter[0], new Disposer(), out isNewInstance);
        }            
    }

    private class Disposer : IDisposer
    {
        public void Dispose()
        {
            // no-op
        }

        public void AddInstanceForDisposal(IDisposable instance)
        {
            instance.Dispose();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Nic*_*rdt 6

Autofac没有提供任何效果 - 因为Autofac会根据ResolveInstance创建组件,您将面临构造函数的副作用等.

集成测试是解决此问题的最佳方法.