237 .NET ON LINUX Get More Refcardz! Visit DZone.com/Refcardz
CONTENTS This refcard willguideyou alongthepathtobeing When .NETmadeitsdebut in2002, itsupported
development effort, languagesupportmust bere-in versions of .NET Core from your system your from Core of .NET versions as tipsforusingthenew CommandLineInterface find documentationanddiscussionsrelated to.NET productive using.NETCore onLinux, from installation (CLI). BuildingMVCweb sites, RESTful servicesand (VB). Over theyears, many languageshave beenadded multiple languages, includingC\#and Visual Basic to debugging. Informationisavailable tohelpyou to the.NETFramework. Because.NETCore isanew some toolsandhelpfulsettingsare discussed asthey standalone applicationsare alsocovered. Finally, relate toyour development efforts. CENTOS 7.1 (64 BIT) & ORACLE LINUX 7.1 BIT) (64 LINUX &ORACLE 7.1 BIT) (64 CENTOS Core. An architectural overview ispresented, aswell Before starting, you must remove any previous previous any remove must you starting, Before FEDORA 24 (64 BIT) (64 24 FEDORA D FEDORA 23 (64 BIT) (64 23 FEDORA INSTALLING .NET CORE 2.0 SDK ON LINUX ON SDK 2.0 CORE .NET INSTALLING ANY APP,ANY DEVELOPER, ANY PLATFORM sudo ln-s/opt/dotnet/dotnet /usr/local/bin -C /opt/dotnet sudo mkdir-p/opt/dotnet&& tarzxfdotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=848833 curl -sSL-odotnet.tar.gz sudo dnfinstalllibunwindlibicu sudo ln-s/opt/dotnet/dotnet/usr/local/bin -C /opt/dotnet sudo mkdir-p/opt/dotnet&&tarzxfdotnet.tar.gz fwlink/?LinkID=816869 curl -sSL-odotnet.tar.gzhttps://go.microsoft.com/ sudo dnfinstalllibunwindlibicu sudo ln-s/opt/dotnet/dotnet/usr/local/bin /opt/dotnet sudo mkdir-p/opt/dotnet&&tarzxfdotnet.tar.gz-C https://go.microsoft.com/fwlink/?LinkID=809130 curl -sSL-odotnet.tar.gz sudo apt-getinstallcurllibunwind8gettext sudo ln-s/opt/dotnet/dotnet/usr/local/bin -C /opt/dotnet sudo mkdir-p/opt/dotnet&&tarzxfdotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131 curl -sSL-odotnet.tar.gz sudo yuminstalllibunwindlibicu EBIAN 8 (64 BIT) 8(64 EBIAN Tag Helpers...andmore! dotnetCommands The Layers of.NETCore OpenSource Repositories Installing onLinux
.NET Core onLinux © DZONE, INC.
|
DZONE.COM
Visit the website website the Visit
Use the instructions for Ubuntu 14.04, replacing the the replacing 14.04, for Ubuntu instructions the Use the replacing 14.04, for Ubuntu instructions the Use first command with: command first with: command first O UBUNTU 14.04 / LINUX MINT 17 (64 BIT) (64 17 MINT /LINUX 14.04 UBUNTU UBUNTU 16.04 (64 BIT) (64 16.04 UBUNTU BIT) (64 18 MINT /LINUX 16.04 UBUNTU RED HAT ENTERPRISE LINUX 7 SERVER (64 BIT) BIT) (64 7SERVER LINUX HAT ENTERPRISE RED sudo apt-getupdate --recv-keys 417A0893 sudo apt-keyadv--keyserverapt-mo.trafficmanager.net etc/apt/sources.list.d/dotnetdev.list' trafficmanager.net/repos/dotnet-release/ trustymain">/ sudo sh-c'echo"deb[arch=amd64]https://apt-mo. scl enablerh-dotnet20bash echo ‘sourcescl_sourceenablerh-dotnet20’>>~/.bashrc yum installrh-dotnet20 yum installscl-utils subscription-manager repos--enable=rhel-7-server-dotnet-rpms subscription-manager attach--pool=<PoolId> (get thePoolIdtobeusedinnextstep) subscription-manager list--available yakkety main">/etc/apt/sources.list.d/dotnetdev.list’ https://apt-mo.trafficmanager.net/repos/dotnet-release/ sudo sh-c‘echo"deb[arch=amd64] etc/apt/sources.list.d/dotnetdev.list' trafficmanager.net/repos/dotnet-release/ xenialmain">/ sudo sh-c'echo"deb[arch=amd64]https://apt-mo. THER DISTROS THER DEVELOPERS.REDHAT.COM Join Today. It’s free. resources to helpyou code. Get access to products and dot.net . BY DONSCHENCK BROUGHT TO YOU IN PARTNERSHIP WITH .NET CORE 2.0 FOR RED HAT ENTERPRISE LINUX AND OPENSHIFT CONTAINER PLATFORM
Truly portable .NET applications are here with .NET Core 2.0. Develop microservices on your device of choice, and deploy them as Linux containers on Red Hat Enterprise Linux, OpenShift Container Platform, and more. Get started at RedHatLoves.NET 3 .NET ON LINUX
such as dynamic compilation and access to objects and OPEN SOURCE MEANS REPOSITORIES strings, etc., are included in this layer.
At GitHub.com: TOOLING Finally, the "add-on" part, completely separate from PROJECT DESCRIPTION REPOSITORY .NET Core yet used by many developers. Anything in .NET Core foundational CoreFX /dotnet/corefx/ Visual Studio or Visual Studio Code that makes it easier libraries. for the developer—Intellisense, a powerful autocomplete Core .NET Core runtime and base dotnet/coreclr/ helper, for example—is contained in this layer. runtime library (mscorlib). Client WCF libraries that allow THE \`DOTNET\` COMMAND WCF .NET Core applications to com- /dotnet/wcf/ municate with WCF services. After installing .NET Core, you’ll use the command- line tool dotnet, to do everything from creating code CLI Command-line tools. /dotnet/cli/ to publishing the application. Note that the default ASP.NET language is C\#, with support for F\# and VB included. ASP.NET MVC framework. /aspnet/mvc/ MVC USEFUL DOTNET COMMANDS Entity /aspnet/EntityFrame- Frame- Entity Framework data access. IF YOU WANT TO... USE THE COMMAND... work/ work See what version you have dotnet --version Templat- .NET templates for creating See more information about your /templating/ dotnet --info ing Engin code. CLI Create a basic "Hello World" con- THE LAYERS OF .NET CORE dotnet new console sole app .NET Core, and the associated pieces (ASP.NET, Entity Create a simple MVC web app dotnet new --type web Framework, etc) are separated into layers. While this may Create an VB "Hello World" console dotnet new --lang VB seem disjointed, in fact it’s a powerful move, one that app allows the developer to pick and choose the bits they want. Create a library dotnet new --type lib It also means faster development of updates, and will allow the developer to decide which versions to employ. Add a package reference to a dotnet add package project
Run an app in another location dotnet run --project <- path-to-project> Publish an app for deployment dotnet publish Get help dotnet --help
CREATING THE HELLO WORLD CONSOLE APP THE LAYERS EXPLAINED The following three commands will create and run a .NET CORE CLR console Hello World application. This is the runtime, the very foundation of .NET. Included are the just-in-time compiler and Virtual dotnet new console -- This creates the source code. Machine, type system, garbage collection, and more. The Program.cs file contains the working code. Note that dotnet restore is run automatically after creating .NET COREFX the code. The "System" libraries are here, things such as reflection This builds and runs the application. and streams, etc. When you reference, for example, dotnet run -- System.Console in your program, you’re using the core .NET libraries in this layer. It is at this layer where a A CLOSER LOOK AT THE DOTNET RESTORE COMMAND simple console application will run. The dotnet new command creates code (using templates .NET CLI that are being built into .NET Core), but the dotnet restore This is, simply, the command line interface (CLI). command will gather the dependencies necessary for your code to compile. If you are familiar with other ASPNET CORE languages, you know it is necessary to use their package The level that supports application development. Things
© DZONE, INC. | DZONE.COM 4 .NET ON LINUX
manager to retrieve the dependencies for your project. For When listing your dependencies in the *.csproj file, you example, you may use npm when developing in node.js in must also specify the version of the library. For example, order to fetch your dependencies.
When you run dotnet build, or if you run dotnet run and The NuGet configuration file, NuGet.Config, is located at it automatically creates a new binary, by default it will ~/.nuget/NuGet/NuGet.Config. This file contains a list of endpoints used when fetching dependencies. The create a DLL at the following location: endpoints can point to anywhere that can be reached ./bin/[configuration]/[framework]/[binary name] from your machine, including the internet and internal storage. The following lists the contents of the default Using the HelloWorld application in a directory labeled NuGet.Config file: "helloworld" as an example, running dotnet build would
You can override the default NuGet.Config file by storing a project-specific copy in your project’s root directory. For What if .NET Core is not installed on the target system? example, if your project is located at ~/src/mvc, you can In that case, you can build a self-hosting app, meaning create the file ~/src/mvc/NuGet.Config and it will be used you can compile it for the target operating system, then before the default file. distribute it. To run it, it does not need .NET Core installed on the target system; the dotnet publish command will copy all the necessary bits (libraries) into the target build WHERE ARE THE DEPENDENCIES STORED? By default, all dependencies are stored locally on your directory. You need only to copy the contents of that directory to another system and it will execute. machine at ~/.nuget/packages/. You can override this by specify a path in the environment variable **DOTNET_ PACKAGES** before running the dotnet restore command. Note that you can build for any OS from any OS. This As the dependencies are downloaded, a file is created powerful feature means you can build standalone (or updated if it already exists) in the root directory of applications for Windows from Linux, for MacOS from your project. The file, obj/project.assets.json, contains Windows, etc. a list of your dependencies. This list snapshot in time is used to when you run the dotnet restore command. You The details of creating a standalone application are can delete this file, but you’ll need to run dotnet restore covered later in this refcard. again before your next compile. The file is not needed to HOW TO CREATE A BASIC ASP.NET MVC WEBSITE run your application, only to build it. The dotnet net new command has options to allow you to build an ASP.NET MVC website. This is similar to using DEPENDENCY VERSIONS Visual Studio to create a new website and choosing the
© DZONE, INC. | DZONE.COM
5 .NET ON LINUX
MVC option. To build and run a simple MVC website, use TAG HELPER DEFINITION the following: Allows you to specify additional parameters for the controller, based on the name of the attribute. For exam- dotnet net new mvc asp-route- dotnet run ple, asp-route-id could be set by using asp-route- id="@ViewBag.ItemId".
You can now view the basic ASP.NET MVC website at Include files with the ability to use globbing, e.g. asp- asp-src-include http://localhost:5000. src-include="/scripts/**/*.js". Used in conjunction with asp-src-include to exclude asp-src-exclude Note that if you’re running Linux in a VM, the localhost file(s). will be accessible only inside the VM, and not from your Windows host machine. You can make the website ANOTHER TAG HELPER EXAMPLE accessible from the Windows host by using an environment
1. Enable Visual C++ iOS Development in Visual Studio 2015 Update 2 or newer. This is done by selecting BINDING TO A CONTROLLER ACTION WITH A TAG HELPER Visual Studio in the Windows Programs and Features