I have a layered web application that I built with ASP.NET MVC 4, WebAPI and some other components. I'm using the latest release of Autofac 2.6.2.859 as my DI container along with the MVC and WebAPI integrations. I have autofac modules setup in my different layers and I'm using the new RegisterAssemblyModules to scan the AppDomain assemblies for the various modules.
On startup every works great. When I edit the web.config file and the application warms up, my registrations are lost. I get a DependencyResolutionException -
None of the constructors found with 'Public binding flags' on type 'My.Class.Name' can be invoked with the available services and parameters:
So my registrations are not being reloaded. Does anyone know how to address this issue? Should I put my initialization code somewhere else other than Application_Start() ?
UPDATE
Here's what my code looks like
public static class IoC
{
public static void Configure()
{
var builder = new ContainerBuilder();
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
//Assembly Modules
builder.RegisterAssemblyModules<NSUTriviaModuleBase>(assemblies);
// Register API controllers using assembly scanning.
builder.RegisterControllers(assemblies);
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(assemblies);
var container = builder.Build();
// Set the dependency resolver implementation.
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
I think the problem is that all assemblies are loaded into the AppDomain when the application first starts, but when the AppDomain is recycled by IIS the assemblies are then only loaded on demand.
Try using the
GetReferencedAssemblies
method onSystem.Web.Compilation.BuildManager
to get a list of the referenced assemblies instead.That should force the referenced assemblies to be loaded into the AppDomain immediately making them available for module scanning.