Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Using .NET Core 2.0 and RHEL in Linux Containers

 

September 5, 2017
Don Schenck
Related topics:
.NET
Related products:
Red Hat Enterprise Linux

Share:

    .NET Core 2.0 represents the maturation of the .NET Core development effort. This, the third release (previous releases being version 1.0 and 1.1), brings nearly 20,000 more APIs and a much richer and deeper developer experience. To put it in the vernacular, .NET Core is ready for prime time.

    This blog post will show you the critical steps and configurations necessary to use .NET Core 2.0 running on RHEL inside your Linux containers.

    The Challenge

    Beginning with .NET Core 2.0, Microsoft is using meta-references to get the bits necessary for ASP.NET programs. Prior to version 2.0, you were required to list every individual library needed for your ASP.NET program to work. While the theory behind this idea seemed sound -- you'd only ask for the bits you really required -- in practice, there were some issues. For starters, you're basically going to want all the parts of ASP.NET. That's a broad statement, but more accurate than not.

    Secondly, and more problematic, it was, shall we call it a "challenge", to keep the references up-to-date and properly versioned. If you are referencing 15 libraries in one project, it's by definition 15 times more work to keep them all updated versus one lone reference.

    Consider the following .csproj file versus the generated file in the second example:

    vs2ocp.csproj file using ASP.NET meta-package:

    <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>

    <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
    </ItemGroup>
    <ItemGroup>
    <None Update="standalone.sh" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" />
    </ItemGroup>
    </Project>

    Generated code:

    <Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
    <TargetFramework Condition="'$()' == ''">netcoreapp2.0</TargetFramework>
    <TargetFramework Condition="'$()' != ''"></TargetFramework>
    <UserSecretsId Condition="'$(IndividualAuth)' == 'True' OR '$(OrganizationalAuth)' == 'True'">aspnet-vs2ocp-612A697F-5F13-4384-8F85-4B1214C705C6
    <WebProject_DirectoryAccessLevelKey Condition="'$(OrganizationalAuth)' == 'True' AND '$(OrgReadAccess)' != 'True'">0
    <WebProject_DirectoryAccessLevelKey Condition="'$(OrganizationalAuth)' == 'True' AND '$(OrgReadAccess)' == 'True'">1
    </PropertyGroup>
    <!--#if (IndividualLocalAuth && UseLocalDB) -->

    <!--#endif -->
    <ItemGroup Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True' ">
    <None Update="app.db" CopyToOutputDirectory="PreserveNewest" />
    </ItemGroup>

    <ItemGroup Condition="'$()' == ''">
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" Condition="'$(IndividualAuth)' == 'True'" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" Condition="'$(IndividualAuth)' == 'True'" />
    </ItemGroup>
    <ItemGroup Condition="'$()' != ''">
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" Condition="'$(IndividualAuth)' == 'True' OR '$(OrganizationalAuth)' == 'True'" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="2.0.0" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.0" Condition="'$(IndividualLocalAuth)' == 'True'" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" Condition="'$(IndividualLocalAuth)' == 'True'" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" Condition="'$(IndividualLocalAuth)' == 'True'" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True'" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' == 'True'" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" Condition="'$(IndividualLocalAuth)' == 'True'" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" Condition="'$(UseBrowserLink)' == 'True'" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" Condition="'$(IndividualAuth)' == 'True'" />
    </ItemGroup>

    <ItemGroup Condition="'$(NoTools)' != 'True'">
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" Condition="'$(IndividualLocalAuth)' == 'True'" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" Condition="'$(IndividualAuth)' == 'True' OR '$(OrganizationalAuth)' == 'True'" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
    </ItemGroup>

    </Project>

    As you can see, the meta package does a lot of the heavy lifting.

    To make things fast, this combination of libraries are rolled into the runtime store, a store of pre-compiled libraries. This makes installation and start-up much faster, as they are stored locally. They are assumed to be part of the .NET Core installation on the machine.

    The Core of the Challenge

    With .NET Core 2.0, one of the artifacts being shipped with .NET Core is the Runtime Package Store (which enables the creation of pre-compiled package cache, decreasing deployment size and start-up times). The goal of this artifact is to create smaller and faster deployments of ASP.NET applications.  However, it introduces an assumption: any ASP.NET application will assume that the Runtime Package Store is present with .NET Core. If the Runtime Package Store is missing, the application will fail at runtime.

    (More background details about the package store and how it works and what benefits it brings is available here: https://github.com/dotnet/designs/issues/8 ).

    For Red Hat, this introduces a problem. We currently do not build ASP.NET from source. As a result, the ASP.NET Runtime Package Store is not present in our .NET Core. Any ASP.NET application - no matter if they are built using our SDK or Microsoft's SDK - will, therefore, fail to run on top of our distribution of .NET Core.
     

    Is There A Solution In Sight?

    Yes. Red Hat and Microsoft are working very closely to streamline this situation. In the meantime...
     

    What's A Developer To Do?

    No worries; of course we have a solution. And, to be honest, it's No Big Deal. It involves a small script and adding one tiny section to your project (e.g. *.csproj) file.
     
    For starters, add this section to your project file; in this example case, this is example.csproj:
     
    <ItemGroup>
    <None Update="standalone.sh" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" />
    </ItemGroup>
     
    Notice the reference to the script file "standalone.sh". This file must also be added to your project with the following contents:
    #!/bin/bash
    ASSEMBLY=example.dll
    SCL=rh-dotnet20
    DIR="$(dirname "$(readlink -f "$0")")"
    scl enable $SCL -- dotnet "$DIR/$ASSEMBLY" "$@"
     
    Note that you must change the ASSEMBLY= line with the name of your application.
     

    Three Small Steps

    That's it for the tweaks necessary. In every case, you'll simply:
    1. Add the tiny section to your *.csproj file.
    2. Add the "standalone.sh" script to your project directory.
    3. Edit one line in that script.
     

    Building the Code

    After that, you use dotnet publish to prepare your code for containers, using the following command:
    dotnet publish -c Release -r rhel.7-x64 --self-contained=false

    Building an Image

    Finally, add a file, "Dockerfile", with the following contents -- again, adjusting the file name to fit your application:

    FROM registry.access.redhat.com/dotnet/dotnet-20-runtime-rhel7
    ADD bin/Release/netcoreapp2.0/rhel.7-x64/publish/. /app/
    WORKDIR /app/
    EXPOSE 5000
    CMD ["scl", "enable", "rh-dotnet20", "--", "dotnet",  "example.dll"]

    Running the Image in a Container

    Now we can build and run the application, such as this example:
    docker build -t example .
    docker run -d -p 5000:5000 --name example example
     

    Conclusion

    While there are some small changes needed to get your .NET Core 2.0 application running in a RHEL-based container, the payoff is worth the effort. As Cloud Native Computing and microservices become more common, this knowledge will allow you to contribute to this new frontier. If you want to learn more about this trend, check out Red Hat OpenShift, a Platform as a Service (PaaS) for running scalable microservices.


    Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

    Last updated: February 5, 2024

    Recent Posts

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    • How to integrate vLLM inference into your macOS and iOS apps

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue