.NET Core CLI 运行 非核心应用程序,它是如何工作的?

.NET Core CLI running non-Core App, how does it work?

我正在学习 dotnet CLI(以及 .NET Core),关于多平台定位,有一件事不清楚。假设我的项目有以下部分:

  "frameworks": {
    "netcoreapp1.0": {
       ...
    },
    "net451": { }
  }

而且我的 PC 上安装了 net451。那我运行吧:

dotnet run -f net451

编译和运行项目用什么?它是否从 net451 sdk 调用 msbuild? 运行ning 应用程序是否仍被视为 .NET Core 应用程序?

一些有用的link:

Running .NET Core apps on multiple frameworks and What the Target Framework Monikers (TFMs) are about

What is used to compile and run the project? Does it call the msbuild from net451 sdk?

dotnet中加上-v标志就可以自己查了:

> dotnet -v run -f net451

Project hwapp (.NETFramework,Version=v4.5.1) will be compiled because expected outputs are missing
        C:\code\hwapp\bin\Debug\net451\rtmapp.exe
        C:\code\hwapp\bin\Debug\net451\rtmapp.pdb
        C:\code\hwapp\bin\Debug\net451\win7-x64\rtmapp.exe
        C:\code\hwapp\bin\Debug\net451\win7-x64\rtmapp.pdb
Compiling hwapp for .NETFramework,Version=v4.5.1
Running C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk.0.0-preview2-003121\csc.dll" -noconfig @C:\code\hwapp\obj\Debug\net451\dotnet-compile-csc.rsp
Process ID: 4900

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.4136972


Running C:\code\hwapp\bin\Debug\net451\win7-x64\rtmapp.exe
Process ID: 12208
Hello World!

> dotnet -v run -f netcoreapp1.0

Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
        C:\code\hwapp\bin\Debug\netcoreapp1.0\rtmapp.dll
        C:\code\hwapp\bin\Debug\netcoreapp1.0\rtmapp.pdb
        C:\code\hwapp\bin\Debug\netcoreapp1.0\rtmapp.deps.json
        C:\code\hwapp\bin\Debug\netcoreapp1.0\rtmapp.runtimeconfig.json
Compiling hwapp for .NETCoreApp,Version=v1.0
Running C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk.0.0-preview2-003121\csc.dll" -noconfig @C:\code\hwapp\obj\Debug\netcoreapp1.0\dotnet-compile-csc.rsp
Process ID: 12084

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.6766971


Running C:\Program Files\dotnet\dotnet.exe exec --additionalprobingpath C:\Users\Svick\.nuget\packages C:\code\hwapp\bin\Debug\netcoreapp1.0\rtmapp.dll
Process ID: 9068
Hello World!

请注意,在这两种情况下,都直接使用了 C# 编译器 csc(它是 .Net Core 版本的 csc,因此 运行 使用 dotnet csc.dll)。两者之间的主要区别在于 .rsp 文件,其中包含 csc 的附加参数。

net451版本中,有趣的部分是这样的:

-out:"C:\code\hwapp\bin\Debug\net451\hwapp.exe"
-r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\mscorlib.dll"
-r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.dll"
-r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Core.dll"
-r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Microsoft.CSharp.dll"

这就是使用 net451 的含义:.Net Framework 4.5.1 的参考程序集用于编译应用程序。

netcoreapp1.0 版本中,它看起来像这样 (t运行cated):

-out:"C:\code\hwapp\bin\Debug\netcoreapp1.0\hwapp.dll"
-r:"C:\Users\Svick\.nuget\packages\Microsoft.CSharp.0.1\ref\netstandard1.0\Microsoft.CSharp.dll"
-r:"C:\Users\Svick\.nuget\packages\Microsoft.VisualBasic.0.1\ref\netstandard1.1\Microsoft.VisualBasic.dll"
-r:"C:\Users\Svick\.nuget\packages\Microsoft.Win32.Primitives.0.1\ref\netstandard1.3\Microsoft.Win32.Primitives.dll"
-r:"C:\Users\Svick\.nuget\packages\System.AppContext.1.0\ref\netstandard1.6\System.AppContext.dll"
-r:"C:\Users\Svick\.nuget\packages\System.Buffers.0.0\lib\netstandard1.1\System.Buffers.dll"
-r:"C:\Users\Svick\.nuget\packages\System.Collections.0.11\ref\netstandard1.3\System.Collections.dll"
-r:"C:\Users\Svick\.nuget\packages\System.Collections.Concurrent.0.12\ref\netstandard1.3\System.Collections.Concurrent.dll"

请注意,.Net Standard Library 参考程序集被改用了。

在这两种情况下,根本不使用 msbuild。

Can the running app be still considered as .NET Core App?

我不这么认为。我会说这是一个 .Net Framework 应用程序,使用 .Net Core CLI/SDK 创建。是的,这很混乱。