某些服务无法构建(验证服务描述符时出错)C#
Some services are not able to be constructed (Error while validating the service descriptor) C#
我正在制作 asp .NET 核心 Web API,我制作了一些服务并注入了它,它工作正常。
我在同一个解决方案中制作了一个新的网络应用程序,我想使用相同的服务
我刚得到
Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)
AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: E_CommerceApi.Services.IAttributeService Lifetime: Scoped ImplementationType: E_CommerceApi.Services.AttributeService': Unable to resolve service for type 'E_CommerceApi.Authentication.ApplicationDbContext' while attempting to activate 'E_CommerceApi.Services.AttributeService'.)
服务和服务代码
public interface IAttributeService
{
Task<AllAttributes> GetAll();
}
public class AttributeService : IAttributeService
{
private readonly ApplicationDbContext _db;
public AttributeService(ApplicationDbContext db)
{
_db = db;
}
}
在同一项目中注入服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
// register services
services.AddScoped<IAttributeService, AttributeService>();
}
在另一个不起作用的 Web 项目中注入服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddScoped<IAttributeService, AttributeService>();
}
由于您没有在第二个项目中注册 DBContext,
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
解析 IAttributeService
的尝试失败,因为它的实现 (AttributeService
) 希望将其注入到其构造函数中。
但是,如果我了解您正在尝试做什么,但我不确定我是否了解,那么您似乎希望能够访问第二个项目中的服务而不必执行此操作。
您的第二个项目是完全独立的 Web 应用程序吗?
如果是这样,那么您需要注册它需要的所有组件。所以添加 DBContext.
如果不是,即。如果它是一个带有控制器和视图的引用项目,这些控制器和视图将通过其引用包含在主项目中。然后您需要将其视为主要 Web 托管应用程序的一个模块。这样您就不会再有另一个 Startup class,也不需要注册任何服务。相反,您将能够通过第一个容器及其接口访问任何服务。
如果第二个项目有需要注册到容器中的服务,那么你需要一些方法来做到这一点,比如 IServiceCollection
上的扩展方法,这样它就可以在主 ConfigureServices 中调用方法。
public static void RegisterSecondProjectModule(this IServiceCollection services)
{
// register your services here
services.AddScoped...
}
// then in your main ConfigureServices method:
services.RegisterSecondProjectModule();
注意:对于您希望在解决方案中的两个/所有项目中都可用的任何服务,将接口移动到两个都可以引用的单独项目中是有意义的。我通常会创建一个名称后缀为 .Abstractions
的项目,然后更改默认命名空间以将其排除。
我正在制作 asp .NET 核心 Web API,我制作了一些服务并注入了它,它工作正常。 我在同一个解决方案中制作了一个新的网络应用程序,我想使用相同的服务 我刚得到
Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)
AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: E_CommerceApi.Services.IAttributeService Lifetime: Scoped ImplementationType: E_CommerceApi.Services.AttributeService': Unable to resolve service for type 'E_CommerceApi.Authentication.ApplicationDbContext' while attempting to activate 'E_CommerceApi.Services.AttributeService'.)
服务和服务代码
public interface IAttributeService
{
Task<AllAttributes> GetAll();
}
public class AttributeService : IAttributeService
{
private readonly ApplicationDbContext _db;
public AttributeService(ApplicationDbContext db)
{
_db = db;
}
}
在同一项目中注入服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
// register services
services.AddScoped<IAttributeService, AttributeService>();
}
在另一个不起作用的 Web 项目中注入服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddScoped<IAttributeService, AttributeService>();
}
由于您没有在第二个项目中注册 DBContext,
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
解析 IAttributeService
的尝试失败,因为它的实现 (AttributeService
) 希望将其注入到其构造函数中。
但是,如果我了解您正在尝试做什么,但我不确定我是否了解,那么您似乎希望能够访问第二个项目中的服务而不必执行此操作。
您的第二个项目是完全独立的 Web 应用程序吗?
如果是这样,那么您需要注册它需要的所有组件。所以添加 DBContext.
如果不是,即。如果它是一个带有控制器和视图的引用项目,这些控制器和视图将通过其引用包含在主项目中。然后您需要将其视为主要 Web 托管应用程序的一个模块。这样您就不会再有另一个 Startup class,也不需要注册任何服务。相反,您将能够通过第一个容器及其接口访问任何服务。
如果第二个项目有需要注册到容器中的服务,那么你需要一些方法来做到这一点,比如 IServiceCollection
上的扩展方法,这样它就可以在主 ConfigureServices 中调用方法。
public static void RegisterSecondProjectModule(this IServiceCollection services)
{
// register your services here
services.AddScoped...
}
// then in your main ConfigureServices method:
services.RegisterSecondProjectModule();
注意:对于您希望在解决方案中的两个/所有项目中都可用的任何服务,将接口移动到两个都可以引用的单独项目中是有意义的。我通常会创建一个名称后缀为 .Abstractions
的项目,然后更改默认命名空间以将其排除。