Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal API Support #653

Closed
mumby0168 opened this issue Aug 12, 2021 · 11 comments
Closed

Minimal API Support #653

mumby0168 opened this issue Aug 12, 2021 · 11 comments

Comments

@mumby0168
Copy link

Seeing the recent advancements on the minimal APIs within ASP.NET Core.

It would be really cool if we could look at some extension methods that sit on top of this and provide a way to map IRequest<T> handlers directly to http routes without having to use a controller.

I expect this would be a set of extension methods.

@elementh
Copy link

That would be very cool, but my opinion is that it should be in the form of an extension nuget package, maybe?

@mumby0168
Copy link
Author

mumby0168 commented Aug 12, 2021

Yeah something like MediatR.Extensions.AspNetCore.Http ?

I would love to get cracking with this just not sure what repository it would live under?

@mumby0168
Copy link
Author

I started to put together a project to scope this out you can see it here https://github.com/mumby0168/blog-samples/tree/main/new-features/MinimalApis

While doing so I came across a current limitation of the minimal API's I have logged in issue about it here: dotnet/aspnetcore#35304

@mumby0168
Copy link
Author

@jbogard what do you think about this? They have recently added a more flexible way to allow custom parameters to be bound. See the previous comment with a link to the issue.

@Kahbazi
Copy link
Contributor

Kahbazi commented Sep 6, 2021

Yeah something like MediatR.Extensions.AspNetCore.Http ?

@mumby0168 This is what I have developed in https://github.com/Kahbazi/MediatR.AspNetCore.Endpoints

You can register IRequest<T> directly to the ASP.NET Core routing without Controllers.

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();
        });
    }
}

@mumby0168
Copy link
Author

Yeah something like MediatR.Extensions.AspNetCore.Http ?

@mumby0168 This is what I have developed in https://github.com/Kahbazi/MediatR.AspNetCore.Endpoints

You can register IRequest<T> directly to the ASP.NET Core routing without Controllers.

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();
        });
    }
}

Ah that is really cool, how do you handle mapping query params to an object?

@bobbyangers
Copy link

bobbyangers commented Sep 6, 2021

Yeah that us pretty cool. And how would you apply security requirements, on the IRequest with a custom filter ?

@Kahbazi
Copy link
Contributor

Kahbazi commented Sep 6, 2021

Ah that is really cool, how do you handle mapping query params to an object?

I haven't implement that yet.

Yeah that us pretty cool. And how would you apply security requirements, on the IRequest with a custom filter ?

You can apply [Authorize] attributes the same way as controllers and it will work as long as you also add authorization middleware.

@srollinet
Copy link

srollinet commented Oct 2, 2021

I try to do something similar, with extension methods that allow to map a route to a an IRequest.

Something like that

app.MediatR().MapGet("/hello", (string name) => new HelloQuery(name));

Which will be the equivalent of

app.MapGet("/hello", (string name, IMediator mediator, CancellationToken token) =>
     mediator.Send(new HelloQuery(name), token)
);

The idea is to have something similar as the original API, with less boilerplate.

The problem is that I don't know the correct approach to use... I tried to create a dynamic delegate that wrap the original one and that adds the mediator and the token, but I don't think it is a good approach, and I have a lack of knowledge in this domain so I struggled with lot of problems.

Maybe the most simple solution is to have a middleware that handles the IRequest, but with this solution, I don't know how Swagger will know the actual return type of the method. I will try to dig a bit.

@vip32
Copy link

vip32 commented Jul 27, 2022

@srollinet did you make any progress?

@srollinet
Copy link

srollinet commented Aug 8, 2022

@vip32 No, I haven't done anything. And I will probably wait for asp.net core 7 because the new [AsParameters] attribute will simplify a lot what I want to achieve

https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-5/

@jbogard jbogard closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants