.NET Core 1.0

.NET Core is a version of .NET Framework (first versions were released in the summer of 2016) that denotes a major breakthrough in the Microsoft Development Technologies ecosystem, the greatest promise being its ability to execute cross-platform: Windows, MacOS, and Linux.

Besides, .NET Core is modular, open source, and cloud-ready. It can be deployed along with the application itself, minimizing installation issues.

Although the number was consecutive to previous versions initially, Microsoft decided to restart the numbering, reinforcing the idea that this is a totally new concept with respect to classical versions, as a better way to avoid ambiguities. For those who were already aware of the initial versions, let's remember that the equivalence is as follows (refer to the screenshot):

.NET Core 1.0

The screenshot shows the equivalence between the new names and how some technologies go beyond platforms and (as it happens in ASP.NET Core or MVC Core) can even execute over the classic platform (.NET Framework 4.6).

.NET Core is based on CoreCLR, which is a lightweight runtime, providing the basic services. This includes automatic memory management, garbage collection, and the basic type libraries.

.NET Core, as with many other projects now, is part of the .NET foundation.

It also includes CoreFx, which is a set of modular assemblies. These assemblies can be added to your project as per your demand (remember that in .NET 4.x, we always had to make the entire BCL available). Now, you select only the assemblies that you need.

The list of supported environments

According to C# Corner's .NET Core - Fork In The Road (http://www.c-sharpcorner.com/article/net-core-fork-in-the-road/), the following table explains availability for distinct platforms, although the list is continuously growing:

The list of supported environments

Another target of .NET Core is to achieve project unification by means of a unique project.json file, in which all configuration features will appear independently of the type of project being built (no more app.config, web.config, and so on). However, in Visual Studio 2017, the dependencies declared in the project.json file have been moved to the .sln file for unification.

.NET Core is supposed to be built on four parts, including Core FX, Core CLR, Core RT, and Core CLI. Let's take a quick look these parts, one by one.

Core FX

Core FX contains the implementation of the foundational libraries, including the classic namespaces: System.Collections, System.IO, System.Xml, and so on. However, it doesn't include the base types, which are part of mscorlib, in a different repo, CoreCLR.

You can access these repos in GitHub at https://github.com/dotnet/corefx.

Core CLR

Core CLR is actually the .NET virtual machine (the runtime). It includes RyuJIT (or CLR JIT), which is a new generation 64-bit compiler, the .NET Garbage Collector, the mscorlib.dll previously mentioned, and a bunch of libraries.

The repo is available at https://github.com/dotnet/coreclr, and you'll also find all the related documentation there.

It is deployed along with your application (so no more .NET Framework x.x version required messages) and allows side-by-side execution; therefore, it guarantees the integrity of other existing applications.

Core RT

Core RT is an alternative to Core CLR, optimized for AoT (Ahead of Time) scenarios. It's available at the repo at https://github.com/dotnet/corert.

Obviously, you might be wondering about this term (AoT) and the difference with respect to the JIT compilation that we've been using.

Let's remember that a JIT compiler is responsible for converting MSIL code into native code. And this is done at runtime; so, every time a method is called for the first time, it is compiled and executed.

In this manner, the application can be executed in distinct CPUs and OSes with the runtime installed, but the caveat is that it's a process that takes time and has an impact on the application's performance.

On the other hand, AoT compilers also compile MSIL to native code, but Wikipedia says that they do it, reducing the runtime overhead, into a native (system-dependent) machine code with the intention of executing the resulting binary file natively.

Wikipedia also adds this:

"In most situations with fully AOT compiled programs and libraries it is possible to drop a considerable fraction of theruntime environment, thus saving disk space, memory, battery and startup times (no JIT warmup phase), etc. Because of this, it can be useful in embedded or mobile devices."

As RobJb points out in StackOverflow:

"AOT compilers can also spend as much time optimizing as they like, whereas JIT compilation is bound by time requirements (to maintain responsiveness) and the resources of the client machine. For this reason, AOT compilers can perform a complex optimization that would be too costly during JIT."

To summarize, the emphasis of CoreRT is on code optimization and conversion into a specific native platform. The generated executable will greater in size, but it contains the application, all its dependencies, plus the CoreRT.

Applications that use CoreRT execute faster and can use the proper optimizations of a native compiler, favoring better performance and code quality improvement.

Core CLI

Core CLI is a command line interface, independent from other libraries, providing an easy way to install a basic framework where we can test .NET Core code on any platform in only a few steps.

The installation is simple: files of types MSI in Windows, PKG in MacOS, or and apt-get in Linux; or they may even use a curl script.

