ABP Vnextの方法JSON外形(AddJsonFile)が導入されました。
ABPは、JSONによって動的依存性注入プロファイルを含みます
パブリッククラスWPFAppModule:AbpModule
{
公共オーバーライドボイドConfigureServices(ServiceConfigurationContextコンテキスト)
{
VAR構成= BuildConfiguration()。
// VAR構成= context.Services.GetConfiguration();
context.Services.AddFromConfigurationFile(configuration.GetSection( "サービス"));
}
プライベート静的IConfigurationRoot BuildConfiguration()
{
()新しいConfigurationBuilderを返す
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile( "configuration.json"、任意の真:真、reloadOnChange)
、//.AddJsonFile("autofac.json」オプション:偽、reloadOnChange:真)
.Build();
}
}
//configuration.jsonファイル
{
//説明:「アセンブリ名実現実装クラスの名前空間のクラス名、」
「:{」サービス
「シングルトン」:[
]、
"一過性":[
{
"サービス": "WpfApp1.ISampleService、WpfApp1"、
"実装": "WpfApp1.SampleService、WpfApp1"
}
]、
"AddScoped":[
]
}
}
ABPは、動的プロファイルを介して注入しました
https://blog.csdn.net/xhl_james/article/details/90700217
名前空間WpfApp1
{
publicクラスのServicesConfiguration
{
公共のIEnumerable <ServiceItem>シングルトン{取得します。セットする; }
公共のIEnumerable <ServiceItem>過渡{得ます。セットする; }
公共のIEnumerable <ServiceItem> AddScoped {得ます。セットする; }
}
publicクラスServiceItem
{
公共の文字列サービス{取得します。セットする; }
パブリック文字列の実装{GET。セットする; }
}
パブリック静的クラスAddFromConfigurationFileExtension
{
/// <要約>
///通过配置文件进行注入实现。
/// </要約>
/// <備考>
///は、設定するには、コンフィギュレーションファイルの採用は、その後、必ずインターフェイスを作成し、アセンブリに対応することに留意すべきである
ディレクトリに/// typeof演算(AddFromConfigurationFileExtension).Assemblyアセンブリ。
/// Type.GetTypeは、そうでない場合は、対応するクラスを見つけられないでしょう。
/// </備考>
/// <PARAM NAME = "サービス"> </ param>の
/// <PARAM NAME = "設定"> </ param>の
/// <戻り値> </戻り値>
のpublic static IServiceCollection AddFromConfigurationFile(このIServiceCollectionサービス、
IConfigurationSection構成)
{
VAR servicesConfiguration = configuration.Get <servicesConfiguration>();
タイプ@interface =ヌル、実装= NULL;
IF(servicesConfiguration。
foreachの(VAR-サービスservicesConfiguration.Singleton中)
{
@interface Type.GetType =(falseにservice.Service、);
実装= Type.GetType(falseにservice.Implementation、);
IF(@interface == nullの)
スロー新しい新しい例外ArgumentNullExceptionザ・($ "{} service.Serviceしてください連絡先を見つけていない管理者。!"、がNameOf(@interface));
IF(@interface == nullの)
スロー新しい新しい例外ArgumentNullException($「} {service.Implementationはありません管理者としてくださいました。連絡先」、がNameOf(実装));!
services.AddSingleton(@interface、インプリメンテーション);
}
}
IF(servicesConfiguration.Transient!= NULL)
{
foreachの(servicesConfiguration.Transient中のvarサービス)
{
//正常に登録することを確認してください。後者の影響関数を防ぐために。
= Type.GetTypeの@interface(falseにservice.Service、);
実装=(falseにservice.Implementation、)Type.GetType;
IF(@interface == nullの)
スロー新しい新しい例外ArgumentNullExceptionザ・($「} {service.Serviceが見つからない、してください。管理者!」、がNameOf(@interface));
IF(@interface == nullの)
スロー新しい新しい例外ArgumentNullException($ "} {service.Implementationがください連絡先を見つけていない管理者"、がNameOf(実装。!) );
services.AddTransient(@interface、インプリメンテーション);
}
}
IF(!servicesConfiguration.AddScoped = NULL)
{
foreachのは(varがインサービスservicesConfiguration.AddScoped)
{
//適切な位置合わせを確保します。後者の影響関数を防ぐために。
= Type.GetTypeの@interface(falseにservice.Service、);
実装=(falseにservice.Implementation、)Type.GetType;
IF(@interface == nullの)
スロー新しい新しい例外ArgumentNullExceptionザ・($「} {service.Serviceが見つからない、してください。管理者」、がNameOf(@interface));!
IF(@interface == nullの)
新しい例外ArgumentNullExceptionスロー($ "{service.Implementation } 、がNameOf(実装)管理者してください連絡先が見つかりませんが。!");
services.AddScoped(@interface、インプリメンテーション);
}
}
リターンサービス;
}
}
}