From 3c7b40618cec526740ca4a5ba01bafb7ad3d4cc1 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Tue, 18 Dec 2018 14:14:38 +0300 Subject: [PATCH] Added IServiceCollection.OnRegistred Event section. --- docs/en/Dependency-Injection.md | 42 ++++++++++++++++++++++++ docs/en/Dynamic-Proxying-Interceptors.md | 3 ++ 2 files changed, 45 insertions(+) create mode 100644 docs/en/Dynamic-Proxying-Interceptors.md diff --git a/docs/en/Dependency-Injection.md b/docs/en/Dependency-Injection.md index 5186f29717..d1ca14950c 100644 --- a/docs/en/Dependency-Injection.md +++ b/docs/en/Dependency-Injection.md @@ -270,6 +270,48 @@ using (var scope = _serviceProvider.CreateScope()) Both services are released when the created scope is disposed (at the end of the using block). +## Advanced Features + +### IServiceCollection.OnRegistred Event + +You may want to perform an action for every service registered to the dependency injection. In the `PreConfigureServices` method of your module, register a callback using the `OnRegistred` method as shown below: + +````csharp +public class AppModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.OnRegistred(ctx => + { + var type = ctx.ImplementationType; + //... + }); + } +} +```` + +`ImplementationType` provides the service type. This callback is generally used to add interceptor to a service. Example: + +````csharp +public class AppModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.OnRegistred(ctx => + { + if (ctx.ImplementationType.IsDefined(typeof(MyLogAttribute), true)) + { + ctx.Interceptors.TryAdd(); + } + }); + } +} +```` + +This example simply checks if the service class has `MyLogAttribute` attribute and adds `MyLogInterceptor` to the interceptor list if so. + +> Notice that `OnRegistred` callback might be called multiple times for the same service class if it exposes more than one service/interface. So, it's safe to use `Interceptors.TryAdd` method instead of `Interceptors.Add` method. See [the documentation](Dynamic-Proxying-Interceptors.md) of dynamic proxying / interceptors. + ## 3rd-Party Providers While ABP has no core dependency to any 3rd-party DI provider, it's required to use a provider that supports dynamic proxying and some other advanced features to make some ABP features properly work. diff --git a/docs/en/Dynamic-Proxying-Interceptors.md b/docs/en/Dynamic-Proxying-Interceptors.md new file mode 100644 index 0000000000..76a23daa8f --- /dev/null +++ b/docs/en/Dynamic-Proxying-Interceptors.md @@ -0,0 +1,3 @@ +## Dynamic Proxying / Interceptors + +TODO \ No newline at end of file