EndpointProviders 1.2.5
See the version list below for details.
dotnet add package EndpointProviders --version 1.2.5
NuGet\Install-Package EndpointProviders -Version 1.2.5
<PackageReference Include="EndpointProviders" Version="1.2.5" />
<PackageVersion Include="EndpointProviders" Version="1.2.5" />
<PackageReference Include="EndpointProviders" />
paket add EndpointProviders --version 1.2.5
#r "nuget: EndpointProviders, 1.2.5"
#:package EndpointProviders@1.2.5
#addin nuget:?package=EndpointProviders&version=1.2.5
#tool nuget:?package=EndpointProviders&version=1.2.5
EndpointProviders
The simplest way to dynamically add endpoints in a Minimal API, leveraging Dependency Injection principles.
Why another one?
Most libraries targeting Minimal API functionality rely on class instances of classes that can dynamically add Endpoints using a "marker" interface. But why is it not fully practical? Let's explore an example below.
public class SampleWithEndPoints : IMarker
{
public void AddEndpoints (WebApplication app)
{
app.MapGet("/api/...", Handler1);
app.MapGet("/api/...", Handler2);
app.MapGet("/api/...", Handler3);
app.MapPost("/api/...", Handler4);
...
}
//c'mon it's not practical to add Dependency Injection for each method
//wouldn't it be nice if we added the IRepository repo in the constructor of the class and make all these handlers not static?
public static IResult Handler1(IRepository repo, int id) => {... _repo.DoSth(id); ...}
public static IResult Handler2(IRepository repo, int id) => {...}
public static IResult Handler3(IRepository repo, int id) => {...}
public static IResult Handler4(IRepository repo, int id) => {...}
}
...
//and at some point register all endpoints via a method which scans all the IMarker classes in the assembly/app.
app.RegisterEndPoints(typeof(App));
What annoys me is that the role of the IMarker interface in the example above is limited to automatic registering all endpoints.
A more practical approach would be to ALSO use the IMarker to enable Dependency Injection within the SampleWithEndPoints class, avoiding the need to repeatedly declare IRepository repo for each handler.
The solution: EndpointProviders!
How to install
Via the Package Manager:
Install-Package EndpointsProviders
Via the .NET CLI:
dotnet add package EndpointsProviders
How to use
For this library, every class whose instance we want to use for adding endpoints must derive from the EndpointProvider abstract class.
The EndpointProvider itself implements the IEndpointProvider interface.
Each class derived from EndpointProvider is required to override the AddEndpoints method. The example above can now be rewritten as:
public class SampleWithEndPoints : EndpointProvider
{
readonly IRepository _repo;
//Each EndpointProvider should have the constructor below.
//The provider can then be used to create other instances via Dependency Injection in the constructor.
public SampleWithEndPoints(IServiceProvider provider) : base(provider)
{
repo = provider.GetService<IRepository>();
}
public override WebApplication AddEndpoints (WebApplication app)
{
app.MapGet("/api/...", Handler1);
app.MapGet("/api/...", Handler2);
app.MapGet("/api/...", Handler3);
...
return app;
}
//we can now use every instance that we created, without having to inject the IRepository for each handler!
//and the handlers are not static
public IResult Handler1(int id) => { ... _repo.DoSth(id); ...}
public IResult Handler2(int id) => {...}
public IResult Handler3(int id) => {...}
}
To register all endpoints from classes with the IEndpointProvider interface, use AddEndpointProviderFactory before building the app and AddEndpointsFromEndpointProviders after building the app:
using EndpointProviders;
...
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// 1st step: call AddEndpointProviderFactory before building the app
builder.Services.AddEndpointProviderFactory();
// 2nd step: register your dependencies
builder.Services.AddScoped<WeatherForecastRepository>();
...
WebApplication app = builder.Build();
// 3rd step: call AddEndpointsFromEndpointProviders after building the app
// The method accepts any class that can identify its parent Assembly
// In the example below, the WeatherForecastEndpoints identifies the assembly
app.AddEndpointsFromEndpointProviders(typeof(WeatherForecastEndpoints));
app.Run();
Key Points:
- The
AddEndpointProviderFactorymethod registers the factory service in the dependency injection container. - The
AddEndpointsFromEndpointProvidersmethod collects and initializes allIEndpointProviderobjects by injecting theIServiceProviderinto their constructors. - You can pass multiple marker types to
AddEndpointsFromEndpointProvidersto scan multiple assemblies:app.AddEndpointsFromEndpointProviders(typeof(MarkerClass1), typeof(MarkerClass2));
Example 1 - Simple example
Let's modify the classic WeatherForecast sample Minimal API project, to provide an explicit example.
We slightly modify the WeatherForecast class to a struct, as shown below (there is no particular reason for this, just my preference):
namespace EndpointProviderTests;
public readonly struct WeatherForecast
{
public DateOnly Date { get; init; }
public int TemperatureC { get; init; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; init; }
}
Now, let's build a repository that retrieves WeatherForecast instances:
namespace EndpointProviderTests;
public class WeatherForecastRepository
{
string[] _summaries = new[] {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public List<WeatherForecast> GetNext(int count) =>
Enumerable.
Range(1, count).
Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = _summaries[Random.Shared.Next(_summaries.Length)]
}).
ToList();
}
Now, let's add the EndpointProvider that will contain the endpoints to be added to the web app. Note that we include a constructor that accepts an IServiceProvider argument.
Through the passed provider, we inject the repository, which can now be used by any handler (in this case, we have only one).
using EndpointProviders;
namespace EndpointProviderTests;
public class WeatherForecastEndpoints : EndpointProvider
{
readonly WeatherForecastRepository _repo;
public WeatherForecastEndpoints(IServiceProvider provider) : base(provider)
{
_repo = provider.GetRequiredService<WeatherForecastRepository>();
}
public override WebApplication AddEndpoints(WebApplication app)
{
app.MapGet("/weatherforecast", ForecastHandler)
.WithName("GetWeatherForecast")
.WithOpenApi();
return app;
}
// Note: we do not need to pass the repo as a parameter to the handler
// We use the instance field _repo instead
private IResult ForecastHandler(int count)
{
if (count <= 0)
return Results.BadRequest("Count must be greater than 0");
// We use the repository instance to get the forecasts
var forecasts = _repo.GetNext(count);
return Results.Ok(forecasts);
}
}
Program.cs Setup
In your Program.cs, register the repository and add endpoints:
using EndpointProviders;
using EndpointProviderTests;
var builder = WebApplication.CreateBuilder(args);
// Add services
builder.Services.AddEndpointProviderFactory();
builder.Services.AddScoped<WeatherForecastRepository>();
var app = builder.Build();
// Add middleware and endpoints
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// Register all endpoints from providers in the current assembly
app.AddEndpointsFromEndpointProviders(typeof(Program));
app.Run();
Multiple Endpoint Providers Example
You can have multiple endpoint provider classes, each managing different API routes:
public class ProductsEndpoints : EndpointProvider
{
readonly IProductService _productService;
public ProductsEndpoints(IServiceProvider provider) : base(provider)
{
_productService = provider.GetRequiredService<IProductService>();
}
public override WebApplication AddEndpoints(WebApplication app)
{
app.MapGet("/api/products", GetProducts);
app.MapGet("/api/products/{id}", GetProductById);
app.MapPost("/api/products", CreateProduct);
return app;
}
private IResult GetProducts() => Results.Ok(_productService.GetAll());
private IResult GetProductById(int id) =>
Results.Ok(_productService.GetById(id));
private IResult CreateProduct(CreateProductDto dto) =>
Results.Created($"/api/products/{dto.Id}", _productService.Create(dto));
}
public class OrdersEndpoints : EndpointProvider
{
readonly IOrderService _orderService;
public OrdersEndpoints(IServiceProvider provider) : base(provider)
{
_orderService = provider.GetRequiredService<IOrderService>();
}
public override WebApplication AddEndpoints(WebApplication app)
{
app.MapGet("/api/orders", GetOrders);
app.MapGet("/api/orders/{id}", GetOrderById);
app.MapPost("/api/orders", CreateOrder);
return app;
}
private IResult GetOrders() => Results.Ok(_orderService.GetAll());
private IResult GetOrderById(int id) =>
Results.Ok(_orderService.GetById(id));
private IResult CreateOrder(CreateOrderDto dto) =>
Results.Created($"/api/orders/{dto.Id}", _orderService.Create(dto));
}
Both endpoint providers will be automatically discovered and registered when you call:
app.AddEndpointsFromEndpointProviders(typeof(Program));
Benefits
✅ Clean Separation of Concerns - Each endpoint provider handles a specific domain
✅ Dependency Injection - Services are injected via the constructor, not repeated in each handler
✅ Automatic Discovery - All endpoint providers are automatically found and registered
✅ Type-Safe - Full IntelliSense support for dependency injection
✅ Scalable - Add new endpoint providers without modifying existing code
Requirements
- .NET 6 or higher
- ASP.NET Core with Minimal APIs support
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- FluentValidation (>= 12.1.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
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 |
|---|---|---|
| 1.2.6 | 100 | 7/19/2026 |
| 1.2.5 | 98 | 7/15/2026 |
| 1.2.4 | 345 | 12/16/2024 |
| 1.2.3 | 301 | 12/7/2024 |
| 1.2.2 | 661 | 4/19/2024 |
| 1.2.1 | 272 | 2/15/2024 |
| 1.2.0 | 303 | 7/28/2023 |
| 1.1.1 | 325 | 7/14/2023 |
| 1.1.0 | 294 | 7/14/2023 |
| 1.0.8 | 297 | 7/6/2023 |
| 1.0.7 | 293 | 7/6/2023 |
| 1.0.6 | 296 | 6/29/2023 |
| 1.0.5 | 264 | 6/28/2023 |
| 1.0.2 | 280 | 6/27/2023 |
| 1.0.1 | 277 | 6/27/2023 |