Chapter 1. .NET Core 2.0 on Red Hat Enterprise Linux

This Getting Started Guide describes how to install .NET Core 2.0 on Red Hat Enterprise Linux (RHEL). See Red Hat Enterprise Linux documentation for more information about Red Hat Enterprise Linux 7.

1.1. Install and Register Red Hat Enterprise Linux

  1. Install RHEL 7 using one of the following images:

  2. Register the system by following the appropriate steps in Registering, Unregistering, and Reregistering a System in the Red Hat Subscription Management document.

    You can also use the following command to register the system.

    $ sudo subscription-manager register
  3. Display a list of all subscriptions that are available for your system and identify the pool ID for the subscription.

    $ sudo subscription-manager list --available

    This command displays the subscription name, unique identifier, expiration date, and other details related to it. The pool ID is listed on a line beginning with Pool ID.

  4. Attach the subscription that provides access to the dotNET on RHEL repository. Use the pool ID you identified in the previous step.

    $ sudo subscription-manager attach --pool=<appropriate pool ID from the subscription>
  5. Enable the .NET Core channel for Red Hat Enterprise 7 Server, Red Hat Enterprise 7 Workstation, or HPC Compute Node with one of the following commands, respectively.

    $ sudo subscription-manager repos --enable=rhel-7-server-dotnet-rpms
    $ sudo subscription-manager repos --enable=rhel-7-workstation-dotnet-rpms
    $ sudo subscription-manager repos --enable=rhel-7-hpc-node-dotnet-rpms
  6. Verify the list of subscriptions attached to your system.

    $ sudo subscription-manager list --consumed
  7. Install the scl tool.

    $ sudo yum install scl-utils

1.2. Install .NET Core

  1. Install .NET Core 2.0 and all of its dependencies.

    $ sudo yum install rh-dotnet20 -y
  2. Enable the rh-dotnet20 Software Collection environment so you can run dotnet commands in the bash shell.

    $ scl enable rh-dotnet20 bash
    Note

    This command does not persist; it creates a new shell, and the dotnet command is only available within that shell. 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-dotnet20 because it may affect other programs. For example, rh-dotnet20 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 verison of libcurl. If you want to permanently enable rh-dotnet for yourself, add the following line to your ~/.bashrc file.

    source scl_source enable rh-dotnet20

  3. Run the following command to prove the installation succeeded.

    $ dotnet --help
    .NET Command Line Tools (2.0.0)
    Usage: dotnet [runtime-options] [path-to-application]
    Usage: dotnet [sdk-options] [command] [arguments] [command-options]
    
    path-to-application:
      The path to an application .dll file to execute.
    
    SDK commands:
      new              Initialize .NET projects.
      restore         Restore dependencies specified in the .NET project.
      run               Compiles and immediately executes a .NET project.
      build             Builds a .NET project.
      publish          Publishes a .NET project for deployment (including the runtime).
    ....

1.3. Create an Application

  1. If you want to run the classic "Hello World" test case, create the following directory.

    $ mkdir hello-world
  2. Navigate to the hello-world directory.

    $ cd hello-world
  3. Create a project.

    $ dotnet new console
    The template "Console Application" was created successfully.
    
    Processing post-creation actions...
    Running 'dotnet restore' on /home/<USER>/hello-world/hello-world.csproj...
    Restore succeeded.
  4. Pull the dependencies needed for the project.

    $ dotnet build
  5. Run the project.

    $ dotnet run
    Hello World!

1.4. Publish Applications

The .NET Core 2.0 applications can be published to use a shared system-wide version of .NET Core or to include .NET Core. These two deployment types are called framework-dependent deployment (FDD) and self-contained deployment (SCD), respectively.

For RHEL, we recommend publishing by FDD. This method ensures 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 dotnet Software Collection. On the other hand, SCD uses a runtime built by Microsoft and uses the global Red Hat Enterprise Linux libraries. There are known issues when using .NET Core with these libraries.

1.4.1. Publish .NET Core Applications

  1. Use the following command to publish an application using the FDD.

    $ dotnet publish -f netcoreapp2.0 -c Release
  2. If the application will only be used on RHEL, you can trim the dependencies needed for other platforms by using these commands.

    $ dotnet restore -r rhel.7-x64
    $ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 --self-contained false
  3. Enable the Software Collection and pass the application assembly name to the dotnet command to run the application on a RHEL system.

    $ scl enable rh-dotnet20 -- dotnet <app>.dll
  4. This command can be added to a script that is published with the application. Add the following script to your project and update the ASSEMBLY variable.

    #!/bin/bash
    
    ASSEMBLY=<app>.dll
    SCL=rh-dotnet20
    DIR="$(dirname "$(readlink -f "$0")")"
    
    scl enable $SCL -- dotnet "$DIR/$ASSEMBLY" "$@"
  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>

1.4.2. Publish ASP.NET Core Applications

  1. By default, ASP.NET Core 2.0 web applications are published with a dependency on a runtime store. This is a set of packages that are expected to be available on the runtime system. They are not included with the published application. RHEL does not include the ASP.NET runtime store, so the applications must be published, including all dependencies. This can be done by setting the PublishWithAspNetCoreTargetManifest property to false in the project file.

    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
      </ItemGroup>
    </Project>
  2. Alternatively, this property can be set when publishing the application.

    $ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 --self-contained false /p:PublishWithAspNetCoreTargetManifest=false

1.5. Run Applications on Docker

This section shows how to use the dotnet/dotnet-20-runtime-rhel7 image to run your application inside a Docker container. It requires you to have the docker binary installed, the docker daemon running, and the rh-dotnet20 Software Collection enabled.

  1. Create and navigate to the following directory.

    $ mkdir mvc_runtime_example && cd mvc_runtime_example
  2. Create a new project.

    $ dotnet new mvc --no-restore
  3. Pull all dependencies.

    $ dotnet restore -r rhel.7-x64
  4. Publish the project.

    $ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 \
    --self-contained false /p:PublishWithAspNetCoreTargetManifest=false
  5. Create a file called Dockerfile and add the following contents.

    $ cat > Dockerfile <<EOF
    FROM registry.access.redhat.com/dotnet/dotnet-20-runtime-rhel7
    
    ADD bin/Release/netcoreapp2.0/rhel.7-x64/publish/ .
    
    CMD ["dotnet", "mvc_runtime_example.dll"]
    EOF
  6. Build your image.

    $ sudo docker build -t dotnet-20-runtime-example .
  7. Run your image.

    $ sudo docker run -d -p8080:8080 dotnet-20-runtime-example
  8. View the result in a browser: http://127.0.0.1:8080

Report a bug