이 콘텐츠는 선택한 언어로 제공되지 않습니다.
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
Installed and registered RHEL 7 with attached subscriptions.
For more information, see Registering the System and Attaching Subscriptions.
Procedure
Enable the .NET Core software repositories:
sudo subscription-manager repos --enable=rhel-7-variant-dotnet-rpms
$ sudo subscription-manager repos --enable=rhel-7-variant-dotnet-rpmsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace variant with
server,workstationorhpc-nodedepending on what RHEL system you are running (RHEL 7 Server, RHEL 7 Workstation, or HPC Compute Node, respectively).Verify the list of subscriptions attached to your system:
sudo subscription-manager list --consumed
$ sudo subscription-manager list --consumedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Install the
scltool:sudo yum install scl-utils -y
$ sudo yum install scl-utils -yCopy to Clipboard Copied! Toggle word wrap Toggle overflow Install .NET Core 2.1 and all of its dependencies:
sudo yum install rh-dotnet21 -y
$ sudo yum install rh-dotnet21 -yCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
rh-dotnet21Software Collection environment:scl enable rh-dotnet21 bash
$ scl enable rh-dotnet21 bashCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can now run
dotnetcommands in thisbashshell session.If you log out, use another shell, or open up a new terminal, the
dotnetcommand is no longer enabled.WarningRed Hat does not recommend permanently enabling
rh-dotnet21because it may affect other programs. For example,rh-dotnet21includes a version oflibcurlthat differs from the base RHEL version. This may lead to issues in programs that do not expect a different version oflibcurl. If you want to enablerh-dotnetpermanently, addsource scl_source enable rh-dotnet21to your~/.bashrcfile.
Verification steps
Verify the installation:
dotnet --info
$ dotnet --infoCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
Create a new Console application in a directory called
my-app:dotnet new console --output my-app
$ dotnet new console --output my-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output returns:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A simple
Hello Worldconsole application is created from a template. The application is stored in the specifiedmy-appdirectory.The directory includes the following files:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
Run the project:
dotnet run --project my-app
$ dotnet run --project my-appCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output returns:
Hello World!
Hello World!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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-dotnet21Software Collection. -
Self-contained deployment (SCD) - The application includes .NET. This method uses a runtime built by Microsoft. Running applications outside the
rh-dotnet21Software Collection may cause issues due to the unavailability of native libraries.
Prerequisites
Existing .NET Core application.
For more information on how to create a .NET Core application, see Section 2.2, “Creating an application using .NET Core 2.1”.
2.3.1. Publishing .NET Core applications 링크 복사링크가 클립보드에 복사되었습니다!
Procedure
Publish the framework-dependent application:
dotnet publish my-app -f netcoreapp2.1 -c Release
$ dotnet publish my-app -f netcoreapp2.1 -c ReleaseCopy to Clipboard Copied! Toggle word wrap Toggle overflow Replace my-app with the name of the application you want to publish.
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
$ dotnet restore my-app -r rhel.7-x64 $ dotnet publish my-app -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ scl enable rh-dotnet21 -- dotnet <app>.dllCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can add the
scl enable rh-dotnet21 — dotnet <app>.dllcommand to a script that is published with the application.Add the following script to your project and update the
ASSEMBLYvariable:Copy to Clipboard Copied! Toggle word wrap Toggle overflow To include the script when publishing, add this ItemGroup to the
csprojfile:<ItemGroup> <None Update="<scriptname>" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" /> </ItemGroup><ItemGroup> <None Update="<scriptname>" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" /> </ItemGroup>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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.
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
$ dotnet publish -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
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
Create a new MVC project in a directory called
mvc_runtime_example:dotnet new mvc --output mvc_runtime_example --no-restore
$ dotnet new mvc --output mvc_runtime_example --no-restoreCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
$ 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.AppCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
Dockerfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build your image:
podman build -t dotnet-21-runtime-example .
$ podman build -t dotnet-21-runtime-example .Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you get an error containing the message
unable to retrieve auth token: invalid username/password, you need to provide credentials for theregistry.redhat.ioserver. Use the commandpodman login registry.redhat.ioto log in. Your credentials are typically the same as those used for the Red Hat Customer Portal.Run your image:
podman run -d -p8080:8080 dotnet-21-runtime-example
$ podman run -d -p8080:8080 dotnet-21-runtime-exampleCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
View the application running in the container:
xdg-open http://127.0.0.1:8080
$ xdg-open http://127.0.0.1:8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow