NetCore.AutoRegisterDi 1.0.0

Extension method to find/register classes in an assembly into Microsoft.Extensions.DependencyInjection

There is a newer version of this package available.
See the version list below for details.
Install-Package NetCore.AutoRegisterDi -Version 1.0.0
dotnet add package NetCore.AutoRegisterDi --version 1.0.0
<PackageReference Include="NetCore.AutoRegisterDi" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetCore.AutoRegisterDi --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetCore.AutoRegisterDi, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetCore.AutoRegisterDi as a Cake Addin
#addin nuget:?package=NetCore.AutoRegisterDi&version=1.0.0

// Install NetCore.AutoRegisterDi as a Cake Tool
#tool nuget:?package=NetCore.AutoRegisterDi&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

NetCore.AutoRegisterDi

This NuGet library contains an extension method to scan an assemby and register all the public classes against their implemented interfaces(s) into the Microsoft.Extensions.DependencyInjection dependency injection provider.

I have written a simple version of AutoFac's RegisterAssemblyTypes method that works directly with Microsoft's DI provider. Here is an example of me using this with ASP.NET Core

public void ConfigureServices(IServiceCollection services)
{
   //... other configure code removed

   var assemblyToScan = Assembly.GetExecutingAssembly(); //..or whatever assembly you need

   service.RegisterAssemblyPublicNonGenericClasses(assemblyToScan)
     .Where(c => c.Name.EndsWith("Service"))
     .AsPublicImplementedInterfaces();

Licence: MIT.

Why have I written this extension?

There are two reasons:

  1. I really hate having to hand-code each registering of the services - this extension method scans assembles and finds/registers classes with interfaces for you.
  2. I used to use AutoFac's assembly scanning feature, but I then saw a tweet by @davidfowl about [Dependency Injection container benchmarks](https://ipjohnson.github.io /DotNet.DependencyInjectionBenchmarks/) which showed the Microsoft's DI provider was much faster than AutoFac. I therefore implemented a similar (but not exactly the same) feature for the Microsoft.Extensions.DependencyInjection library.

Thanks to Inventory Innovations, Inc. who sponsored the creation of this library.

Detailed information

There are three parts:

  1. RegisterAssemblyPublicNonGenericClasses, which finds all the classes.
  2. An options Where method, which allows you to filter the classes to be considered.
  3. The AsPublicImplementedInterfaces method which finds ant interfaces on a class and registers those interfaces as pointing to the class.
1. The RegisterAssemblyPublicNonGenericClasses method

The RegisterAssemblyPublicNonGenericClasses method will find all the classes in the assembly that I referenced that are considered useful for registering. The exact criteria I use are:

  • Public access
  • Not nested, e.g. It won't look at classes defined inside other classes
  • Not Generic, e.g. MyClass<T>
  • Not Abstract

The method takes a list/array of assemblies to scan. Two typical ways of providing an assembly are:

  • Assembly.GetExecutingAssembly(), which does what is says
  • Assembly.GetAssembly(typeof(YourClass)), which gets the assembly that YourClass was defined in.
2. The Where method

Pretty straightforward - you are provided with the Type of each class and you can filter by any of the Type properties etc. This allows you to do things like only registering certain classes,
e.g Where(c => c.Name.EndsWith("Service"))

NOTE: Useful also if you want to register some classes with a different lifetime scope.

3. The AsPublicImplementedInterfaces method

The AsPublicImplementedInterfaces method finds any public, non-nested interfaces (apart from IDisposable) that each class implements and registers each interface, known as service type, against the class, known as the implementation type.

By default it will register the classes as having a lifetime of ServiceLifetime.Transient, but there is a parameter that allows you to override that.

See this useful article on what lifetime (and other terms) means.

NetCore.AutoRegisterDi

This NuGet library contains an extension method to scan an assemby and register all the public classes against their implemented interfaces(s) into the Microsoft.Extensions.DependencyInjection dependency injection provider.

I have written a simple version of AutoFac's RegisterAssemblyTypes method that works directly with Microsoft's DI provider. Here is an example of me using this with ASP.NET Core

public void ConfigureServices(IServiceCollection services)
{
   //... other configure code removed

   var assemblyToScan = Assembly.GetExecutingAssembly(); //..or whatever assembly you need

   service.RegisterAssemblyPublicNonGenericClasses(assemblyToScan)
     .Where(c => c.Name.EndsWith("Service"))
     .AsPublicImplementedInterfaces();

Licence: MIT.

Why have I written this extension?

There are two reasons:

  1. I really hate having to hand-code each registering of the services - this extension method scans assembles and finds/registers classes with interfaces for you.
  2. I used to use AutoFac's assembly scanning feature, but I then saw a tweet by @davidfowl about [Dependency Injection container benchmarks](https://ipjohnson.github.io /DotNet.DependencyInjectionBenchmarks/) which showed the Microsoft's DI provider was much faster than AutoFac. I therefore implemented a similar (but not exactly the same) feature for the Microsoft.Extensions.DependencyInjection library.

Thanks to Inventory Innovations, Inc. who sponsored the creation of this library.

Detailed information

There are three parts:

  1. RegisterAssemblyPublicNonGenericClasses, which finds all the classes.
  2. An options Where method, which allows you to filter the classes to be considered.
  3. The AsPublicImplementedInterfaces method which finds ant interfaces on a class and registers those interfaces as pointing to the class.
1. The RegisterAssemblyPublicNonGenericClasses method

The RegisterAssemblyPublicNonGenericClasses method will find all the classes in the assembly that I referenced that are considered useful for registering. The exact criteria I use are:

  • Public access
  • Not nested, e.g. It won't look at classes defined inside other classes
  • Not Generic, e.g. MyClass<T>
  • Not Abstract

The method takes a list/array of assemblies to scan. Two typical ways of providing an assembly are:

  • Assembly.GetExecutingAssembly(), which does what is says
  • Assembly.GetAssembly(typeof(YourClass)), which gets the assembly that YourClass was defined in.
2. The Where method

Pretty straightforward - you are provided with the Type of each class and you can filter by any of the Type properties etc. This allows you to do things like only registering certain classes,
e.g Where(c => c.Name.EndsWith("Service"))

NOTE: Useful also if you want to register some classes with a different lifetime scope.

3. The AsPublicImplementedInterfaces method

The AsPublicImplementedInterfaces method finds any public, non-nested interfaces (apart from IDisposable) that each class implements and registers each interface, known as service type, against the class, known as the implementation type.

By default it will register the classes as having a lifetime of ServiceLifetime.Transient, but there is a parameter that allows you to override that.

See this useful article on what lifetime (and other terms) means.

Release Notes

First release

NuGet packages (10)

Showing the top 5 NuGet packages that depend on NetCore.AutoRegisterDi:

Package Downloads
Relay.Microservices
A microservice framework built for .NET Core
Relay.Microservices.Testing
A microservice testing framework built for .NET Core
Relay.Core.Plugins.Microservices
Plugins available for the Core framework
Relay.Core.Microservices.Testing
A microservice testing framework built for .NET Core
Relay.Core.Microservices
A microservice framework built for .NET Core

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on NetCore.AutoRegisterDi:

Repository Stars
JonPSmith/PermissionAccessControl2
Version 2 of example application to go with articles on feature and data authorization
JonPSmith/EfCore.GenericBizRunner
Library to run business logic when using Entity Framework Core for database accesses
JonPSmith/EfCoreinAction-SecondEdition
Supporting repo to go with book "Entity Framework Core in Action", second edition

Version History

Version Downloads Last updated
2.1.0 85,401 10/6/2020
2.0.0 100,915 5/9/2020
1.1.0 272,322 8/26/2019
1.0.0 136,167 6/16/2018