.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed
.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_CustomerBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_CustomerBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_CustomerBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_INV_BridgesBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_INV_BridgesBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_INV_BridgesBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Product_WebApiOnlyBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Product_WebApiOnlyBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Product_WebApiOnlyBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_CustomerBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_CustomerBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_CustomerBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_INVBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_INVBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_INVBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_ProductBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_ProductBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_ProductBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_SupplierBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_SupplierBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_SupplierBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_YDBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_YDBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_YDBusiness'.)”
原因
运行WebApi时一些用户自定义服务无法依赖注入。
解决方案
var builder = Host.CreateDefaultBuilder(args);
builder.LoadAssemblies(); //加载全局程序集
/// <summary>
/// 加载程序集
/// </summary>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IHostBuilder LoadAssemblies(this IHostBuilder hostBuilder)
{
//显式引用类所在的dll
var T1 = typeof(CSFramework.WebApi.Common.WebApiTools);
var T2 = typeof(CSFrameworkV6.Models.tb_MyUser);
//加载以下程序集(包含所有实体模型、自定义服务的程序集)
GlobalAssemblies.Assemblies = new System.Reflection.Assembly[]
{
System.Reflection.Assembly.Load("CSFramework.WebApi.Common"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Core"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Models"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Interfaces"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Business"),
System.Reflection.Assembly.Load("CSFramework.WebApi"),
System.Reflection.Assembly.Load("CSFrameworkV6.Models"),
};
return hostBuilder;
}
services.AddInjectionServices(); //自动注入服务(注入用户自定义接口)
服务的生命周期
默认的依赖注入容器提供了三种生命周期:
暂时(AddTransient)
每次在向服务容器进行请求时都会创建新的实例,这种生存期适合轻量级、 无状态的服务。
范围内(AddScoped)
为每个客户端请求创建一次实例。
单例(AddSingleton)
第一次请求时(或者在运行Startup.ConfigureServices 并且使用服务注册指定实例时)创建,每个后续请求都使用相同的实例。如果应用需要单例行为,建议允许服务容器管理服务的生存期。 不要再自己实现单例设计模式。