Home

Awesome

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"
}
MethodMeanErrorStdDevRankGen 0Gen 1Gen 2Allocated
MediatR.AspNetCore.Endpoints56.14 us1.427 us2.462 us12.9297--9.18 KB
Microsoft.AspNetCore.Mvc151.90 us2.980 us3.060 us24.8828--16.29 KB