【发布时间】:2019-05-22 08:10:15
【问题描述】:
当尝试激活 Controller 时,由于服务未正确注入,我收到以下错误:
System.InvalidOperationException: '无法解析服务类型 尝试激活时出现“AnubisServer.Server.IDroneOperations” 'AnubisServer.DroneController'。'
我有以下层次结构:
public interface IOperations { }
public interface ICatalog : IOperations { }
public interface IAdmin : ICatalog { }
public interface ICatalogService : IAdmin, ISomethingElse { }
在我的初创公司中,我正在为最衍生的接口ICatalogService 提供具体实现:
启动
public void ConfigureServices(IServiceCollection services) {
services.AddMvc();
ICatalogService catalogService = new SomeConcreteService();
services.AddSingleton(catalogService);
}
控制器
public class CatalogController:Controller
{
private IOperations operations;
public CatalogController(IOperations ops)
{
this.operations=ops;
}
}
所以我不明白为什么注入基类接口不起作用,因为我提供了派生的具体实例。
有什么想法吗?
【问题讨论】:
-
试试
services.AddSingleton<IOperations>(catalogService)
标签: c# asp.net-core dependency-injection