Home > other >  Publishing to Azure failed due to not seeing AddSwaggerGen
Publishing to Azure failed due to not seeing AddSwaggerGen

Time:02-12

I have a simple ASP.NET Core 3.1 app, which I'm trying to Publish to Azure.

But Publish fails with the following error:

Publish has encountered an error. Be sure that the Startup.cs for your application is calling AddSwaggerGen from within ConfigureServices in order to generate swagger file. Visit enter image description here

But it is configured properly and works well when started locally. Thus I'm not sure what is wrong or how to pass this error.

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddDbContext<TestProductContext>(options =>
        {
            options.UseSqlServer(Configuration["ConnectionStrings:Database"]);
        });
        services.AddControllers().AddOData(opt =>
            opt.Filter().Expand().Select().OrderBy().Count().SetMaxTop(100)
                .AddRouteComponents("odata", GetEdmModel()));

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "OdataTestProject", Version = "v1" });
            c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        });

        //// https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#filter-scopes-and-order-of-execution
        services.AddMvc(opts => { opts.Filters.Add(new AutoLogAttribute()); });
        services.AddApplicationInsightsTelemetry();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "/swagger/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "OdataTestProject v1"));
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }


    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        var entitySet = builder.EntitySet<TestProduct>("TestProducts");
        entitySet.EntityType.HasKey(entity => entity.Id);
        return builder.GetEdmModel();
    }
}

CodePudding user response:

  • One way to clear the issue is to delete the existing App , create new one and copy the code from old project.Check if your path contains any spaces

OR

  • Delete bin and obj folders .Reopen the project and let the NuGet load in the background (it has removed all the NuGet refs). Do a CLEAN and REBUILD
  • Try to use the latest version of Swashbuckle.AspNetCore.
  • Add a global.json file to your Web API project
    Put this in it:
 { 
  "sdk":
   { 
    "version": "3.1.406", 
    "rollForward": "latestPatch" 
   } 
 }
  • Instead of Swashbuckle.AspNetCore.Swagger nuget package install **Swashbuckle.AspNetCore **.

  • Swashbuckle.AspNetCore. package contains the below inbuilt nuget packages

enter image description here

Please refer git hub for more information

  • Related