.NET Core: version numbers and global.json

Original author: Andrew Lock
  • Transfer

I'm sure most people reading this know that Microsoft recently announced ASP.NET Core and .NET Core 2.0 Preview 1 at Microsoft Build 2017.



This article has no purpose - to give an introduction to ASP.NET Core. Instead, we will consider installing .NET Core 2.0 Preview 1 on your computer so that it does not interfere with other projects running under ASP.NET Core 1.0 / 1.1. Those. we will install several versions of .NET Core on one computer.



Install .NET Core 2.0 Preview 1 SDK


Obviously, the first thing to do is install the .NET Core 2.0 Preview 1 SDK from here . It is very easy: no choice of installation options - just download and install. And there is only one version number!



One small note: .NET Core now also includes ASP.NET Core. This means that you need to install fewer external packages when you deploy your application, which is good news!


It is also worth noting that if you want to create ASP.NET Core 2.0 applications in Visual Studio, you will need to install a preliminary version of Visual Studio 2017. It can be installed in parallel with the stable version.


.NET Core Versions


I wrote above that the new .NET Core has only one version number 2.0 preview 1 , but this is not entirely true. There are two different aspects to installing .NET Core: the SDK / CLI version number (command line interface) and the runtime version number (runtime or .NET Core Shared Framework Host).


If you just installed 2.0 preview 1, then if you type dotnet --info in the console , you will see something like the following:


$ dotnet --info
.NET Command Line Tools (2.0.0-preview1-005977)
Product Information:  
 Version:            2.0.0-preview1-005977
 Commit SHA-1 hash:  414cab8a0b
Runtime Environment:  
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.0.0-preview1-005977\
Microsoft .NET Core Shared Framework Host
  Version  : 2.0.0-preview1-002111-00
  Build    : 1ff021936263d492539399688f46fd3827169983

There is a whole bunch of different information, among which there are two different version numbers:


  • 2.0.0-preview1-005977 - SDK version
  • 2.0.0-preview1-002111-00 - runtime version

But these version numbers are a little misleading. I also have .NET Core SDK version 1.0 and .NET Core Runtime versions 1.1.1 and 1.0.4 installed on my computer, but there is no information about them.


Understanding .NET Core runtime Versions


One of the benefits of .NET Core is the ability to install multiple versions of the runtime in parallel, without affecting each other. This is different from how the .NET Framework is installed. You cannot install the .NET Framework 4.5, 4.6 and 4.7 in parallel - version 4.7 will replace previous versions.


You can see which versions of the .NET Core runtime are already installed if you go to the folder C:\Program Files\dotnet\shared\Microsoft.NETCore.App(on poppies you need to look in the folder /usr/local/share/dotnet/shared/Microsoft.NETCore.App). As you can see, there are three versions installed on my computer:



The next question is how do you know which version of the runtime will be used when you launch your application?


Everything is very simple: you need to specify the desired version in the .csprojfile!


For example, in a .NET Core 1.1 project, you can set the parameter (or , if you are building a project for several different versions) to netcoreapp1.1:


netcoreapp1.1

In this case, the application will use .NET Core version 1.1.1 (see the list of installed versions above). If you set to netcoreapp1.0, then version 1.0.4 will be used.


A file .csproj:for an ASP.NET application using the runtime version 2.0 preview 1will look something like this:


netcoreapp2.0aspnet-v2test-32250BD7-D335-414A-A537-53B40874D211

The file .csprojindicates the value netcoreapp2.0and the maximum corresponding version will be used (on my computer, this 2.0.0-preview1-002111-00).


Understanding SDK Versions


I hope you now understand everything about the versions of the .NET Core runtime. But we still have an open question about the SDK / CLI version.


If you go to the folder C:\Program Files\dotnet\sdk(you need to look at the folder on the poppies /usr/local/share/dotnet/sdk), you will see which versions of the SDK are installed on your computer. As you can see, I have two versions installed: 1.0.0and 2.0.0-preview1-005977.



Roughly speaking, the SDK - it is a piece that provides the commands associated with the assembly: dotnet new, dotnet build, dotnet publishetc.


In general, any version of the SDK, which is greater than the version used to create the project, can be used to build it ( dotnet buildand dotnet publish). Thus, you can simply use the SDK version 2.0 to work with projects created in the SDK version 1.0.


This means that in most cases you can use the latest SDK for all projects. Another version of the SDK may be needed, for example, if you want to build a project using a file project.json(in this case you will need an RC2 SDK).


The current version of the SDK also affects new projects created by the team dotnet new. If you use the SDK version 2.0 Preview 1, you will get an application based netcoreapp2.0, if you use the SDK version 1.0, you will get an application based netcoreapp1.1!


The next question is how to tell the application which version of the SDK to use.


Select SDK version in global.json file


The file global.jsonhas a very simple format that just sets which version of the SDK to use:


{
  "sdk": {
    "version": "1.0.0"
  }
}

Раньше файл global.json использовался еще и для того, чтобы указать папки с исходным кодом решения, но эта функциональность будет удалена в будущих версиях.

Когда вы запускаете dotnet new или dotnet build, dotnet ищет global.json, сначала в текущей папке, потом во всех родительских папках. Если global.json найден (и доступна версия SDK, указанная там!), то эта версия будет использована для всех запускаемых команд SDK внутри этой папки. Если не получилось найти ни один файл global.json, будет использована последняя доступная версия SDK, в моем случае, 2.0.0-preview1-005977.


Personally, I placed the above global.jsonin my folder Projectsand therefore all existing projects that are in it will continue to use SDK 1.0.0 (as well as all new projects that I create there). Then I created a subfolder netcore20and added the following global.json. In it, I place all the projects in which I want to "play" with the preliminary version of ASP.NET Core 2.0, without risking to get problems because of this!


{
  "sdk": {
    "version": "2.0.0-preview1-005977"
  }
}

Conclusion


Versioning has been one of the problems of .NET Core until recently. Aligning all versions in the future will certainly simplify the situation and, hopefully, this will cause less confusion. But it's still worth trying to understand the difference between runtime versions and SDK versions. I hope this post helped clarify some of these issues!


Also popular now: