Hi
I got another type of unhandled exception. This time is about Swagger:
e.Diagnostics.ExceptionHandlerMiddleware - An unhandled exception has occurred while executing the request. System.InvalidOperationException: Unable to resolve service for type 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' while attempting to Invoke middleware 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware'. at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp, Type type, Type middleware) at lambda_method(Closure , Object , HttpContext , IServiceProvider ) at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) at Abp.AspNetZeroCore.Web.Authentication.JwtBearer.JwtTokenMiddleware.<>c__DisplayClass0_0.<<UseJwtTokenMiddleware>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Abp.AspNetZeroCore.Web.Authentication.JwtBearer.JwtTokenMiddleware.<>c__DisplayClass0_0.<<UseJwtTokenMiddleware>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
11 Answer(s)
-
0
Please share your
Startup.cs
code. -
0
Sent to your email.
-
0
Email: [email protected]
-
0
hi @kasem
Can you debug to confirm that AddSwaggerGen has been executed?
For the In Process hosting model, you can refer to the documentation for Microsoft and the community. Regarding the automatic restart of the application, I think this is more like a problem with IIS.
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-2.2#in-process-hosting-model https://weblog.west-wind.com/posts/2019/Mar/16/ASPNET-Core-Hosting-on-IIS-with-ASPNET-Core-22
-
0
Hi @maliming
It's enabled in dev mode but disabled in production. Not sure if it's the correct way to disable Swagger in production?
I get this error in production
Thanks
-
0
-
0
Not sure I got it but I already have this in appsettings in both production and dev.
"SwaggerUIEnabled": "false"
Swagger isn't accessible in prod.
Do you mean just omit the line bool.Parse(_appConfiguration["App:SwaggerUIEnabled"]) ?
Should the ON/OFF I have in appsettings enable/disable without the If statement I added?
-
0
Did you re-publish your application after changing the code to the following? Because the
startup.cs
you sent to me uses theWebConsts.SwaggerUiEnabled
constant, which is alwaystrue
if (bool.Parse(_appConfiguration["App:SwaggerUIEnabled"])) { services.AddSwaggerGen(options => ... } if (bool.Parse(_appConfiguration["App:SwaggerUIEnabled"])) { app.UseSwagger(); app.UseSwaggerUI(options => ... }
-
0
Oh you are right! seems I missed the other code snippet that runs Swagger. Although the first statement made Swagger inaccessible in production the second if statement isn't available in my code yet. Do you think this is what causing that error?
-
0
Yes.
Because you didn't configure swagger's dependency injection, but used its middleware.
You can modify your application by following the code above. https://support.aspnetzero.com/QA/Questions/7662#answer-054d8fbc-be36-d038-ed91-39f04b40dca6
-
1
Aha! Fixed now. I'll deploy soon and update you
Thanks a lot