Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core and Swagger API generation

Tags:

c#

swagger

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"});
    }
}
like image 881
JCircio Avatar asked Jun 08 '17 15:06

JCircio


People also ask

What is Swagger .NET Core?

Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. It allows both computers and humans to understand the capabilities of a REST API without direct access to the source code. Its main goals are to: Minimize the amount of work needed to connect decoupled services.

Is Swagger automatically generated?

You can write a Swagger spec for your API manually, or have it generated automatically from annotations in your source code.


1 Answers

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

like image 189
Vladimir Arustamian Avatar answered Oct 07 '22 02:10

Vladimir Arustamian