I am currently working on a project involving three microservices developed using .NET 8. These microservices are deployed using a single App Service Plan, each with its respective Azure AppService. The setup seems fine when validating the endpoints via Swagger URL, and we have path-based URLs set up in the production environment.
Here’s the scenario: any request that comes with /m1/* should be served by the Azure AppService hosting the m1 microservice code. The same rule applies for microservices m2 and m3.
The code for setting up Swagger in the application is as follows:
public static void UseSwaggerExtension(this IApplicationBuilder app, IWebHostEnvironment env, string appName)
{
bool enableSwagger = FeatureManagerService.IsFeatureEnabled(nameof(FeatureFlags.EnableSwagger));
if (!enableSwagger) { return; };
app.UseSwagger(c =>
{
c.RouteTemplate = $"docs/{{documentName}}/openapi.json";
});
ConfigureSwaggerForPROD(app, env, appName);
app.UseSwaggerUI(config =>
{
config.SwaggerEndpoint("v1/openapi.json", "Version 1");
config.RoutePrefix = $"docs";
config.DocExpansion(DocExpansion.List);
config.DisplayRequestDuration();
config.DefaultModelsExpandDepth(-1);
});
}
private static void ConfigureSwaggerForPROD(IApplicationBuilder app, IWebHostEnvironment env, string appName)
{
if (env.IsProduction())
{
app.UseSwagger(c =>
{
string basePrefix = appName.Equals(Contants.M3, StringComparison.CurrentCultureIgnoreCase) ? "m3" : appName.ToLower();
c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
swaggerDoc.Servers = [
new() { Url = $"/{basePrefix}"}
]);
});
}
}
The problem arises post-deployment when I try to validate the Swagger URL: https://testapi.com/m1/docs/index.html. I can access the Swagger page, but when validating one endpoint, the API request does not seem to route to the right endpoint, resulting in an HTTP Status Code: 404.
The package reference included in the project is:
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Is there something wrong with my Swagger configuration? Have I missed something in the routing setup? I would appreciate any guidance or suggestions to help resolve this issue.
Can anyone please help me here by providing their guidance. Any help would be greatly appreciated.