ABP Vnext 引入外部JSON配置文件(AddJsonFile)的方法。
包括ABP通过JSON配置文件进行动态注入依赖
public class WPFAppModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = BuildConfiguration();
//var configuration = context.Services.GetConfiguration();
context.Services.AddFromConfigurationFile(configuration.GetSection("Services"));
}
private static IConfigurationRoot BuildConfiguration()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("configuration.json", optional: true, reloadOnChange: true)
//.AddJsonFile("autofac.json", optional: false, reloadOnChange: true)
.Build();
}
}
//configuration.json文件
{
//描述:"实现类命名空间+实现类名称,所在程序集名称"
"Services": {
"Singleton": [
],
"Transient": [
{
"Service": "WpfApp1.ISampleService,WpfApp1",
"Implementation": "WpfApp1.SampleService,WpfApp1"
}
],
"AddScoped": [
]
}
}
ABP通过配置文件进行动态注入
https://blog.csdn.net/xhl_james/article/details/90700217
namespace WpfApp1
{
public class ServicesConfiguration
{
public IEnumerable<ServiceItem> Singleton { get; set; }
public IEnumerable<ServiceItem> Transient { get; set; }
public IEnumerable<ServiceItem> AddScoped { get; set; }
}
public class ServiceItem
{
public string Service { get; set; }
public string Implementation { get; set; }
}
public static class AddFromConfigurationFileExtension
{
/// <summary>
/// 通过配置文件进行注入实现。
/// </summary>
/// <remarks>
/// 需要注意的是由于通过的配置文件进行配置,那么要确保接口、实现对应的程序集和
/// typeof(AddFromConfigurationFileExtension).Assembly程序集同目录。
/// 不然Type.GetType会找不到对应的类。
/// </remarks>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection AddFromConfigurationFile(this IServiceCollection services,
IConfigurationSection configuration)
{
var servicesConfiguration = configuration.Get<ServicesConfiguration>();
Type @interface = null, implementation = null;
if (servicesConfiguration.Singleton != null)
{
foreach (var service in servicesConfiguration.Singleton)
{
@interface = Type.GetType(service.Service, false);
implementation = Type.GetType(service.Implementation, false);
if (@interface == null)
throw new ArgumentNullException($"{service.Service}未找到。请和管理员联系!", nameof(@interface));
if (@interface == null)
throw new ArgumentNullException($"{service.Implementation}未找到。请和管理员联系!", nameof(implementation));
services.AddSingleton(@interface, implementation);
}
}
if (servicesConfiguration.Transient != null)
{
foreach (var service in servicesConfiguration.Transient)
{
//确保能够正确注册。防止影响后面的功能。
@interface = Type.GetType(service.Service, false);
implementation = Type.GetType(service.Implementation, false);
if (@interface == null)
throw new ArgumentNullException($"{service.Service}未找到。请和管理员联系!", nameof(@interface));
if (@interface == null)
throw new ArgumentNullException($"{service.Implementation}未找到。请和管理员联系!", nameof(implementation));
services.AddTransient(@interface, implementation);
}
}
if (servicesConfiguration.AddScoped != null)
{
foreach (var service in servicesConfiguration.AddScoped)
{
//确保能够正确注册。防止影响后面的功能。
@interface = Type.GetType(service.Service, false);
implementation = Type.GetType(service.Implementation, false);
if (@interface == null)
throw new ArgumentNullException($"{service.Service}未找到。请和管理员联系!", nameof(@interface));
if (@interface == null)
throw new ArgumentNullException($"{service.Implementation}未找到。请和管理员联系!", nameof(implementation));
services.AddScoped(@interface, implementation);
}
}
return services;
}
}
}