Hello world - .NET Core

    Follow the steps below to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the following commands:

    • A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
    • installed and running on your local machine, and a Docker Hub account configured (we’ll use it for a container registry).
    • You have installed .NET Core SDK 3.1.
    1. First, make sure you have installed:

      1. dotnet --version
      2. 3.1.100
    2. From the console, create a new empty web project using the dotnet command:

      1. dotnet new web -o helloworld-csharp
    3. Update the CreateHostBuilder definition in Program.cs by adding .UseUrls() to define the serving port:

      1. public static IHostBuilder CreateHostBuilder(string[] args)
      2. {
      3. string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
      4. string url = String.Concat("http://0.0.0.0:", port);
      5. return Host.CreateDefaultBuilder(args)
      6. .ConfigureWebHostDefaults(webBuilder =>
      7. {
      8. webBuilder.UseStartup<Startup>().UseUrls(url);
      9. });
      10. }
    4. Update the app.UseEndpoints(...) statement in Startup.cs to read and return the TARGET environment variable:

      1. FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
      2. WORKDIR /app
      3. # Install production dependencies.
      4. # Copy csproj and restore as distinct layers.
      5. COPY *.csproj ./
      6. RUN dotnet restore
      7. # Copy local code to the container image.
      8. COPY . ./
      9. WORKDIR /app
      10. # Build a release artifact.
      11. RUN dotnet publish -c Release -o out
      12. # Use Microsoft's official runtime .NET image.
      13. FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
      14. WORKDIR /app
      15. COPY --from=build /app/out ./
      16. # Run the web service on container startup.
      17. ENTRYPOINT ["dotnet", "helloworld-csharp.dll"]
    5. Create a .dockerignore file to ensure that any files related to a local build do not affect the container that you build for deployment.

      1. Dockerfile
      2. README.md
      3. **/obj/

    Once you have recreated the sample code files (or used the files in the sample folder) you’re ready to build and deploy the sample app.

    1. Use Docker to build the sample code into a container. To build and push with Docker Hub, run these commands replacing {username} with your Docker Hub username:

    2. After the build has completed and the container is pushed to docker hub, you can deploy the app into your cluster. Ensure that the container image value in service.yaml matches the container you built in the previous step. Apply the configuration using kubectl:

      1. kubectl apply --filename service.yaml
      • Create a new immutable revision for this version of the app.
      • Network programming to create a route, ingress, service, and load balance for your app.
      • Automatically scale your pods up and down (including to zero active pods).
    3. To find the URL for your service, use

      1. kubectl get ksvc helloworld-csharp --output=custom-columns=NAME:.metadata.name,URL:.status.url
      2. NAME URL
      3. helloworld-csharp http://helloworld-csharp.default.1.2.3.4.xip.io
    4. Now you can make a request to your app and see the result. Replace the URL below with the URL returned in the previous command.

      1. curl http://helloworld-csharp.default.1.2.3.4.xip.io

    To remove the sample app from your cluster, delete the service record:

    Was this page helpful?

    Glad to hear it! Please tell us how we can improve.