DependencyInjection.SourceGenerator.Microsoft 3.0.0

dotnet add package DependencyInjection.SourceGenerator.Microsoft --version 3.0.0
                    
NuGet\Install-Package DependencyInjection.SourceGenerator.Microsoft -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DependencyInjection.SourceGenerator.Microsoft" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DependencyInjection.SourceGenerator.Microsoft" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DependencyInjection.SourceGenerator.Microsoft">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DependencyInjection.SourceGenerator.Microsoft --version 3.0.0
                    
#r "nuget: DependencyInjection.SourceGenerator.Microsoft, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DependencyInjection.SourceGenerator.Microsoft@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=3.0.0
                    
Install as a Cake Tool

DependencyInjection.SourceGenerator.Microsoft

Register services using attributes instead of registering in code.

This library simplifies dependency injection in .NET applications by moving service registration from startup code to the service classes themselves using attributes. This approach provides compile-time validation, reduces boilerplate code, and keeps service registration logic next to the implementation where it belongs. With features like automatic registration of interface implementations, decorator pattern support, and factory registration, it helps maintain clean and maintainable dependency injection configuration, especially in larger applications.

Usage

Add the "Register" attribute to the class you want to register. The attribute takes a type and a lifetime. The type is the type you want to register and the lifetime is the lifetime of the service. The lifetime is optional and defaults to Transient.

This library supports the following dependency injection frameworks, follow the links for more information on how to use them:

To use this library you need to install the source generator package and the contracts package. The source generator package is a development dependency and will not be exposed as a dependency to consumers of your projects, while the contracts package contains the attributes and enums used to configure the generator.

Note: The DependencyInjection.SourceGenerator.Microsoft.Contracts package has been deprecated. Please refer to the Migration Guide for more information.

Microsoft.Extensions.DependencyInjection

  • DependencyInjection.SourceGenerator.Microsoft NuGet
  • DependencyInjection.SourceGenerator.Microsoft.Contracts NuGet
namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public interface IAnotherService
{
	string GetAnother();
}

[Register(ServiceName = "ServiceName", Lifetime = Lifetime.Singleton)]
public class ExampleService : IExampleService
{
	public string GetExample()
	{
		return "Example";
	}
}

[Decorate]
public class KeyedService : IExampleService
{
	public string GetExample()
	{
		return "Keyed";
	}
}

[Decorator]
public class ServiceDecorator : IExampleService
{
	private readonly IExampleService _exampleService;

	public ServiceDecorator(IExampleService exampleService)
	{
		_exampleService = exampleService;
	}

	public string GetExample()
	{
		return _exampleService.GetExample();
	}
}

[Register<IAnotherService>]
public class MultipleInterfacesService : IExampleService, IAnotherService
{
	public string GetExample()
	{
		return "MultipleInterfaces";
	}

	public string GetAnother()
	{
		return "Another";
	}
}

Generates a class ServiceCollectionExtensions Assuming the project is named MyProject, the generated method will be named AddMyProject.

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace RootNamespace;
using global::Microsoft.Extensions.DependencyInjection;

[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static class ServiceCollectionExtensions
{
    public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddKeyedSingleton<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService>("ServiceName");
        services.Decorate<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ServiceDecorator>();
        services.AddTransient<global::RootNamespace.Services.IAnotherService, global::RootNamespace.Services.MultipleInterfacesService>();
        return services;
    }
}

You can then use the generated extension method as follows:

var services = new ServiceCollection();
services.AddMyProject();

for AspNetCore web APIs:

public void ConfigureServices(IServiceCollection services)
{
	services.AddMyProject();
}

and for minimal APIs:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMyProject();

Register all services in the project

You can also register all services in a project by adding the RegisterAll attribute to the assembly. This will register all implementations of the specified interface or base type.

using DependencyInjection.SourceGenerator.Contracts.Attributes;

[assembly: RegisterAll<IExampleService>]

namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public class ExampleService1 : IExampleService
{
	public string GetExample()
	{
		return "Example 1";
	}
}

public class ExampleService2 : IExampleService
{
	public string GetExample()
	{
		return "Example 2";
	}
}

this will generate the following code:

public static class ServiceCollectionExtensions
{
	public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddTestProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
	{
		services.AddTransient<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService1>();
		services.AddTransient<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService2>();
		return services;
	}
}

Lifetime

The Lifetime is an enum with the following values:

  • Transient
  • Scoped
  • Singleton

IncludeFactory

The IncludeFactory property allows you to register a service as a Func<TService>, enabling you to inject a factory method into your constructors. This is useful when you need to create multiple instances of a service on demand.

Example

namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

