2

我已经使用Autofac多年,从未见过这种行为:构建之后,应用程序运行良好。 然而后的应用程序池被回收(我通过触摸web.config中测试这一点),我得到一个DependencyResolutionExceptionAutofac无法在应用程序池回收后解决

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Namespace.Controllers.HomeController' can be invoked with the available services and parameters

的HomeController有一个依赖,它具有其他依赖,等等。但我知道可以通过解决它们,因为它在它被回收之前就已经完成了。为什么地球上会发生这种情况?

我自动装配一切像这样:

public static IContainer GetAutoFacContainer() 
{ 
    var builder = new ContainerBuilder(); 
    var assembliesToRegister = AppDomain.CurrentDomain.GetAssemblies() 
     .Where(a => a.FullName.StartsWith("Prefix")).ToArray(); 
    builder.RegisterAssemblyTypes(assembliesToRegister) 
     .AsImplementedInterfaces().AsSelf().PropertiesAutowired().InstancePerLifetimeScope(); 
    return builder.Build(); 
} 

然后我用容器的WebAPI MVC控制器。

var autoFacContainer = DependencyRegistrar.GetAutoFacContainer(); 
DependencyResolver.SetResolver(new AutofacDependencyResolver(autoFacContainer)); 
GlobalConfiguration.Configuration.DependencyResolver = 
    new AutofacWebApiDependencyResolver(autoFacContainer); 
+1

相关:https://stackoverflow.com/questions/37713188/asp-net-api-di-simple-injector-null-reference-on-iis-pool-recycle – Steven

+3

另请参阅:http:// docs .autofac.org/en/latest/faq/iis-restart.html – Steven

+0

谢谢Steven - 解决了我的问题! – ItsJason

回答

2

感谢史蒂芬的评论上面,我没有找到其他问题的答案还有Autofac网站。为什么这些从未在我的研究中出现,我不知道。

http://docs.autofac.org/en/latest/faq/iis-restart.html

解决方案

变化AppDomain.CurrentDomain.GetAssemblies()

BuildManager.GetReferencedAssemblies().Cast<Assembly>()

完成!

相关问题