user3802434
user3802434

Reputation: 876

Ambiguites in AspNet Core 2.0 using Swagger in Web API

I am trying to use Swagger in an AspNet Core 2 Web API. I have one sample that works based on:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

However, when I try to use the same approach in another service I get compile error:

2>Startup.cs(41,17,41,27): error CS0121:

The call is ambiguous between the following methods or properties: 'Microsoft.AspNetCore.Builder.SwaggerBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, System.Action)'

and

'Microsoft.AspNetCore.Builder.SwaggerBuilderExtensions.UseSwagger(Microsoft.AspNetCore.Builder.IApplicationBuilder, string, System.Action)'

2>Done building project "SocialNetwork.Api.csproj" -- FAILED.

The target call is in Startup.cs in the Configure method.

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

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

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

        app.UseMvc();
    }

Does anyone have insight on this? Any help would be greatly appreciated. Thank you.

Upvotes: 6

Views: 4582

Answers (4)

Dominik Ernst
Dominik Ernst

Reputation: 11

The only solution is

app.UseSwagger((Action)null!);

Upvotes: 0

snnpro
snnpro

Reputation: 307

Just uninstall the Swagger nugget package and you should be fine

Upvotes: 0

I uninstalled Swagger and just installed the SwashBuckle.AspNetCore because it already implements the Swagger object inside. that is why it is calling ambigious.

Upvotes: 5

Morten Nørgaard
Morten Nørgaard

Reputation: 2809

Could it be that you have references to multiple Swagger-libraries? In my case I had accidentally added a reference to other swagger-related Nuget packages, and that caused the same error as you described.

Upvotes: 2

Related Questions