이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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


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

2.1. Installing .NET Core 2.1

To install .NET Core on RHEL 7 you need to first enable the .NET Core software repositories and install the scl tool.

Prerequisites

Procedure

  1. Enable the .NET Core software repositories:

    $ sudo subscription-manager repos --enable=rhel-7-variant-dotnet-rpms
    Copy to Clipboard Toggle word wrap

    Replace variant with server, workstation or hpc-node depending on what RHEL system you are running (RHEL 7 Server, RHEL 7 Workstation, or HPC Compute Node, respectively).

  2. Verify the list of subscriptions attached to your system:

    $ sudo subscription-manager list --consumed
    Copy to Clipboard Toggle word wrap
  3. Install the scl tool:

    $ sudo yum install scl-utils -y
    Copy to Clipboard Toggle word wrap
  4. Install .NET Core 2.1 and all of its dependencies:

    $ sudo yum install rh-dotnet21 -y
    Copy to Clipboard Toggle word wrap
  5. Enable the rh-dotnet21 Software Collection environment:

    $ scl enable rh-dotnet21 bash
    Copy to Clipboard Toggle word wrap

    You can now run dotnet commands in this bash shell session.

    If you log out, use another shell, or open up a new terminal, the dotnet command is no longer enabled.

    Warning

    Red Hat does not recommend permanently enabling rh-dotnet21 because it may affect other programs. For example, rh-dotnet21 includes a version of libcurl that differs from the base RHEL version. This may lead to issues in programs that do not expect a different version of libcurl. If you want to enable rh-dotnet permanently, add source scl_source enable rh-dotnet21 to your ~/.bashrc file.

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. These native libraries are part of the rh-dotnet21 Software Collection.
  • Self-contained deployment (SCD) - The application includes .NET. This method uses a runtime built by Microsoft. Running applications outside the rh-dotnet21 Software Collection may cause issues due to the unavailability of native libraries.

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.7-x64
    $ dotnet publish my-app -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false
    Copy to Clipboard Toggle word wrap
  3. Enable the Software Collection and pass the application assembly name to the dotnet to run the application on a RHEL system:

    $ scl enable rh-dotnet21 -- dotnet <app>.dll
    Copy to Clipboard Toggle word wrap
  4. You can add the scl enable rh-dotnet21 — dotnet <app>.dll command to a script that is published with the application.

    Add the following script to your project and update the ASSEMBLY variable:

    #!/bin/bash
    
    APP=<app>
    SCL=rh-dotnet21
    DIR="$(dirname "$(readlink -f "$0")")"
    
    scl enable $SCL -- "$DIR/$APP" "$@"
    Copy to Clipboard Toggle word wrap
  5. To include the script when publishing, add this ItemGroup to the csproj file:

    <ItemGroup>
        <None Update="<scriptname>" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" />
    </ItemGroup>
    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.7-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 dotnet/dotnet-21-runtime-rhel7 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.7-x64
    $ dotnet publish mvc_runtime_example -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
    Copy to Clipboard Toggle word wrap
  3. Create the Dockerfile:

    $ cat > Dockerfile <<EOF
    FROM registry.redhat.io/dotnet/dotnet-21-runtime-rhel7
    
    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
    Note

    If you get an error containing the message unable to retrieve auth token: invalid username/password, you need to provide credentials for the registry.redhat.io server. Use the command podman login registry.redhat.io to log in. Your credentials are typically the same as those used for the Red Hat Customer Portal.

  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
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat