I am loading assemblies from a "plugins" folder and registering some implementations dynamically.
var appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var pluginsPath = Path.Combine(appPath, path);
var asmfiles = Directory.EnumerateFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
foreach(var asmfile in asmfiles)
{
Assembly.LoadFile(asmfile);
}
After loading all assemblies, I proceed to register the services:
builder.RegisterAssemblyTypes(asms)
.Where(t => typeof(IValidator).IsAssignableFrom(t))
.AsClosedTypesOf(typeof(IValidator<>));
builder.RegisterAssemblyTypes(asms)
.Where(t => typeof(IService).IsAssignableFrom(t) && t.IsClass)
.AsImplementedInterfaces()
.AsSelf();
Just to make sure, I log all registrations:
container.ComponentRegistry
.Registrations
.SelectMany(x => x.Services)
.Select(x => x.Description)
2016-07-13 09:58:52.5673 TRACE: Registered services:
Services.Test.ITestService
ServiceModel.IService
Services.Test.TestService
2016-07-13 09:58:52.5673 TRACE: Registered validators:
TestRequestValidator
Problem is, Autofac can't seem to create a controller with a ITestService
dependency in its constructor.
"type": "Autofac.Core.DependencyResolutionException",
"message": "None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ApiHostService.Controllers.EchoController' can be invoked with the available services and parameters: Cannot resolve parameter 'Services.Test.ITestService testService' of constructor 'Void .ctor(Services.Test.ITestService)'.",
What could be the problem?
Edit: resolving from the container fails.