我正在尝试托管我在 dotnet core 中完成的 API,非常标准的东西,从数据库访问和返回数据。我以前部署过 IIS(不是在本机中),通常一切顺利,但这次不行。
我已经在 IIS 上创建了 Web 应用程序,启动了 WACS 来创建并向其分配证书,然后运行它,一切都很好,DNS 指向正确,我可以从 API 检索一些数据,但是,几个请求之后我得到这个:
服务不可用。
嗯,这很奇怪,该应用程序能够在 Kestrel 中完美运行,我只需执行它,并且 cmd 窗口弹出窗口显示它正在托管该应用程序,但是一旦我将其移至 IIS,一切都崩溃了,经过几次重试后好像失败了。
我检查了事件查看器,这就是发生的情况:
好的,应用程序似乎已成功启动。
几分钟后,发生了这种情况:
等等什么?为什么 IIS 试图关闭该应用程序?在仅 Kestrel 模式下运行 exe 时效果很好。
我做了什么:
我已进入设置并更改了这些:
我禁用了快速失败保护,因为担心会出现一些异常情况而将其关闭。
好像不是吧...请求了几次还是死掉了。
我已将此用户添加到文件夹的权限中:
测试连接告诉我这一点:
这可能是问题所在吗?如果是的话,为什么它能够满足一些请求,然后尝试关闭自己?
有人可以帮助我吗?我从来没有遇到过 IIS 尝试关闭自己的应用程序的问题。
PS:应用程序日志告诉我它刚刚关闭:
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: D:\IIS Root\RVGL API\
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
IIS 日志只向我显示请求,它服务了几个 200 秒,然后只有 503 秒。
这是我的Program.cs,我不知道这是否重要:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidIssuer = builder.Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
};
});
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "RVGL Endpoints",
Version = "v1"
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "",
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme {
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/test/swagger.json", "test API");
c.SupportedSubmitMethods(new Swashbuckle.AspNetCore.SwaggerUI.SubmitMethod[] { });
});
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
看起来 Kestrel 运行良好并且没有任何问题,但是一旦它托管在 IIS 上,它似乎就会使应用程序崩溃,应用程序池似乎没问题,我也尝试过将它托管在具有不同操作系统的不同计算机上(Windows Server 2022)并且发生了同样的问题,我也尝试将其托管在另一台 Windows 11 计算机上,完全相同的问题。