Skip to content

Kahbazi/MediatR.AspNetCore.Endpoints

Repository files navigation

MediatR.AspNetCore.Endpoints

NuGet

Getting Started

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMediatR(GetType().Assembly);
        services.AddMediatREndpoints();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapMediatR();
        });
    }
}

RequestHandler

public class SampleRequestHandler : IRequestHandler<SampleRequest, SampleResponse>
{
    [HttpPost("SampleRequest")]
    public async Task<SampleResponse> Handle(SampleRequest request, CancellationToken cancellationToken)
    {
        await Task.Delay(200);

        return new SampleResponse
        {
            Name = "Kahbazi",
            Timestamp = DateTime.Now
        };
    }
}

Request

public class SampleRequest : IRequest<SampleResponse>
{
    public int Id { get; set; }
}

Response

public class SampleResponse
{
    public string Name { get; set; }

    public DateTime Timestamp { get; set; }
}

Bechmark for a simple http request

Request:

{
  "Id" : 47,
  "Name" : "kahbazi"
}

Response:

{
  "Message" : "Hello kahbazi"
}
Method Mean Error StdDev Rank Gen 0 Gen 1 Gen 2 Allocated
MediatR.AspNetCore.Endpoints 56.14 us 1.427 us 2.462 us 1 2.9297 - - 9.18 KB
Microsoft.AspNetCore.Mvc 151.90 us 2.980 us 3.060 us 2 4.8828 - - 16.29 KB