[Register(IncludeFactory = true)]
public class ExampleService : IExampleService
{
	public string GetExample()
	{
		return "Example";
	}
}

This will generate the following code:

public static class ServiceCollectionExtensions
{
	public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
	{
		services.AddTransient<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService>();
		services.AddTransient<global::System.Func<global::RootNamespace.Services.IExampleService>>(provider => () => provider.GetRequiredService<global::RootNamespace.Services.IExampleService>());
		return services;
	}
}

You can then inject Func<IExampleService> into your constructors:

public class Consumer
{
	private readonly Func<IExampleService> _exampleServiceFactory;

	public Consumer(Func<IExampleService> exampleServiceFactory)
	{
		_exampleServiceFactory = exampleServiceFactory;
	}

	public void UseService()
	{
		var serviceInstance = _exampleServiceFactory();
		// Use the service instance
	}
}

RegisterAll with IncludeFactory

You can also use IncludeFactory with the RegisterAll attribute to register all implementations of a specified type along with their factory methods.

using DependencyInjection.SourceGenerator.Contracts.Attributes;

[assembly: RegisterAll<IExampleService>(IncludeFactory = true)]

namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public class ExampleService1 : IExampleService
{
	public string GetExample()
	{
		return "Example 1";
	}
}

public class ExampleService2 : IExampleService
{
	public string GetExample()
	{
		return "Example 2";
	}
}

This will generate the following code:

public static class ServiceCollectionExtensions
{
	public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
	{
		services.AddTransient<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService1>();
		services.AddTransient<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService2>();
		services.AddTransient<global::System.Func<global::RootNamespace.Services.IExampleService>>(provider => () => provider.GetRequiredService<global::RootNamespace.Services.IExampleService>());
		return services;
	}
}

You can then inject Func<IExampleService> into your constructors as shown in the previous example.

Method Registrations

You can also register services using static methods. The method must be static, public or internal, and must have a single parameter of type IServiceProvider or IServiceCollection.

Example with IServiceProvider

namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public class ExampleService : IExampleService
{
	public string GetExample()
	{
		return "Example";
	}
}

public static class ServiceRegistrations
{
	[Register]
	public static IExampleService RegisterExampleService(IServiceProvider services)
	{
		return new ExampleService();
	}
}

This will generate the following code:

public static class ServiceCollectionExtensions
{
	public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
	{
		services.AddTransient<global::RootNamespace.Services.IExampleService>(global::RootNamespace.Services.ServiceRegistrations.RegisterExampleService);
		return services;
	}
}

Example with IServiceCollection

namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public class ExampleService : IExampleService
{
	public string GetExample()
	{
		return "Example";
	}
}

public static class ServiceRegistrations
{
	[Register]
	public static void RegisterExampleService(IServiceCollection services)
	{
		services.AddTransient<IExampleService, ExampleService>();
	}
}

This will generate the following code:

public static class ServiceCollectionExtensions
{
	public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
	{
		global::RootNamespace.Services.ServiceRegistrations.RegisterExampleService(services);
		return services;
	}
}

You can then use the generated extension method as shown in the previous examples.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 132 8/13/2025
3.0.0-alpha9 118 1/23/2025
3.0.0-alpha8 161 1/21/2025
3.0.0-alpha7 101 1/21/2025
3.0.0-alpha6 113 1/21/2025
3.0.0-alpha5 102 1/20/2025
3.0.0-alpha4 95 1/20/2025
3.0.0-alpha3 107 1/20/2025
3.0.0-alpha2 105 1/20/2025
3.0.0-alpha11 140 1/23/2025
3.0.0-alpha1 109 1/19/2025
2.0.0 210 8/30/2024
1.6.0 232 2/14/2024
1.5.1 238 1/15/2024
1.5.0 168 1/15/2024
1.4.3 234 1/10/2024
1.4.2 200 1/10/2024
1.4.1 227 1/9/2024
1.4.0 198 1/9/2024
1.3.3 215 1/8/2024
1.3.2 209 1/8/2024
1.3.1 197 1/8/2024
1.3.0 189 1/8/2024
1.2.2 181 1/8/2024
1.2.2-alpha3 195 1/8/2024
1.2.2-alpha2 165 1/8/2024
1.2.1 214 1/5/2024
1.2.1-alpha6 216 1/5/2024
1.2.1-alpha5 223 1/5/2024
1.2.1-alpha2 216 1/5/2024
1.2.1-alpha1 210 1/5/2024
1.2.0 213 1/5/2024
1.1.2 223 1/5/2024
1.1.1 192 1/5/2024
1.1.0 182 1/5/2024
1.0.2 214 1/5/2024
1.0.1 228 1/4/2024
1.0.0 231 1/4/2024