从 Core 2.2 升级到 ASP.NET Core 3.0 后出现 IIS 配置错误

IIS config error after upgrading to ASP.NET Core 3.0 from Core 2.2

如果我的 Web 项目升级到 asp.net 核心 3.0,我会升级一些,并试图将它们推送到 IIS Web 服务器。我安装了 .net core 3.0 托管包和运行时并卸载了旧版本以防万一。

我尝试加载页面但出现错误:

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Error Code     0x8007000d
Config Error       
Config File    \?\C:\inetpub\MySite\web.config

下面是我的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=".\MySite.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="">
        <environmentVariables>
          <environmentVariable name="EXCLUDED_LINE" value="Test" />
          <environmentVariable name="COMPLUS_ForceENC" value="1" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

除了 .net 版本之外,我没有更改服务器上的任何其他内容 - 我直接替换了之前的工作项目。

startup.cs:

using System;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace MySite
{
    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.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAuthentication(IISDefaults.AuthenticationScheme);

            services.AddTransient<IClaimsTransformation, CustomClaimsTransformer>();

            services.AddSingleton<IAuthorizationHandler, CheckADGroupHandler>();

            services.AddRazorPages().AddFluentValidation();

            services.AddDbContext<MyContext>(options =>
                options.UseSqlServer(
                    "Data Source=MYSERVER\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"));
        }

            // 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();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

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

            app.UseEndpoints(endpoints => endpoints.MapRazorPages());

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

        }
    }
}

项目:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <AssemblyName>MySite</AssemblyName>
    <RootNamespace>MySite</RootNamespace>
    <LangVersion>8</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
    <PackageReference Include="FluentValidation.AspNetCore" Version="8.5.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="3.0.0" />
    <PackageReference Include="NLog" Version="4.6.2" />
    <PackageReference Include="NLog.Targets.Syslog" Version="5.1.0" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.8.1" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="wwwroot\css" />
    <Folder Include="wwwroot\webfonts\" />
  </ItemGroup>

</Project>

我厌倦了手动将其复制到测试 IIS,并且大多数页面只显示错误 500,尽管我可以浏览 web.config。

我在新服务器和安装 3.0 之前卸载核心 2.2 的服务器上都遇到了这个问题。最终我找到了一台可以工作的服务器并意识到我忘记卸载旧的托管包所以它同时具有 2.2 和 3.0

最后,我通过在服务器上安装 asp.net 核心 2.2 托管包以及 asp.net 核心 3.0 托管来修复它。网站现在运行正常。