Skip to content
moattarwork edited this page Dec 14, 2022 · 1 revision

IOC is at heart of ASP.NET Core and we leverage it in every aspect of our framework.

Interception

As of recent changes in the framework, we have interception using the native container. In order to access this feature, you will need to install Foil package, at which point you will have several extensions for IServiceCollection that will allow you to register interceptors as shown in the below example:

public sealed class Startup
{
    // Code omitted for clarity

    public void ConfigureServices(IServiceCollection services)
    {
        services.BootstrapApp<Startup>(Configuration,
            app => app
                .HandleApplicationException<MyApplicationBaseException>()
                .AddServices((container, config) =>
                {
                    container.AddTransient<IMyService, MyService>(
                        by => by.InterceptBy<LogInterceptor>());
                })
        );
    }

    // Code omitted for clarity
}

The LogInterceptor shown in the example can be found in ICG.AspNetCore.Logging.SeriLog package. If you wish to implement your own interceptor you will need to derive from AsyncInterceptorBase class and implement the required methods.

We use interception heavily to instrument components, which enables us to get insights into what is happening inside our application for no extra price for the developer. This is especially useful when we need to troubleshoot an application or explain certain behaviors.

Support for other IoC

To support other IOC container they can be used as extensions on ServiceCollection to import registration into new containers. Below are some samples:

Clone this wiki locally