0% found this document useful (0 votes)
3 views5 pages

DOTNET On Red Hat Enterprise Linux Cheat Sheet Developer2

Uploaded by

weiwuwei2233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

DOTNET On Red Hat Enterprise Linux Cheat Sheet Developer2

Uploaded by

weiwuwei2233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

developers.redhat.

com redhat-developer @rhdevelopers

Cheat sheet

.NET on Red Hat Enterprise Linux


The .NET Framework has been around since 2002, supporting Windows developers in many programming
languages. .NET Core, made available in 2016, is the new generation of the .NET platform, expanded to run on
Windows, macOS, and Linux. A Windows developer can now, for example, write code that runs on Red Hat Enterprise
Linux (https://developers.redhat.com/products/rhel/overview) with almost zero learning curve.

Desktop applications, phone apps, web sites, web services, and even Linux daemons can be written in one or more of
the supported .NET languages: C#, Visual Basic, and F#.

Because .NET can now run on Linux, the world of microservices has opened up to .NET developers as well. Running a
.NET program in a Linux container is as simple as it is in any other language.

This cheat sheet covers the basics of installing .NET on Red Hat Enterprise Linux (RHEL), how to get a simple
program running, and how to run a program in a Linux container. Finally, a list of valuable resources is included.

Installation
To install the .NET Core SDK on RHEL, use the following command:

sudo yum install -y dotnet-sdk-8.0.x86_64

Build options
When you build a .NET application, you have output options: Portable (the default) or Standalone.

Portable application
By default, running dotnet build will build your program as a Portable Application. This means the compiler will create
{appname}.dll ; for example, helloworld.dll . This DLL can then be copied to any machine hosting an installation
of .NET Core and it will run, regardless of operating system. For example, you could copy the DLL from a Mac onto a
Linux machine (which has .NET Core installed) and it will execute.
developers.redhat.com redhat-developer @rhdevelopers

Standalone application
.NET Core allows you to compile an application so that it will run on a machine that does not have .NET installed.
Further, you can cross-compile from any operating system to any operating system. For example, you can compile
your application on your Windows 10 machine and then install and run it on a Linux machine that does not have .NET
Core installed. This powerful feature of .NET Core does not exist in the previous .NET Framework. To create a
standalone application, you use the dotnet publish command.

Note that the standalone application does not create an .exe file. Like the Portable application, it creates a DLL that is
run by the dotnet host. The standalone application avoids the need for a .NET Core installation by copying all of the
necessary bits to the directory where you build the application. That is, you are not copying one file to the target; you
must copy the entire directory and any sub-directories.

The “Hello World” program


Get a simple “Hello World” program running in four commands:

mkdir helloworld
cd helloworld
dotnet new console
dotnet run

Helpful dotnet CLI commands

dotnet —help
The —-help flag will display help for the lowest-level available based on the dotnet command you’ve typed.
dotnet —help gives you broad help about the dotnet command, while dotnet new —help gives you more detailed help
about the dotnet new command, and so on.

dotnet new [options]


The dotnet new new command is used to create a new project. This function uses the dotnet templating engine, which
is continually evolving. It can be found at https://github.com/dotnet/templating
developers.redhat.com redhat-developer @rhdevelopers

For example, to create an MVC ASP.NET web site, you would run the following command:

dotnet new mvc

This creates a web site using C# as the language. Adding the —language F# flag will create source code in F#.

dotnet restore
The dotnet restore command will retrieve the dependency libraries needed for your application as defined in the
project file. By default, the command will download the libraries from https://www.nuget.org.

Note that dotnet restore will run automatically when building an application unless you use the -—no-restore flag.

dotnet add
The dotnet add command is used to insert a reference into your project file. While you can, if you wish, manually edit
your project file, this command makes it easier to add a reference. For example, if you wish to reference the NuGet
package “Json.NET” in your project, you would use the following command:

dotnet add package Newtonsoft.Json

.NET and Linux containers


You can run your .NET code in a Linux container just as you can any code that runs in Linux. This allows you to create
ready-to-run images that are isolated from the host operating system, can easily be transported, and start in just a
few seconds.

Simple web service


Consider the following very small web service. Using the default code that is generated using the dotnet new webapi
command, the following sections will describe how to create and run a Linux image.

Creating the source code


To create the source code, create a directory, move into it, and run the proper command:

mkdir myexample
cd myexample
dotnet new myexample
developers.redhat.com redhat-developer @rhdevelopers

Creating the build configuration


A configuration file, Containerfile, is required to build the Linux image using Podman. While you can use the
microsoft/dotnet image as the base image for your build, it won’t be using RHEL. To use RHEL to run your
application, use the appropriate Red Hat UBI images. Here’s an example Containerfile:

FROM registry.access.redhat.com/ubi8/dotnet-80-runtime AS base


WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM registry.access.redhat.com/ubi8/dotnet-80 AS build


USER 0
WORKDIR /src
COPY [“myexample.csproj”, “.”]
RUN dotnet restore “./myexample.csproj”
COPY . .
WORKDIR “/src/.”
RUN dotnet build “myexample.csproj” -c Release -o /app/build

FROM build AS publish


RUN dotnet publish “myexample.csproj” -c Release -o /app/publish/
p:UseAppHost=false

FROM base AS final


WORKDIR /app
COPY —from=publish /app/publish .
ENTRYPOINT [“dotnet”, “myexample.dll”]

Building the image


The following command will build the image. Note that the image name does not need to match the application name.

podman build -t myexample

Running the container


Use the following command to run the container:

podman run -d -p 8080:8080 myexample

You can see the results using the following command:

curl localhost:8080/weatherforecast
developers.redhat.com redhat-developer @rhdevelopers

Valuable resources
The following web sites are excellent sources of information regarding .NET Core development:

• dot.net: This vanity URL will redirect you to Microsoft’s official .NET Core page. From there you can download
and install .NET Core, find documentation, etc.

• live.asp.net: This URL will direct you to the weekly ASP.NET Core Community Standup, a live webcast (also
recorded and available on YouTube) of the newest happenings regarding ASP.NET and .NET Core.

• redhatloves.net: This vanity URL directs you to the .NET on Linux section of the Red Hat Developer program.
This program contains resources such as books, blog posts, videos, and free software—including the Red Hat
Development Suite. This suite includes RHEL, Virtual Box, Vagrant, docker, kubernetes, and OpenShift.

• github.com/dotnet: The GitHub home of the .NET Foundation.

• github.com/aspnet: The home of the ASP.NET Core code, including EntityFramework (which is included in this
repo but is not a part of ASP.NET).

OpenShift on your machine


You can install and run OpenShift on your local PC using Red Hat OpenShift Local. Get it at https://
developers.redhat.com/products/openshift-local/overview.

Developer Sandbox for Red Hat OpenShift


You can get no-cost access to a Red Hat-hosted OpenShift cluster via the Developer Sandbox for Red Hat
OpenShift. Visit https://developers.redhat.com/developer-sandbox to learn more.

You might also like