简体   繁体   English

重新启动IIS网站后,类尚未注册

[英]Class has not been registered after restart IIS website

I've created a website in IIS. 我已经在IIS中创建了一个网站。 Rebuild my app, it is all OK, but after restart IIS website, there is an error: 重建我的应用程序,一切正常,但是重新启动IIS网站后,出现错误:

Autofac.Core.Registration.CompoentNotRegisteredException: The registered service 'WYC.Core.Services.BlogService' has not been registered. Autofac.Core.Registration.CompoentNotRegisteredException:已注册的服务“ WYC.Core.Services.BlogService”尚未注册。 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. 为避免此异常,请注册一个组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解决可选的依赖项。

What has happened? 发生了什么事? Why? 为什么? My way: 我的方式:

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

When IIS recycles the application pool, assemblies are loaded on demand. 当IIS回收应用程序池时,程序集将按需加载。 It is not recommended to use AppDomain.CurrentDomain.GetAssemblies() in IIS because all assemblies may not being loaded yet. 不建议在IIS中使用AppDomain.CurrentDomain.GetAssemblies() ,因为可能尚未加载所有程序集。

In order to avoid this issue, you can use the GetReferencedAssemblies method on the System.Web.Compilation.BuildManager 为了避免此问题,可以在System.Web.Compilation.BuildManager上使用GetReferencedAssemblies方法

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

the code above should works after a IIS recycling process. 上面的代码在IIS回收过程之后应该可以工作。

See Why aren't my assemblies getting scanned after IIS restart for more information. 请参阅IIS重新启动后为什么不扫描我的程序集以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM