什么是 vNext 控制台应用程序?
What is a vNext console application?
我安装了 Visual Studio 2015 RC 来试用 ASP.Net vNext 模板。在“Web”部分,我注意到一个显示为
的控制台应用程序
我决定把它弄乱,我发现了一些有趣的点:
- 默认模板未将
Main()
指定为 static
。
System.CodeDom
和 System.Net
等许多程序集不可用。
System.Console.ReadKey
等很多方法无法使用
这些 vNext 控制台应用程序是什么?为什么要限制以及它们的用途是什么?
这些控制台应用程序正在利用新的 .NET execution environment (DNX, previously KRE). It includes a new project system and allows you to target different versions of the CLR。
其中一个版本是 CoreCLR,它是 .NET 的精简版。这个版本是模块化的,它的 libraries 作为一堆 NuGet 包分发。以 .NET Core 为目标时,您可能需要包含一些额外的包(project.json 文件中的依赖项部分)。
然而,一些限制可能是由于并非所有 API 都已经迁移到 .NET Core,或者它们不会迁移到 .NET Core,因为 API 表面在 .NET Core 上更小。
答案
What is a vNext console application?
这是一个在新的 .NET 运行时环境 (DNX) 中运行的控制台应用程序。
Why the restrictions and what are the uses of them?
出现这些限制是因为您的目标是 .NET Core (dnxcore50
) 而不是(或除此之外)完整的 .NET Framework (dnx451
)。这些限制的使用,如据我所知,是允许与许多不同的操作系统交叉兼容。也就是说,.NET Core 的功能比完整框架少,因为这样更容易与许多系统兼容。随着时间的推移,这些限制可能会随着更多完全跨平台的出现而消失。
The default template does not specify Main() as static.
DNX
附带 Microsoft.Framework.ApplicationHost
。这个默认的应用宿主 "knows how to find a public void Main
method. This is the entry point used to set up the ASP.NET hosting layer..." 它也仍然知道如何找到传统的 static void Main
方法。实例 Main
方法的一个优点是它允许我们请求运行时环境将服务注入我们的应用程序。
Many assemblies such as System.CodeDom and System.Net are not available. Many methods such as System.Console.ReadKey cannot be used.
System.Console.ReadKey
在 dnx451
中可用,但在 dnxcore50
中不可用。我上次检查时 System.Net
也是如此。所以,如果你想使用它们,请确保定位 dnx451
而不是 dnxcore50
。
想要取消限制?只需从您的 project.json
文件中删除 dnxcore50
条目。那么您将只针对完整的框架而不受限制。
另请参阅
https://msdn.microsoft.com/en-us/magazine/dn913182.aspx
我正在慢慢了解新的运行时,并将使用相关参考更新此 post。
Microsoft.Framework.Runtime.Sources.EntryPointExecutor->TryGetEntryPoint()
有一段代码正在挑选程序 class 及其主要功能
我安装了 Visual Studio 2015 RC 来试用 ASP.Net vNext 模板。在“Web”部分,我注意到一个显示为
的控制台应用程序我决定把它弄乱,我发现了一些有趣的点:
- 默认模板未将
Main()
指定为static
。 System.CodeDom
和System.Net
等许多程序集不可用。System.Console.ReadKey
等很多方法无法使用
这些 vNext 控制台应用程序是什么?为什么要限制以及它们的用途是什么?
这些控制台应用程序正在利用新的 .NET execution environment (DNX, previously KRE). It includes a new project system and allows you to target different versions of the CLR。
其中一个版本是 CoreCLR,它是 .NET 的精简版。这个版本是模块化的,它的 libraries 作为一堆 NuGet 包分发。以 .NET Core 为目标时,您可能需要包含一些额外的包(project.json 文件中的依赖项部分)。
然而,一些限制可能是由于并非所有 API 都已经迁移到 .NET Core,或者它们不会迁移到 .NET Core,因为 API 表面在 .NET Core 上更小。
答案
What is a vNext console application?
这是一个在新的 .NET 运行时环境 (DNX) 中运行的控制台应用程序。
Why the restrictions and what are the uses of them?
出现这些限制是因为您的目标是 .NET Core (dnxcore50
) 而不是(或除此之外)完整的 .NET Framework (dnx451
)。这些限制的使用,如据我所知,是允许与许多不同的操作系统交叉兼容。也就是说,.NET Core 的功能比完整框架少,因为这样更容易与许多系统兼容。随着时间的推移,这些限制可能会随着更多完全跨平台的出现而消失。
The default template does not specify Main() as static.
DNX
附带 Microsoft.Framework.ApplicationHost
。这个默认的应用宿主 "knows how to find a public void Main
method. This is the entry point used to set up the ASP.NET hosting layer..." 它也仍然知道如何找到传统的 static void Main
方法。实例 Main
方法的一个优点是它允许我们请求运行时环境将服务注入我们的应用程序。
Many assemblies such as System.CodeDom and System.Net are not available. Many methods such as System.Console.ReadKey cannot be used.
System.Console.ReadKey
在 dnx451
中可用,但在 dnxcore50
中不可用。我上次检查时 System.Net
也是如此。所以,如果你想使用它们,请确保定位 dnx451
而不是 dnxcore50
。
想要取消限制?只需从您的 project.json
文件中删除 dnxcore50
条目。那么您将只针对完整的框架而不受限制。
另请参阅
https://msdn.microsoft.com/en-us/magazine/dn913182.aspx
我正在慢慢了解新的运行时,并将使用相关参考更新此 post。
Microsoft.Framework.Runtime.Sources.EntryPointExecutor->TryGetEntryPoint() 有一段代码正在挑选程序 class 及其主要功能