Genesis.MinimalApis
6.3.1
See the version list below for details.
dotnet add package Genesis.MinimalApis --version 6.3.1
NuGet\Install-Package Genesis.MinimalApis -Version 6.3.1
<PackageReference Include="Genesis.MinimalApis" Version="6.3.1" />
paket add Genesis.MinimalApis --version 6.3.1
#r "nuget: Genesis.MinimalApis, 6.3.1"
// Install Genesis.MinimalApis as a Cake Addin #addin nuget:?package=Genesis.MinimalApis&version=6.3.1 // Install Genesis.MinimalApis as a Cake Tool #tool nuget:?package=Genesis.MinimalApis&version=6.3.1
Minimal Endpoints Dependency Injection
Create a minimal endpoint from any function in any file.
Genesis.MinimalApis provides support for easily exposing minimal endpoints from any file.
Important!
MapPatch
andHttpPatchAttribute
are only supported in dotnet 7.0 and greater! UseMapPost
andHttpPostAttribute
in dotnet 6.0!Please reference MapPatch
Getting Started
Add the following package from nuget
dotnet add package Genesis.MinimalApis
Usage
Any function in pretty much any file can be exposed as a minimal endpoint via adding the IEndpoints
interface to a class or struct or by adding any HttpMethodAttribute
found in Microsoft.AspNetCore.Mvc
.
Full DI support is provided for endpoints registered either manually or by the use of attributes. Additional examples are provided under Samples.
Endpoints can be registered under any lifecycle.
- Transient
- Scoped
- Singleton
If registering a class that inherits from IEndpoints
helper dependency injection functions for services are provided.
AddTransientEndpoints<T>(this IServiceCollection services) where T : IEndpoint
AddScopedEndpoints<T>(this IServiceCollection services) where T : IEndpoint
AddSingletonEndpoints<T>(this IServiceCollection services) where T : IEndpoint
Structs that inherit from IEndpoints
may also be registered, though they require a but more setup.
RegisterEndpoints<T>(this IServiceCollection services, Func<ServiceProvider, object> func);
Static classes can be registered when using attributes found in Microsoft.AspNetCore.Mvc
.
Manual Definition
Endpoints can be defined and used in class files by using the IEndpoints
interface.
//. GreetingService.cs
internal sealed class GreetingService : IEndpoints {
//. function to expose as an endpoint
public string Greet(string name) =>
$"Hello {name}!";
//. function from interface to which will be used to register the app.
public void RegisterEndpoints(IEndpointRouteBuilder app) {
app.MapGet("greet/{name}", Greet)
};
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingletonEndpoints<GreetingService>();
var app = builder.Build();
//. Register endpoint GET /greet/{name} in GreetingService.cs
app.MapEndpoints<GreetingService>();
app.Run();
Attribute Definition
Endpoints can be exposed by using attributes found in Microsoft.AspNetCore.Mvc
and Microsoft.AspNetCore.Mvc.Routing
.
Supported Attributes:
//. GreetingService.cs
using Microsoft.AspNetCore.Mvc;
[Route("api")]
internal sealed class GreetingService {
[HttpGet("greet/{name}")]
public string Greet(string name) =>
$"Hello {name}!";
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<GreetingService>();
var app = builder.Build();
//. Map attributes in GreetingService to GET /api/Greet/{name}
app.MapEndpoints<GreetingService>();
app.Run();
Static Attribute Definition
Attributes can be used on static classes such that static functions can be registered as endpoint.
Only method injection will work using this method of registration as static classes cannot be instantiated.
//. GreetingFunctions.cs
using Microsoft.AspNetCore.Mvc;
[Route("api")]
internal static class GreetingFunctions {
[HttpGet("greet/{name}")]
public static string Greet(string name) =>
$"Hello {name}!";
}
//. Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
//. Map attributes in GreetingFunctions to GET /api/Greet/{name}
app.MapStaticEndpoints<GreetingService>();
app.Run();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Genesis.MinimalApis:
Package | Downloads |
---|---|
Genesis.LanguageExt.AspNetCore
Converts monad types from LanguageExt to AspNet Core Result types |
GitHub repositories
This package is not used by any popular GitHub repositories.