【发布时间】:2016-07-13 08:16:16
【问题描述】:
我正在从“插件”文件夹加载程序集并动态注册一些实现。
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);
}
加载所有程序集后,我继续注册服务:
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();
为了确保,我记录了所有注册:
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
问题是,Autofac 似乎无法在其构造函数中创建具有 ITestService 依赖项的控制器。
"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)'.",
可能是什么问题?
编辑:从容器中解析失败。
【问题讨论】:
-
当您尝试直接从容器解析 ITestService 时会发生什么?
-
无法解决。
标签: asp.net-web-api owin autofac