ASP.NET Core v3.1 应用程序真的可以自包含吗?

Can an ASP.NET Core v3.1 app really be self contained?

我有一台服务器,目前在 IIS 中托管许多 .net core v2 服务。

我有一个新的服务。通常我只会使用服务器安装的任何 运行 时间,但我想,“.Net Core 可以部署独立的应用程序,因此我可以同时拥有 v2 服务和 v3 服务。"

我担心我错了。

当我 运行 自包含应用程序时,它给出以下错误:

Handler "aspNetCore" has a bad module "AspNetCoreModuleV2" in its module list.

当我查找时 I find, that I will need to install the hosting bundle. Which when you go to the .net core site that has it,它显示为 .Net Core(在 IIS 机器上)推荐的 install 方法。

这是我开始担心的地方。我以为我不必在我的服务器上安装 .net core 3。我想我可以 运行 一个独立的应用程序。

我无法安装 .Net Core v3,除非测试所有以 .Net Core v2 为目标的现有服务在 .Net Core v3 运行time 上都能正常工作。

所以,看起来我必须将我的服务降级到 v2(糟糕,我讨厌工作倒退。)

但在我这样做之前,我想我会问:
是否可以在 IIS 上托管一个 Asp.Net Core v3 Web API 服务,它只有 .Net Core v2 运行时间?

要使用哪个 运行time 版本由您的项目文件定义,因此不同的 运行times 可以 运行 在同一台机器上没有任何问题。 假设您有两个服务:Service1 是使用 .net core 2.1 构建的,Service2 是使用 .net core 3.1 构建的。 项目文件将如下所示:

Service1.csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

Service2.csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

一旦您通过 dotnet rundotnet ServiceX.dll 运行 您的应用程序,dotnet 可执行文件就是应用程序的宿主,并选择正确的 运行基于程序集元数据的时间。 运行使用的时候其实是写在编译好的DLL中

因此,2.* 和 3.* 应用程序可以完全共存于同一台机器上,前提是安装了所需的 运行 次。

在此处阅读更多内容:https://docs.microsoft.com/en-us/dotnet/core/versions/selection

检查这个实验。 在我的机器上安装了以下内容: 1) Asp.Net 核心运行时 2.1.14 2) Asp.Net 核心运行时 3.1.2 3) IIS 10.0 4) Asp.Net 核心模块(与托管包一起安装)

我创建了 2 个应用程序,每个 运行时间一个:

dotnet new web -f netcoreapp2.1 -o Service1

dotnet new web -f netcoreapp3.1 -o Service2

然后我像这样修改了两个应用程序 Startup.cs:

    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                var framework = Assembly
                    .GetEntryAssembly()?
                    .GetCustomAttribute<TargetFrameworkAttribute>()?
                    .FrameworkName;
                var taskLocation = typeof(Task).Assembly.Location;
                await context.Response.WriteAsync($@"Hello World from {framework}
Location of Task assembly: {taskLocation}.
                ");
            });
        }
    }

我在发布中发布了这两个服务: 服务1: C:\Users\info\source\repos\Service1> dotnet publish -c 发布 适用于 .NET Core 的 Microsoft (R) Build Engine 版本 16.4.0+e901037fe 版权所有 (C) Microsoft Corporation。保留所有权利。

  Restore completed in 159,32 ms for C:\Users\info\source\repos\Service1\Service1.csproj.
  Service1 -> C:\Users\info\source\repos\Service1\bin\Release\netcoreapp2.1\Service1.dll
  Service1 -> C:\Users\info\source\repos\Service1\bin\Release\netcoreapp2.1\publish\

服务 2:

C:\Users\info\source\repos\Service2> dotnet publish -c Release
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 41,98 ms for C:\Users\info\source\repos\Service2\Service2.csproj.
  Service2 -> C:\Users\info\source\repos\Service2\bin\Release\netcoreapp3.1\Service2.dll
  Service2 -> C:\Users\info\source\repos\Service2\bin\Release\netcoreapp3.1\publish\

在 IIS 下我创建了两个网站,每个服务一个,每个都指向正确的输出目录

service1.lvh.me >>> C:\Users\info\source\repos\Service1\bin\Release\netcoreapp2.1\publish\

service2.lvh.me >>> C:\Users\info\source\repos\Service2\bin\Release\netcoreapp3.1\publish\

然后我访问了每个网站。访问 service1.lvh.me 我得到: 访问 service2.lvh.me 我得到:

如您所见,每个应用程序都需要正确版本的框架,并且从特定文件夹加载框架类型。

因此,在同一台机器上安装不同版本的 .net 核心框架 运行 以及在同一 IIS 上安装特定版本的 Web 应用程序 运行 是没有问题的。