Skip to content

Setup: App startup

Jon P Smith edited this page Sep 12, 2020 · 2 revisions

What to run when your application starts

When your application starts EFCore.GenericEventRunner needs to find and register your event handlers and its EventRunner class with NET Core dependency injection (DI) provider. You tell the method which assembles in your application and it then scans each assembles for event handlers and registers then with the DI provider.

There are two options, one without extra configuration and one with configuration.

1. Register without configuration

You provider a series of assemblies to scan. The easier way to do this is use Assembly.GetAssembly(<Type>). e.g.

services.RegisterGenericEventRunner(
    Assembly.GetAssembly(typeof(ReviewAddedHandler)),
    Assembly.GetAssembly(typeof(CosmasUpdaterHandler))
    );

NOTE: If you don't provide any assemblies, then it scans the assembly that called the RegisterGenericEventRunner extension method.

2. Register with configuration

This is very similar to the non-cofig version, but the first parameter should now be of the type IGenericEventRunnerConfig. Here is an example of using the GenericEventRunnerConfig class to register a exception handler for a class called BookDbContext.

var eventConfig = new GenericEventRunnerConfig();
eventConfig.RegisterSaveChangesExceptionHandler<BookDbContext>(
    BookWithEventsConcurrencyHandler.HandleCacheValuesConcurrency);

services.RegisterGenericEventRunner(
    eventConfig,                //Adds configuration to GenericEventRunner
    Assembly.GetAssembly(typeof(ReviewAddedHandler)),
    Assembly.GetAssembly(typeof(CosmasUpdaterHandler))
    );

Clone this wiki locally