17

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"});
    }
}

3 Answers 3

32

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

Sign up to request clarification or add additional context in comments.

4 Comments

I assume you are refering to this line in my code? var builder = services.AddMvcCore();"
I cannot use AddMvc as this errors. This project was started from a blank webapi code project.
Then try services.AddMvcCore() .AddApiExplorer();
Yup.. That worked.. I was typing that same response as you were. your initial answer gave me all the direction I needed. Awesome.. thank you. Now, to add structuremap for IoC and see what that breaks on swagger. I have had that issue in the past in regular MVC projects
0

For ASP.NET Core 2.2 you should write:

services.AddMvcCore().AddApiExplorer();

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.