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

部署至Azure Web Service后出现HTTP ERROR 500的解决请求

部署Azure Web Service后出现HTTP 500内部服务器错误的排查与解决

我已将.NET 6 Web应用部署至Azure Web Service,本地运行正常,但部署后出现内部服务器错误(HTTP ERROR 500)。已在服务器防火墙设置中勾选“允许Azure服务和资源访问此服务器”。

IIS提示的可能原因如下:

最可能的原因:

IIS已收到请求,但在处理请求期间发生内部错误。错误的根本原因取决于处理请求的模块以及工作进程发生错误时的状态。
IIS无法访问网站或应用的web.config文件,这可能是NTFS权限设置不正确导致的。
IIS无法处理网站或应用的配置。
已认证用户没有使用此DLL的权限。
请求被映射到托管处理程序,但未安装.NET扩展功能。

通过Kudu Console(地址:https://sitename.scm.azurewebsites.net)查看的web.config文件内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\CRM_BackEnd.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

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();
var defaultConnectionString = string.Empty;
    
builder.Services.AddDbContext<CRMDBContext>(option =>
{
    option.UseSqlServer(builder.Configuration.GetConnectionString("BE_CRM"));
});
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.Configure<PublitioApi>(builder.Configuration.GetSection("PublitioApi"));
builder.Services.Configure<SendGirdApi>(builder.Configuration.GetSection("SendGridApi"));
builder.Services.AddDependenceInjection();
builder.Services.AddSwagger();
builder.Services.AddSwaggerGenNewtonsoftSupport();
builder.Services.AddControllers().AddJsonOptions(options =>
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    //app.UseSwagger();
    //app.UseSwaggerUI();
}
else
{
    app.Use(async (context, next) =>
    {
        await next();
        if(context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
        {
            context.Request.Path = "/swagger/index.html";
            await next(); 
        }
    });
    app.UseHsts();
}

app.UseSwagger(c =>
{
    c.RouteTemplate = "MyBackEnd/swagger/{documentName}/swagger.json";
});

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/MyBackEnd/swagger/v1/swagger.json","CRM_BackEnd v1");
});
app.UseHttpsRedirection();
app.UseCors(builder =>
{
    builder
    .AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader();
});
app.UseAuthentication();

app.UseJwt();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

.csproj文件内容:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>disable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UserSecretsId>1798ad59-2539-4d78-bdc2-fd2f9983b3e9</UserSecretsId>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
        <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
        <PackageReference Include="Firebase.Auth" Version="1.0.0" />
        <PackageReference Include="FirebaseAdmin" Version="2.3.0" />
        <PackageReference Include="FirebaseAuthentication.net" Version="3.7.2" />
        <PackageReference Include="FirebaseStorage.net" Version="1.0.3" />
        <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        <PackageReference Include="Otp.NET" Version="1.2.2" />
        <PackageReference Include="Publitio-NET" Version="1.0.1" />
        <PackageReference Include="SendGrid" Version="9.28.1" />
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
        <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.4.0" />
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.0" />
        <PackageReference Include="System.Security.Cryptography.ProtectedData" Version="6.0.0" />
    </ItemGroup>
    <ItemGroup>
        <Content Update="firebasesdk.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
    <ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
</Project>

排查与解决步骤

1. 启用标准输出日志定位具体错误

当前web.config中stdoutLogEnabled设置为false,无法捕获应用启动或运行时的具体异常。修改web.config:

<aspNetCore processPath="dotnet" arguments=".\CRM_BackEnd.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />

保存后重启Azure Web Service,访问应用触发错误,再通过Kudu Console进入site\wwwroot\logs目录查看日志文件,日志会包含具体的异常信息(如数据库连接失败、配置项缺失、依赖加载错误等)。

2. 检查Azure配置项是否完整

本地开发时可能使用User Secrets或本地appsettings.json存储敏感配置,但部署到Azure后这些配置不会自动同步:

  • 登录Azure门户,进入目标Web Service,在配置 -> 应用程序设置中检查是否存在BE_CRM连接字符串,以及AppSettings、PublitioApi、SendGridApi对应的配置项,确保值正确。
  • 通过Kudu Console进入site\wwwroot目录,确认firebasesdk.json文件存在且内容与本地一致。

3. 修正web.config路径错误

原web.config中arguments字段的路径存在转义错误,应确保为.\CRM_BackEnd.dll,保证dotnet能正确找到启动程序集。

4. 切换托管模式排查模块兼容性

当前使用inprocess托管模式,若AspNetCoreModuleV2存在兼容性问题,可尝试切换为outofprocess模式:

<aspNetCore processPath="dotnet" arguments=".\CRM_BackEnd.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="outofprocess" />

重启Web Service后测试是否恢复正常。

5. 修复依赖包版本兼容性

项目中引用的Microsoft.AspNetCore.Http.Abstractions 2.2.0版本与.NET 6存在兼容风险,建议升级到6.x版本:

<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="6.0.0" />

重新发布应用到Azure。

6. 验证数据库连接权限

虽然已勾选“允许Azure服务访问服务器”,仍需确认:

  • 数据库服务器的防火墙规则是否包含Azure Web Service的出站IP地址(可在Web Service的属性中查看出站IP列表,添加到数据库防火墙)。
  • 连接字符串中的用户名、密码是否正确,且该用户拥有数据库的读写权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:41:13