2015-12-15 72 views
0

我在IIS中創建了一個網站。重建我的應用程序,它是一切OK,但重新啓動IIS網站之後,有一個錯誤:類重新啓動IIS網站後未註冊

Autofac.Core.Registration.CompoentNotRegisteredException: The registered service 'WYC.Core.Services.BlogService' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

發生了什麼事?爲什麼?我的方式:

container.RegisterAssemblyTypes(assemblies) 
     .Where(t => t.Name.EndsWith("Service")) 
     .SingleInstance() 
     .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); 

回答

1

當IIS回收應用程序池時,會根據需要加載程序集。不建議在IIS中使用AppDomain.CurrentDomain.GetAssemblies(),因爲所有程序集可能尚未加載。

爲了避免這個問題,你可以在System.Web.Compilation.BuildManager

var assemblies = BuildManager.GetReferencedAssemblies(); 
container.RegisterAssemblyTypes(assemblies) 
     .Where(t => t.Name.EndsWith("Service")) 
     .SingleInstance() 
     .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies); 

的代碼中使用GetReferencedAssemblies上述方法應該在IIS回收過程之後的作品。

查看Why aren’t my assemblies getting scanned after IIS restart瞭解更多信息。

+0

謝謝你,你太棒了! – PoppingKevin

+0

歡迎來到StackOverflow。如果答案有幫助,不要忘記接受它以幫助其他用戶。請參閱[我應該怎麼做當有人回答我的問題?](http://stackoverflow.com/help/someone-answers)瞭解更多信息 –