![]() |
VOOZH | about |
dotnet add package AutoRegisterInject --version 2.0.2
NuGet\Install-Package AutoRegisterInject -Version 2.0.2
<PackageReference Include="AutoRegisterInject" Version="2.0.2" />
<PackageVersion Include="AutoRegisterInject" Version="2.0.2" />Directory.Packages.props
<PackageReference Include="AutoRegisterInject" />Project file
paket add AutoRegisterInject --version 2.0.2
#r "nuget: AutoRegisterInject, 2.0.2"
#:package AutoRegisterInject@2.0.2
#addin nuget:?package=AutoRegisterInject&version=2.0.2Install as a Cake Addin
#tool nuget:?package=AutoRegisterInject&version=2.0.2Install as a Cake Tool
<img src="https://github.com/patrickklaeren/AutoRegisterInject/blob/main/Icon.png?raw=true" alt="Responsive Image" style="width:40%; height:auto;">
AutoRegisterInject, also referred to as ARI, is a C# source generator that will automatically create Microsoft.Extensions.DependencyInjection registrations for types marked with attributes.
This is a compile time alternative to reflection/assembly scanning for your injections or manually adding to the ServiceCollection every time a new type needs to be registered.
For example:
namespace MyProject;
[RegisterScoped]
public class Foo { }
will automatically generate an extension method called AutoRegister() for IServiceProvider, that registers Foo, as scoped.
internal IServiceCollection AutoRegister(this IServiceCollection serviceCollection)
{
serviceCollection.AddScoped<Foo>();
return serviceCollection;
}
In larger projects, dependency injection registration becomes tedious and in team situations can lead to merge conflicts which can be easily avoided.
AutoRegisterInject moves the responsibility of service registration to the owning type rather than external service collection configuration, giving control and oversight of the type that is going to be registered with the container.
Install the Nuget package, and start decorating classes with ARI attributes.
Use dotnet add package AutoRegisterInject or add a package reference manually:
<PackageReference Include="AutoRegisterInject" />
Classes should be decorated with one of four attributes:
[RegisterScoped][RegisterSingleton][RegisterTransient][RegisterHostedService]Variants for keyed and the service Try register pattern are also available:
[TryRegisterScoped][TryRegisterSingleton][TryRegisterTransient][RegisterKeyedScoped][RegisterKeyedSingleton][RegisterKeyedTransient]Each keyed attribute has a Try counterpart.
Register a class:
[RegisterScoped]
class Foo;
and get the following output:
serviceCollection.AddScoped<Foo>();
Update the service collection by invoking:
var serviceCollection = new ServiceCollection();
serviceCollection.AutoRegister();
serviceCollection.BuildServiceProvider();
You can now inject Foo as a dependency and have this resolved as scoped.
Alternatively, you can register hosted services by:
[RegisterHostedService]
class Foo;
and get:
serviceCollection.AddHostedService<Foo>();
Implement one or many interfaces on your target class:
[RegisterTransient]
class Bar : IBar;
and get the following output:
serviceCollection.AddTransient<IBar, Bar>();
Important note: AutoRegisterInject is opinionated and Bar will only be registered with its implemented interface. ARI will not register Bar. Bar will always need to be resolved from IBar in your code.
Implementing multiple interfaces will have the implementing type be registered for each distinct interface.
[RegisterTransient]
class Bar : IBar, IFoo, IBaz;
will output the following:
serviceCollection.AddTransient<IBar, Bar>();
serviceCollection.AddTransient<IFoo, Bar>();
serviceCollection.AddTransient<IBaz, Bar>();
Important note: AutoRegisterInject is opinionated and Bar will only be registered with its implemented interfaces. ARI will not register Bar. Bar will always need to be resolved from IBar, IFoo or IBaz in your code.
ARI can also generate an interface for a type by decorating it with [AutoInterface] and declaring the interface on the class:
[AutoInterface]
[RegisterScoped]
public class Foo : IFoo
{
public string Name { get; set; }
public void Run() { }
}
will generate:
public interface IFoo
{
string Name { get; set; }
void Run();
}
and because Foo implements IFoo, ARI will register it as an interface registration:
serviceCollection.AddScoped<IFoo, Foo>();
This works with namespaced types and the standard registration attributes.
In addition to the AutoRegister extension method, every assembly that AutoRegisterInject is a part of, a AutoRegisterFromAssemblyName will be generated. This allows you to configure your service collection from one, main, executing assembly.
Given 3 assemblies, MyProject.Main, MyProject.Services, MyProject.Data, you can configure the ServiceCollection as such:
var serviceCollection = new ServiceCollection();
serviceCollection.AutoRegisterFromMyProjectMain();
serviceCollection.AutoRegisterFromMyProjectServices();
serviceCollection.AutoRegisterFromMyProjectData();
serviceCollection.BuildServiceProvider();
AutoRegisterInject will remove illegal characters from assembly names in order to generate legal C# method names. ,, . and will be removed.
By default ARI will register a type with all the interfaces it implements, however this excludes System.IDisposable and its IAsyncDisposable counterpart.
You can ignore interfaces by telling ARI to explicitly register with only declared interfaces in the given attributes:
public interface IFoo { }
public interface IBar { }
[RegisterScoped(typeof(IBar))]
public class Foo;
this will result in Foo ONLY being registered as IBar and the following output:
serviceCollection.AddTransient<IBar, Foo>();
IFoo will be ignored entirely.
Where you want to register as multiple interfaces, you can pass multiple types.
[RegisterScoped(typeof(IBar), typeof(IFoo))]
public class Foo;
This works for all applicable attributes.
AutoRegisterInject is MIT licensed. Do with it what you please under the terms of MIT.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on AutoRegisterInject:
| Package | Downloads |
|---|---|
|
GoLive.Blazor.Controls
Some random Blazor Web Controls. |
|
|
CastelloBranco.LocatorGenerator
Source Generator Library to AutoWrive ViewModels in Avalonia |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.2 | 217 | 6/15/2026 |
| 2.0.1 | 955 | 6/6/2026 |
| 2.0.0 | 108 | 6/6/2026 |
| 1.4.1 | 100,305 | 6/18/2024 |
| 1.4.0 | 10,435 | 4/22/2024 |
| 1.3.1 | 4,029 | 3/5/2024 |
| 1.3.0 | 824 | 3/2/2024 |
| 1.2.1 | 37,875 | 1/17/2023 |
| 1.2.0 | 412 | 1/17/2023 |
| 1.1.2 | 909 | 12/10/2022 |
| 1.1.1 | 433 | 12/10/2022 |
| 1.1.0 | 444 | 12/10/2022 |
| 1.0.1 | 460 | 12/10/2022 |
| 1.0.0 | 582 | 12/10/2022 |