Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Swagger Unhandled exception #7662


User avatar
0
kasem created

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)
  • User Avatar
    0
    maliming created
    Support Team

    Please share your Startup.cs code.

  • User Avatar
    0
    kasem created

    Sent to your email.

  • User Avatar
    0
    maliming created
    Support Team
  • User Avatar
    0
    maliming created
    Support Team

    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

  • User Avatar
    0
    kasem created

    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

  • User Avatar
    0
    maliming created
    Support Team

    You can put enable/disable swagger in the settings.

    bool.Parse(_appConfiguration["App:SwaggerUIEnabled"])

  • User Avatar
    0
    kasem created

    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?

  • User Avatar
    0
    maliming created
    Support Team

    Did you re-publish your application after changing the code to the following? Because the startup.cs you sent to me uses the WebConsts.SwaggerUiEnabled constant, which is always true

    if (bool.Parse(_appConfiguration["App:SwaggerUIEnabled"]))
    {
    	services.AddSwaggerGen(options => ...
    }
    
    if (bool.Parse(_appConfiguration["App:SwaggerUIEnabled"]))
    {
    	app.UseSwagger();
    	app.UseSwaggerUI(options => ...
    }
    
  • User Avatar
    0
    kasem created

    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?

  • User Avatar
    0
    maliming created
    Support Team

    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

  • User Avatar
    1
    kasem created

    Aha! Fixed now. I'll deploy soon and update you

    Thanks a lot