1

Building a test app with .NET 7 minimal api swagger for learning purposes. Mostly a default project at this point. What has me confused is if I convert what you see below to not be async then Swagger properly generates/discovers the facilities endpoint, but as soon as I make it async it no longer does it. To be clear the endpoint still works but I'd like for swagger to work. I've tried various attempts at defining name/summary/description myself but it never seems to work. I have a feeling it's just never being discovered by swagger in the first place.

Here is my Program.cs:

using Infrastructure.Modules;
using Infrastructure.Services.Facilities;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "Producer API", Version = "v1" });
});
builder.Services.AddScoped<IFacilityService, TestFacilityService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Producer API v1");
    });
}

app.UseHttpsRedirection();

using var scope = app.Services.CreateScope();

var facilityService = scope.ServiceProvider.GetRequiredService<IFacilityService>();

app.RegisterFacilityEndpoints(facilityService);

app.Run();

and here is the FacilityModule.cs:

using Infrastructure.Services.Facilities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System.Text.Json;

namespace Infrastructure.Modules
{
    public static class FacilityModule
    {
        public static void RegisterFacilityEndpoints(this IEndpointRouteBuilder endpoints, IFacilityService facilityService)
        {
            endpoints.MapGet("/facilities", async context =>
                {
                    var facilities = await facilityService.GetFacilitiesAsync();
                    var json = JsonSerializer.Serialize(facilities);
                    context.Response.ContentType = "application/json";
                    await context.Response.WriteAsync(json);
                })
                .WithOpenApi(operation => new(operation)
                {
                    Summary = "This is a summary",
                    Description = "This is a description",
                });

            // Add more endpoints as needed
        }
    }
}
FrankTheDank
  • 197
  • 1
  • 2
  • 9
  • 1
    [This](https://github.com/dotnet/aspnetcore/issues/39766) seems to describe also your problem. Looks like to be the case when using the overload for the delgate with only the httpcontext. It reads as going for `async (response,context) => etc` should work around that even if you don't need the HttpResponse as input. – Ralf Jun 14 '23 at 18:34
  • Thank you so much! I knew it was something tiny. More specifically I had to fully specify async (HttpResponse response,HttpContext context) – FrankTheDank Jun 14 '23 at 19:38
  • Check out also [this](https://stackoverflow.com/a/74274426/2501279) and [this](https://stackoverflow.com/a/70971804/2501279) answers. – Guru Stron Jun 19 '23 at 11:33

0 Answers0