Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 2. Using .NET Core 2.1 on Red Hat Enterprise Linux 8


Learn how to install .NET Core 2.1 as well as create and publish .NET Core applications.

2.1. Installing .NET Core 2.1

.NET Core 2.1 is included in the AppStream repositories for RHEL 8. The AppStream repositories are enabled by default on RHEL 8 systems.

You can install the .NET Core 2.1 runtime with the latest 2.1 Software Development Kit (SDK). When a newer SDK becomes available for .NET Core 2.1, you can install it by running sudo yum update.

Prerequisites

Procedure

  • Install .NET Core 2.1 and all of its dependencies:

    $ sudo yum install dotnet-sdk-2.1 -y
    Copy to Clipboard Toggle word wrap

Verification steps

  • Verify the installation:

    $ dotnet --info
    Copy to Clipboard Toggle word wrap

    The output returns the relevant information about the .NET Core installation and the environment.

2.2. Creating an application using .NET Core 2.1

Learn how to create a C# hello-world application.

Procedure

  1. Create a new Console application in a directory called my-app:

    $ dotnet new console --output my-app
    Copy to Clipboard Toggle word wrap

    The output returns:

      The template "Console Application" was created successfully.
    
      Processing post-creation actions...
      Running 'dotnet restore' on my-app/my-app.csproj...
      Restoring packages for /home/username/my-app/my-app.csproj...
      Generating MSBuild file /home/username/my-app/obj/my-app.csproj.nuget.g.props.
      Generating MSBuild file /home/username/my-app/obj/my-app.csproj.nuget.g.targets.
      Restore completed in 224.85 ms for /home/username/my-app/my-app.csproj.
    
      Restore succeeded.
    Copy to Clipboard Toggle word wrap

    A simple Hello World console application is created from a template. The application is stored in the specified my-app directory.

    The directory includes the following files:

    $ tree my-app
    my-app
    ├── my-app.csproj
    ├── obj
    │   ├── my-app.csproj.nuget.dgspec.json
    │   ├── my-app.csproj.nuget.g.props
    │   ├── my-app.csproj.nuget.g.targets
    │   ├── project.assets.json
    │   └── project.nuget.cache
    └── Program.cs
    
    1 directory, 7 files
    Copy to Clipboard Toggle word wrap

Verification steps

  • Run the project:

    $ dotnet run --project my-app
    Copy to Clipboard Toggle word wrap

    The output returns:

    Hello World!
    Copy to Clipboard Toggle word wrap

2.3. Publishing applications using .NET Core 2.1

.NET Core 2.1 applications can be published to use a shared system-wide version of .NET Core or to include .NET Core.

The following methods exist for publishing .NET Core 2.1 applications:

  • Framework-dependent deployment (FDD) - The application uses a shared system-wide version of .NET. When publishing an application for RHEL, Red Hat recommends using FDD, because it ensures that the application is using an up-to-date version of .NET Core, built by Red Hat, that includes a specific set of native dependencies.
  • Self-contained deployment (SCD) - The application includes .NET. This method uses a runtime built by Microsoft.

Prerequisites

2.3.1. Publishing .NET Core applications

Procedure

  1. Publish the framework-dependent application:

    $ dotnet publish my-app -f netcoreapp2.1 -c Release
    Copy to Clipboard Toggle word wrap

    Replace my-app with the name of the application you want to publish.

  2. Optional: If the application is for RHEL only, trim out the dependencies needed for other platforms:

    $ dotnet restore my-app -r rhel.8-x64
    $ dotnet publish my-app -f netcoreapp2.1 -c Release -r rhel.8-x64 --self-contained false
    Copy to Clipboard Toggle word wrap

2.3.2. Publishing ASP.NET applications

When using the Microsoft SDK, ASP.NET Core 2.1 web applications are published with a dependency on the ASP.NET Core shared framework. This is a set of packages that are expected to be available on the runtime system.

When publishing on RHEL, these packages are included with the application. To include the packages using the Microsoft SDK, the MicrosoftNETPlatformLibrary property must be set to Microsoft.NETCore.App in the project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <MicrosoftNETPlatformLibrary>Microsoft.NETCore.App</MicrosoftNETPlatformLibrary>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1" />
  </ItemGroup>
</Project>
Copy to Clipboard Toggle word wrap

This property can be set when publishing the application.

$ dotnet publish -f netcoreapp2.1 -c Release -r rhel.8-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
Copy to Clipboard Toggle word wrap

2.4. Running .NET Core 2.1 applications in containers

Use the ubi8/dotnet-21-runtime image to run a precompiled application inside a Linux container.

Prerequisites

  • Preconfigured containers.

    The following example uses podman.

Procedure

  1. Create a new MVC project in a directory called mvc_runtime_example:

    $ dotnet new mvc --output mvc_runtime_example --no-restore
    Copy to Clipboard Toggle word wrap
  2. Restore and publish the project:

    $ dotnet restore mvc_runtime_example -r rhel.8-x64
    $ dotnet publish mvc_runtime_example -f netcoreapp2.1 -c Release -r rhel.8-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
    Copy to Clipboard Toggle word wrap
  3. Create the Dockerfile:

    $ cat > Dockerfile <<EOF
    FROM registry.access.redhat.com/ubi8/dotnet-21-runtime
    
    ADD bin/Release/netcoreapp2.1/publish/ .
    
    CMD ["dotnet", "mvc_runtime_example.dll"]
    EOF
    Copy to Clipboard Toggle word wrap
  4. Build your image:

    $ podman build -t dotnet-21-runtime-example .
    Copy to Clipboard Toggle word wrap
  5. Run your image:

    $ podman run -d -p8080:8080 dotnet-21-runtime-example
    Copy to Clipboard Toggle word wrap

Verification steps

  • View the application running in the container:

    $ xdg-open http://127.0.0.1:8080
    Copy to Clipboard Toggle word wrap
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat