You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署ASP.NET Core 6 Web API到Azure时Swagger报错的解决方法

ASP.NET Core 6 Web API部署Azure时的发布错误问题

我将ASP.NET Core 6 Web API项目部署到Azure时出现发布错误,报错信息如下:

Unable to detect any use of AddSwaggerGen or ConfigureSwaggerGen in the ConfigureServices method, skipping API update
A call to warmup your site failed with response code: 'InternalServerError'

我已调整Swagger相关配置,但问题仍未解决,以下是项目的Program.cs代码:

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Safari_Wave.Models;
using Safari_Wave.Repository;
using Safari_Wave.Repository.Interface;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace Safari_Wave
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.AllowAnyOrigin() // Update with your React app URL
                   .AllowAnyHeader()
                   .AllowAnyMethod();
                });
            });

            // Add session configuration
            builder.Services.AddDistributedMemoryCache();
            builder.Services.AddSession(options =>
            {
                options.Cookie.Name = "SafariWave";
                options.IdleTimeout = TimeSpan.FromSeconds(10);
                options.Cookie.IsEssential = true;
            });

            // Add Authentication
            builder.Services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
                .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata = false;
                options.SaveToken = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateLifetime = true,
                    //ValidIssuer = builder.Configuration["Jwt:Issuer"],
                    //ValidAudience = builder.Configuration["Jwt:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
                };
            });

            builder.Services.AddControllers().AddJsonOptions(x =>
                x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles).AddNewtonsoftJson(option =>
                {
                    option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                }); ;

            builder.Services.AddAutoMapper(typeof(Program));

            builder.Services.AddDbContext<SafariWaveContext>(options =>
            {
                options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
            });

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "Safari Wave API",
                    Version = "v1",
                });
            });
            
            builder.Services.AddSingleton<SMSService>(provider =>
            {
                var accountSid = builder.Configuration["Twilio:Sid"];
                var authToken = builder.Configuration["Twilio:Token"];
                var verifySid = builder.Configuration["Twilio:verifySid"];
                var logger = provider.GetRequiredService<ILogger<SMSService>>();

                return new SMSService(accountSid, authToken, logger, verifySid);
            });

            builder.Services.AddHttpContextAccessor();
            builder.Services.AddScoped<IPackageManagement, PackageManagement>();
            builder.Services.AddScoped<IUserManagement, UserManagement>();
            builder.Services.AddScoped<IBookingSevice, BookingService>();
            builder.Services.AddScoped<IEnquiryService, EnquiryService>();
            builder.Services.AddScoped<IReviewServices, ReviewService>();
            builder.Services.AddScoped<IOrderService, OrderService>();
            builder.Services.AddScoped<SaveImage>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.RoutePrefix = String.Empty;
            });
           
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "coverImage")),
                RequestPath = "/coverImage"
            });

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "Image")),
                RequestPath = "/Image"
            });

            app.UseHttpsRedirection();
            app.UseCors();
            app.UseSession();

            app.UseAuthentication();
            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}

浏览器中显示如下错误页面:
错误页面

内容的提问来源于stack exchange,提问作者el halcon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:47:07