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
}
}
}