我已經使用Autofac多年,從未見過這種行爲:構建之後,應用程序運行良好。 然而,後的應用程序池被回收(我通過觸摸web.config中測試這一點),我得到一個DependencyResolutionException:Autofac無法在應用程序池回收後解決
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);
相關:https://stackoverflow.com/questions/37713188/asp-net-api-di-simple-injector-null-reference-on-iis-pool-recycle – Steven
另請參閱:http:// docs .autofac.org/en/latest/faq/iis-restart.html – Steven
謝謝Steven - 解決了我的問題! – ItsJason