JCircio
JCircio

Reputation: 543

.NET Core and Swagger API generation

I am creating a barebones .NET Core web api project (Started from blank template below) https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/

The code below was working fine, untill I added Swashbuckle.AspNetCore (Along with the configuration code below), now we get this error

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider' has been registered.

Any ideas?

Please note: We are not using "builder.AppMvc()" as we are trying to slim this api down as much as possible.

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvcCore();

        builder.AddAuthorization();
        builder.AddFormatterMappings();
        builder.AddJsonFormatters();
        builder.AddCors();

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();



        app.UseMvc();


        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
    }
}

[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new[] {"value1", "value2"});
    }
}

Upvotes: 17

Views: 19044

Answers (3)

Ivan  Silkin
Ivan Silkin

Reputation: 485

These types of errors also appear if you try to add AspNetCore MVC controller with Swagger UI into the AspNetCore Web Application. Just separate them into two different projects: mvc controllers and web application.

Upvotes: 0

Tal Aloni
Tal Aloni

Reputation: 1519

For ASP.NET Core 2.2 you should write:

services.AddMvcCore().AddApiExplorer();

Upvotes: 0

Vladimir Arustamian
Vladimir Arustamian

Reputation: 842

Solution: Use AddMvc() instead of AddMvcCore() in Startup.cs and it will work.

or write:

services.AddMvcCore().AddApiExplorer();

These links can help:

No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/299

Upvotes: 32

Related Questions