4

I am trying to implement Swagger in my WebApi. However, it is not working. The issue is that swagger.json loads forever. When I try to go to /swagger/v1/swagger.json, it loads forever and my CPU usages spikes to 100%.

This is my configuration:

public class Startup
{
    ...

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
       ...

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
                Version = "v1",
                Title = "ToDo API"
            });
        });

       ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
    {
       ...

        app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        // Setup the endpoints.
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action}/{id?}");
            endpoints.MapHub<HomeHub>("/homehub");
        });
    }
}

I of course already looked around on the internet, but could not find any solution. I do not get any errors, so what could be the issue?

Update:

I have tried generating the json file with the CLI, and same issue. It just loads and nothing happens. Besides taking 24gb of memory...

2

1 Answer 1

4

I believe that one of your controller or class causes this error. It looks like swagger tries to load something recursively.

I suggest you to use memory profiler (like dotMemory) to find out what is going on (what creates this memory leak)

Another workaround is to comment out all your controllers and enable them one by one until you find the erroneous code.

Also you may want to check this Answer

1
  • 3
    Thanks! It was a memory leak indeed. There was a method in our basecontroller not labeled with the [NonAction] attribute. That was causing the issue. Adding attribue solved the problem. I eventually solved it by commenting out everything and enabling one by one. Commented May 3, 2020 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.