Я пытаюсь создать веб-API в. NET ядре 3.1, однако я получаю следующую ошибку - «Не удается разрешить службу для типа« Swashbuckle.AspNetCore.Swagger.ISwaggerProvider »при попытке вызвать промежуточное программное обеспечение« Swashbuckle ». AspNetCore.Swagger.SwaggerMiddleware '"
Я использую. NET Core 3.1 & Swashbuckle.AspNetCore.SwaggerGen, Версия = 5.2.1.0
Мой Startup.cs похож на:
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
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.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder => builder
.WithOrigins(Configuration["AllowedHosts"])
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
// Load custom configuration related to error settings.
var errorConfig = new ConfigurationBuilder().AddJsonFile("errorSettings.json").Build();
// Load settings
services.AddOptions();
services.Configure<SecuritySettings>(Configuration.GetSection("SecuritySettings"));
services.Configure<ErrorSettings>(errorConfig.GetSection("ErrorSettings"));
// Load Database connection strings from AppSettings and load data context.
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1", Description = "API" });
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddControllersWithViews()
.AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize);
// Configure mappers
var mapperConfig = new MapperConfiguration(opts =>
{
opts.AddProfile(new AutoMapperProfile());
});
var mapper = mapperConfig.CreateMapper();
services.AddSingleton<IMapper>(mapper);
// Add Http Context Accessor
services.AddHttpContextAccessor();
// Add Http client factory
services.AddHttpClient();
// Add transient for Services.
services.AddTransient<ICustomerService, CustomerService>();
services.AddTransient<IUserService, UserService>();
}
// 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.EnvironmentName == "Development")
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// 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");
c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseCors("CorsPolicy");
}
}
}