简体   繁体   中英

Class has not been registered after restart IIS website

I've created a website in IIS. Rebuild my app, it is all OK, but after restart IIS website, there is an error:

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.

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. It is not recommended to use AppDomain.CurrentDomain.GetAssemblies() in IIS because all assemblies may not being loaded yet.

In order to avoid this issue, you can use the GetReferencedAssemblies method on the System.Web.Compilation.BuildManager

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.

See Why aren't my assemblies getting scanned after IIS restart for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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