Besides, a re-platform of ASP.NET over .NET Core 1.0 has been created, as we'll see later. The project file will be a .xproj file, with no differences between flavors or languages.

Once installed, you can emit commands such as dotnetbuild, for example, and generate the results and see the execution. One point to note is that the Core CLI itself is made using Core RT; so it uses optimized native technology as well.

Installation of .NET Core

The .NET Core installation has changed since the first release candidates, and now the previous location at GitHub will lead us to the https://www.microsoft.com/net/core site, where we'll find instructions to download .NET Core in four different contexts: Windows, Linux, MacOS, and Docker.

Note

Also, note that in order to work with .NET Core from Visual Studio 2015, you need to have Upgrade 3 installed. It will appear as an option under the Extensions and Updates in the Updates section.

Besides this installation, you can use .NET Core inside Visual Studio 2015 and higher versions if you additionally install NET Core 1.0.0 – VS 2015 Tooling, available at the same page. It takes a few minutes and asks for a confirmation (refer to the screenshot):

Installation of .NET Core

Note that, at the time of writing this, the VS 2015 Tooling is available as a preview version and will probably be in its final version by the time you read this. Additionally, you can install the Core CLI from the same page mentioned earlier or go directly to the https://github.com/dotnet/cli page.

Once the installation is complete, if we go to Visual Studio and select New Project, we'll see a new section called NET Core, offering three types of applications: Class Library, Console Application, and ASP.NET Core Web Application:

Installation of .NET Core

As you can imagine, in the fist case, we can create a DLL to be used by other projects, and the two latest options are the ones that make sense (for the moment) with this initial version of .NET Core.

If we take a look at the files created with the Console Application option, the Solution Explorer will show us a familiar structure with some differences, though.

To start with, we see the presence of two main directories: one for the solution (which includes a global.json file) and another called src, where we find the rest of the assets of our application.

The global.json file contains the folders that should be searched at compile-time when resolving dependencies for projects. The build system will only search top-level child folders.

By default, the following content is included:

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

This defines two projects in our solution: the standard one and another for testing. Besides this, the sdk key indicates the version to be used (1.0.0-preview-003121), and we can add or change that at will.

Note

A very interesting aspect of the tooling in Visual Studio 2015, when dealing with .json files for configuration is that if we change any value, the corresponding reference will be searched online automatically and downloaded to our project.

There are other options available, such as the architecture to be used (x64 / x86) or the target runtime, as shown in the next screenshot:

Installation of .NET Core

Within the src directory, the typical console structure can be found, only that all references included in the References section point to Microsoft.NETCore.App (1.0.0) and include a long list of components available, all of them in a hierarchical structure of dependencies.

The aspect of the main Program.cs file is just the usual in these projects (no changes), and this is true for AssemblyInfo.cs (although some values would be ignored on other platforms).

However, there's no app.config file. This file has been replaced with another .json file, project.json, which will be in charge of the definitions of the application's configuration from now on (remember that this has changed in Visual Studio 2017, where the .sln file is used to declare dependencies).

And, as it happens with the global.json file, the editor recognizes the values assigned to the keys and provides Intellisense here as well, with interesting hints about the possible values to configure (the next screenshot includes the initial list of references and Intellisense in action within the project.json file):

Installation of .NET Core

Now, I'm going to use a simple code snippet to explore how some of the common namespaces are implemented in .NET Core. In this case, we have three text files located in the same directory as the application (could be any directory, of course), and we're going to search for them, read their content, and present it in the console.

So we have some very simple code in the program.cs file, which serves as the entry point of the application:

staticstring pathImages = @"<Your Path to files>;
staticvoid Main(string[] args)
{
  Console.WriteLine(" Largest number of Rows: " + Console.LargestWindowHeight);
  Console.WriteLine(" Largest number of Columns: " + Console.LargestWindowWidth);
  Console.WriteLine(" ------------------------------
");
  ReadFiles(pathImages);
  Console.ReadLine();
}

privatestaticvoid ReadFiles(string path)
{
  DirectoryInfo di = newDirectoryInfo(path);
  var files = di.EnumerateFiles("*.txt",
  SearchOption.TopDirectoryOnly).ToArray();
  foreach (var item in files)
  {
    Console.WriteLine(" "+ File.ReadAllText(item.FullName));
  }
}

We can compile the program as always, and at runtime, we should see the output, only working on the .NET Core infrastructure (refer to the next screenshot):

Installation of .NET Core

As you can see, the functionalities, code, libraries, and namespaces used are just the same as the ones we would use in a standard Console application—only now, we're using the .NET Core 1.0 libraries and architecture.

However, a look at the code (and the output) will probably call your attention, since the name of the executable that we see in the output window is dotnet.exe and not NETCoreConsoleApp1 (the name we gave to our solution).

The reason for this has to do with the complexity linked to this model. The application is thought to be executable on distinct platforms. The default option allows the deployment architecture to determine the best way to configure the JIT compilers depending on the target. This is why the execution is undertaken by the dotnet runtime (named dotnet.exe).

In NET Core, two types of applications are defined: portable and self-contained. As the official documentation states:

"Portable applications are the default type in .NET Core. They require .NET Core to be installed on the targeted machine in order for them to run. To you as a developer, this means that your application is portable between installations of .NET Core.

A self-contained application does not rely on any shared component to be present on the machine where you want to deploy the application. As its name implies, it means that the entire dependency closure, including the runtime, is packaged with the application. This makes it larger, but also makes it capable of running on any .NET Core supported platforms with the correct native dependencies, whether it has .NET Core installed or not. This makes it that much easier to deploy to the target machine since you only deploy your application."

The default configuration we're using is the portable one. Where is this configuration established? In the project.json dependencies section, you'll see that there is a "type":"Platform" entry. That's what indicates this execution model.

Actually, the resulting assembly is a DLL, as you can see by watching the bin/debug directory after compilation. In our case, this DLL is only 6 Kb long.

What about the other choice? Well, if you know that you're going to target a certain platform, you can eliminate the previously mentioned entry in the project.json file (that's first). Second, you should leave the Microsoft.NET Core.App dependency, since it will retrieve all of the rest of the required components. Finally, it will be necessary to indicate (in the runtimes node) those that you want to use.

So, I changed the project.json file to appear with this configuration:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0"
    }
  },
  "runtimes": {
    "win10-x64": {}
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

Now the compiler behaves differently: it generates a new folder (dependent on the debug folder), containing a real native executable, which contains all the required elements to run in any platform of that type (win10-x64, in our demo).

After compilation, you'll see new files appearing, and one of them will be an executable file now. If you move to that folder in the Explorer, you'll see that there's a new file named NETCoreConsoleApp1.exe, which is the standalone executable. Also, this new file is larger than the DLL since it contains all the requirements (refer to the screenshot):

Installation of .NET Core

Note

There is an exhaustive explanation of all possible configuration options at https://docs.microsoft.com/es-es/dotnet/articles/core/tools/project-json.

The CLI interface

As we mentioned earlier, another choice available to develop these type of applications is now a command-line interface offered by the standalone installation of Core CLI or by the previous installation I made (the DotNetCore.1.0.0 - VS2015Tools.Preview2.0.1 file).

Several preconfigured command-line windows are made available depending on the platform to target under the generic name of Cross Tools Command Prompt. Just open the one that corresponds to your target platform and proceed as follows.

You can use the initial, basic demo mode that Microsoft has prepared as a start up with this tool. After opening Command Prompt, create a new directory that will serve as the root for a new project. In my case, I do it in a new C:devhello_world directory (among other things, to avoid some security issues that might arise when using the C: root directory).

At this point, you can ask for help by just typing dotnet –help, as shown in the following screenshot:

The CLI interface

To create a new project over this location, type dotnet new. Core CLI will download all the required components to your directories, including a basic application template, which holds a program.cs file with the classic Hello World console application, along with the default project.json file.

From this point, you can also open the project with Visual Studio Code (any platform, remember) and make the desired changes. The dot (.) indicates the IDE to use the current directory as the solution's directory.

The next step is to call dotnet restore. The result is that NuGet gets called in order to restore the tree dependencies defined in project.json and creates a variant of this file called project.lock.json, which is required if you want to be able to compile and run (if you open this file, you'll see that it is pretty large).

The official documentation defines this file as:

"A persisted and complete set of the graph of NuGet dependencies and other information describing an app. This file is read by other tools, such as dotnet build and dotnet run, enabling them to process the source code with a correct set of NuGet dependencies and binding resolutions."

From here, several options are available. You can launch a dotnet build command, which will build the application and generate a directory structure similar to the one we saw in Visual Studio. It will not run; it will only generate the resulting files.

The alternative option is to call dotnet run. With this command, the build option is called, and next, it launches execution; so, you should see something like this:

The CLI interface

And, of course, it's a good practice to take a look at the resulting files, which will be located in a subdirectory of the debug file, just like in our Visual Studio app:

The CLI interface

If you're curious, you can change the project.json file to generate the standalone executable, just like we did earlier, and the results should be equivalent.

Well, up until this point, we've seen an introduction to the .NET Core 1.0, but that's not the only development model .NET Core supports. Let's take a look at the—very interesting—ASP.NET Core 1.0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.204