Professional C# 6 and .NET Core 1.0. Christian Nagel

Читать онлайн.
Название Professional C# 6 and .NET Core 1.0
Автор произведения Christian Nagel
Жанр Зарубежная образовательная литература
Серия
Издательство Зарубежная образовательная литература
Год выпуска 0
isbn 9781119096634



Скачать книгу

project.json. This file is in JavaScript Object Notation (JSON) format and defines the framework application information such as version, description, authors, tags, dependencies to libraries, and the frameworks that are supported by the application. The generated project configuration file is shown in the following code snippet (code file HelloWorldApp/project.json):

      With the buildOptions settings, the emitEntryPoint is set. This is necessary if you create a Main method as a program entry point. This Main method is invoked when you run the application. This setting is not needed with libraries.

      With the dependencies section, you can add all dependencies of the program, such as additional NuGet packages needed to compile the program. By default, NetStandard.Library is added as a dependency. NetStandard.Library is a reference NuGet package – a package that references other NuGet packages. With this you can avoid adding a lot of other packages, such as System.Console for the Console class, System.Collections for generic collection classes, and many more. NetStandard.Library 1.0 is a standard that defines a list of assemblies that all .NET platforms must support. At the website https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/standard-platform.md you can find a long list of assemblies and their version numbers that are part of 1.0 and the assemblies that are added with 1.1, 1.2, 1.3, and 1.4 of the .NET standard.

      Having a dependency on NetStandard.Library 1.0, you can support the .NET Framework 4.5.2 and up (support for .NET 4, 4.5, 4.5.1 ended in January 2016),NET Core 1.0, the UWP 10.0, and other .NET Frameworks such as Windows Phone Silverlight 8.0, Mono, and Mono/Xamarin. Changing to version 1.3 restricts the support to .NET 4.6,NET Core 1.0, UWP 10.0, and Mono/Xamarin platforms. Version 1.4 restricts support to .NET 4.6.1,NET Core 1.0, and Mono/Xamarin platforms, but you get newer versions and a larger list of assemblies available.

      The frameworks section in project.json lists the .NET Frameworks that are supported by your application. By default, the application is only built for .NET Core 1.0 as specified by the netstandardapp1.5 moniker. netstandardapp1.5 is used with applications built for .NET Core. With libraries, you can use the moniker netstandard1.0. This allows using the library both from .NET Core applications and applications using the .NET Framework. The imports section within netstandardapp1.5 references the older name dnxcore50, which maps the old moniker to the new one. This allows packages that still use the old name to be used.

      .NET Core is the new open source version of the framework that is available on Windows, Linux, and OS X. The runtime that should be supported needs to be added to the runtimes section. The previous code snippet shows support for the Ubuntu Linux distribution, Windows 7 (which also allows running the app on Windows 8), Windows 10, and OS X.

      Adding the string net46, the program is built for the .NET Framework, version 4.6, as well:

      Adding net46 to the frameworks section also results in no more support for non-Windows runtimes, and thus you need to remove these runtimes.

      You can also add additional metadata, such as a description, author information, tags, project, and license URL:

      As you add multiple frameworks to the project.json file, you can specify dependencies that are specific to every framework in a dependencies section below the framework. The dependencies specified in the dependencies section that is at the same hierarchical level as the frameworks section specify the dependencies common to all frameworks.

      After having the project structure in place, you can download all dependencies of the application using the command

      while your command prompt is positioned in the same directory where the project.json file resides. This command downloads all dependencies needed for the application, as defined in the project.json file. Specifying the version 1.0.0-* gets version 1.0.0 and the latest available version for the *. In the file project.lock.json you can see what NuGet packages with which version were retrieved, including dependencies of dependencies. Remember, the packages are stored in a user-specific folder.

      To compile the application, start the command dotnet build and you can see output like this – compiling for .NET Core 1.0 and .NET Framework 4.6:

      As a result of the compilation process, you find the assembly containing the IL code of the Program class within the bin/debug/[netstandardapp1.5|net46] folder. If you compare the build of .NET Core with .NET 4.6, you will find a DLL containing the IL code with .NET Core, and an EXE containing the IL code with .NET 4.6. The assembly generated for .NET Core has a dependency to the System.Console assembly, whereas the .NET 4.6 assembly finds the Console class in the mscorlib assembly.

      You can also compile the program to native code using this command line:

      Compiling to native code results in a faster startup of the application as well as less memory consumption. The native compilation process compiles the IL code of the application as well as all dependencies to a single native image. Don’t expect that all functionality of .NET Core will be available to compile to native code, but as time continues and development from Microsoft proceeds, more and more applications can be compiled to native code.

      To run the application, you can use the dotnet command

      To start the application using a specific version of the framework, you can use the option – framework. This framework must be configured with the project.json file:

      You can also run the application starting the executable that you can find in the bin/debug directory.

      NOTE As you’ve seen building and running the Hello, World app on Windows, the dotnet tools work the same on Linux and OS X. You can use the same dotnet commands on either platform. Before using the dotnet commands, you just need to prepare the infrastructure using the sudo utility for Ubuntu Linux and install a PKG package on OS X as described at http://dotnet.github.io. After installing the .NET Core CLI, you can use the dotnet tools in the same way as you’ve seen in this section – with the exception that the .NET Framework 4.6 is not available. Other than that, you can restore NuGet packages and compile and run the application with dotnet restore, dotnet build, and dotnet run.

      The focus of this book is on Windows, as Visual Studio 2015 offers a more powerful development platform than is available on the other platforms, but many code samples from this book are based on .NET Core, and you will be able to run them on other platforms as well. You can also use Visual Studio Code, a free development environment, to develop applications directly on Linux and OS X. See the section “Developer Tools” later in this chapter for more information about different editions of Visual Studio.

      Packaging and Publishing the Application

      With the dotnet tool you can also create a NuGet package and publish the application for deployment.

      The command dotnet pack creates a NuGet package that you can put on a NuGet server. Developers can now reference the package using this command:

      Running this command with the HelloWorldApp creates the file HelloWorldApp.1.0.0.nupkg that contains the assemblies for all supported frameworks. A NuGet package is a ZIP file. If you rename this file with a .zip extension, you can easily look into it to see the content. With the sample app, two folders are created named dnxcore500 and net46 that contain the respective assemblies. The file HelloWorldApp.nuspec is an XML file that describes the NuGet package, lists the content for supported frameworks, and lists assembly dependencies that are required before the NuGet package can be installed.

      To