C
C#•3w ago
Abd@SDET@ML

API Swagger URL is not loading at all through .NET aspire 9.0

I've added Aspire 9.0 project to spin API swagger and SQL Server container. I'm able to spin SQL Server container, but not API Swagger URL. I've added project references of both API and database projects to the app host project. App host project > Program.cs file contains this code: var builder = DistributedApplication.CreateBuilder(args); //Database var sqlServer = builder.AddSqlServer("sql").AddDatabase("db"); builder.AddSqlProject("sqlproject").WithDacpac("O.WP.Database.dacpac").WithReference(sqlServer); //Api builder.AddProject<Projects.O_WP_PIT_ACT_API>("apiservice").WithReference(sqlServer); builder.Build().Run();
13 Replies
glhays
glhays•3w ago
Can we see your launch settings json file and your startup program.cs?
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
No description
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
No description
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
No description
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
anything you have noticed or found ?
glhays
glhays•3w ago
In a Visual Studio API project, the launchSettings.json file is typically located in the Properties folder of the project directory. You provided the appsettings.json. Program.cs file? Can you just give the code not screenshots.
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
Hi, yes got the launchsettings.json file and here is the code { "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "O.WP.API": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, /"launchUrl": "asset/windfarms",/ "launchUrl": "swagger", "applicationUrl": "http://localhost:8050/pit/act", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
glhays
glhays•3w ago
I assume then you have the program.cs file setup to use swagger? not going to ask again as it appears you have some reluctance about it. "applicationUrl": "http://localhost:8050/pit/act", here you have it pointed to a controller or something? something like so.
builder.Services.AddSwaggerGen();
builder.Services.AddAuthorization();
builder.Services.AddSignalR();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
}
builder.Services.AddSwaggerGen();
builder.Services.AddAuthorization();
builder.Services.AddSignalR();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
}
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
program.cs file has this code using O.WP.PIT.ACT.API.Builders; await WebApplication .CreateBuilder(args) .ExecuteAsync(); im not a developer, infact a quality test engineer, so i need to search things which you are asking for 😄 but i need to write integration tests using .net aspire 9.0 thats where i am right now, api swagger is not launching application url is not pointing to any controller
glhays
glhays•3w ago
Need to see the launch and program.cs for this project: O_WP_PIT_ACT_API; is this the Api you need to use with Swagger?
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
what do you suspect ? it is something configuration issues ? yes its the api i need to use with swagger also locally when i run api project, swagger is launching successfully its only with .net aspire, page is just loading and loading but not landing on endpoints page
glhays
glhays•3w ago
I suspect it is this "http://localhost:8050/pit/act" unless this is the same as in the Api project your stating does launch swagger. This url is pointing to a specific address that most likely knows nothing about Swagger's UI. I can only assume only you know your files. Question you stated your doing testing is this mimicking production? Integration is what you said maybe this might help
if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Aspire")
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API Name v1");
c.RoutePrefix = string.Empty;
});
}
if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Aspire")
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API Name v1");
c.RoutePrefix = string.Empty;
});
}
adjust to work in your environment ofcourse Swagger is usually only used in development environments
Abd@SDET@ML
Abd@SDET@MLOP•3w ago
magically i was able to fix the actual issue without adding above code this below method is called in program.cs, and i have commented builder.ConfigureKestrel(appConfig); there was a warning in console logs of aspire for the same which i ignored initially public static async Task ExecuteAsync(this WebApplicationBuilder builder, CancellationToken cancellationToken = default) { var appConfig = builder .Configuration .GetSection("ApplicationConfiguration") .Get<ApplicationConfiguration>(); builder.Configuration.AddJsonFile("secret.json", optional: true); builder.UserSerilog(appConfig); //builder.ConfigureKestrel(appConfig); builder.ConfigureCors(appConfig); var webHost = builder.WebHost; webHost.ConfigureServices(); var app = builder.Build(); app.UseExceptionHandler(); app.UseMiddleware<CorrelationIdMiddleware>(); app.UsePathBase(appConfig.ApiInfo.BasePath); app.UseHttpsRedirection(); app.UseCorsIfEnabled(appConfig); app.UseAuthentication(); app.UseAuthorization(); app.UseSerilogRequestLogging(); app.UseSwagger(appConfig); app.UseSwaggerUI(appConfig); app.MapControllers(); await app.RunAsync(cancellationToken); }

Did you find this page helpful?