Create and run a sample .NET application

You can create a basic "Hello World" application to verify that your .NET development tools are properly configured, and then publish and deploy the application by using one of several available methods.

Create an application by using .NET 10.0

To verify that your development tools are properly configured, you can create a basic "Hello World" application. This process tests the core functionality of the .NET SDK, which ensures that you can successfully generate, build, and run projects from the command line.

Before you begin

  • Installed .NET 10.0 SDK

Procedure

  1. Create a new console application in a directory named <my-app>:
    $ dotnet new console --output <my-app>
  2. Run the application from its project directory:
    $ dotnet run --project <my-app>

Results

  • Verify that the application runs and displays the expected output:
    Hello World!

Publish .NET 10.0 applications

To publish a .NET 10.0 application, you can choose one of several deployment methods.

Before you begin

  • A .NET application to publish

About this task

The following deployment methods are available:

  • Self-contained deployment (SCD): The application includes the .NET runtime. This method uses a runtime built by Microsoft.
  • Framework-dependent deployment (FDD): The application uses a shared, system-wide version of the .NET runtime.
Note
Red Hat recommends using FDD when publishing an application for Red Hat Enterprise Linux. FDD ensures the application uses an up-to-date .NET runtime that is built by Red Hat and includes tested native dependencies.

Procedure

  1. Publish the framework-dependent application:
    $ dotnet publish <my-app> -f net10.0
  2. Optional: If the application is for RHEL only, trim out the dependencies needed for other platforms:
    $ dotnet publish <my-app> -f net10.0 -r rhel.10-<architecture> --self-contained false

    Replace <architecture> with one of the following values based on your platform:

    • x64 for 64-bit Intel/AMD
    • arm64 for 64-bit Arm
    • s390x for 64-bit IBM Z and LinuxONE
    • ppc64le for 64-bit PPC (Little Endian)

Results

  1. Verify that the application was published by listing the contents of the publish directory:
    $ ls <my-app>/bin/Debug/net10.0/publish/

Deploy applications by using Source-to-Image

To deploy a .NET application from source, you can use the .NET SDK image with the Source-to-Image (S2I) toolkit in the OpenShift Client (oc).

Before you begin

  • You have access to an OpenShift cluster.
  • You have the OpenShift Client (oc) installed and are logged in to the cluster.
  • You have the .NET 10.0 image streams installed on the cluster. See Installing .NET image streams.

Procedure

  1. Create a new OpenShift project:
    $ oc new-project <sample-project>
  2. Add the ASP.NET Core application to the project::
    $ oc new-app --name=<example-app> 'dotnet:10.0~https://github.com/redhat-developer/s2i-dotnetcore-ex#dotnet-10.0' --build-env DOTNET_STARTUP_PROJECT=app
  3. Track the progress of the build:
    $ oc logs -f bc/<example-app>
  4. After the build completes, view the deployed application:
    $ oc logs -f dc/<example-app>

    The application is now accessible within the project.

  5. Optional: Make the project accessible externally:
    $ oc expose svc/<example-app>
  6. Obtain the shareable URL for the route:
    $ oc get routes