Reputation: 55
var app = builder.Build();
#region swagger
app.SwaggerAppBuild();
#endregion
app.UseHttpLogging();
app.UseMiddleware<RequestMiddleware>();
app.UseHttpsRedirection();
app.UseCors("CorsPolicy");
app.UseAuthentication();
so on...
Here app.SwaggerAppBuild() is a custom extension method ..I used for Swagger ..Its only fired when the application starts.I want to know what is usage of "app".What kind of operation should call here? is every thing will act like middleware if I call them by "app"?My understanting is middleware called every http request.
The method code is :
public static void SwaggerAppBuild(this WebApplication app) {
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
c.SwaggerEndpoint("/swagger/v2/swagger.json", "API v2");
});
}
}
Upvotes: 0
Views: 316
Reputation: 23917
Generally speaking, yes, everything you add there is middleware and order-dependant.
I assume, that inside SwaggerAppBuild
, there are the 2 calls to use the swagger middleware:
UseSwagger
UseSwaggerUI
In this case the UseSwagger
middleware is indeed called on each request:
public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
{
if (!RequestingSwaggerDocument(httpContext.Request, out string documentName))
{
await _next(httpContext);
return;
}
try
{
/* ...*/
}
On each request the middleware checks, if the request path is actually matching a certain value, and if not, it just invokes the next middleware in place. If it matches a certain criteria, it will generate a JSON or yml document for you.
The same is true for UseSwaggerUI
. Only this component is creating a static file and a bespoke static file middleware for you and renders the ui for you in it. (very simplified).
You can check the actual implementation over at: https://github.com/domaindrivendev/Swashbuckle.AspNetCore
and the source codes for the middleware:
Upvotes: 1