A unified .NET

.NET 5.0 was released just a few days ago with many new features, improvements, C# 9 support, F# 5 support, and more. .NET 5.0 is the first release of the unified .NET vision that was announced last year. Going forward, there will be just one .NET targeting Windows, Linux, macOS, and more. 

Google Cloud already has support for different versions of .NET. You can run traditional Windows based .NET apps on Windows Servers in Compute Engine or on Windows Containers in Google Kubernetes Engine (GKE). For modern Linux based containerized .NET apps, there’s more choice with App Engine (Flex), GKE and my favorite Cloud Run. Not to mention, the .NET Core 3.1 support in Cloud Functions is currently in preview for serverless .NET functions. 

In the rest of the blog post, I want to show you how to deploy .NET 5.0 to Cloud Run. Cloud Run makes it really easy to deploy and scale containerized apps on a fully managed platform.

.NET 5.0

First, make sure you have the latest .NET 5.0 installed:

> dotnet --version

5.0.100

Let’s follow the ASP.NET tutorial to create a web app with ASP.NET framework. Create a simple web app with plain HTTP support:

> dotnet new webapp -o webapp-cloudrun --no-https

Cloud Run expects for the app to listen on a PORT environment variable. Change CreateHostBuilder function of Program.cs file to do that:

Also, let’s change the default welcome message in Index.cshtml file to display a welcome message like this:

<h1 class="display-4">Welcome to .NET 5.0 on Google Cloud</h1>

After the changes, you can build and run the app locally. It should start a server listening on port 8080:

> dotnet run

info: Microsoft.Hosting.Lifetime[0]

      Now listening on: http://0.0.0.0:8080

Containerize .NET 5.0 

To containerize the app, we will create a Dockerfile. One thing to keep in mind is that with the release of .NET 5.0, all Docker tags for .NET Core 2.1/3.1 and .NET 5.0 is published to one set of unified Docker repositories (see 2375). The names of these repositories have been changed from the originals to no longer include "core" in the name. 

Here’s the Dockerfile for our app using the new dotnet/sdk and dotnet/aspnet base images with 5.0 versions:

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build

WORKDIR /app

COPY *.csproj ./

RUN dotnet restore

COPY . ./

WORKDIR /app

RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS runtime

WORKDIR /app

COPY --from=build /app/out ./

ENTRYPOINT ["dotnet", "webapp-cloudrun.dll"]

Build and save the Docker image to Google Container Registry with Cloud Build:

> gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-dotnet5

Deploy to Cloud Run

Finally, deploy to Cloud Run:

> gcloud run deploy --image gcr.io/${GOOGLE_CLOUD_PROJECT}/hello-dotnet5

        --platform managed

        --allow-unauthenticated

In a few seconds, you should see the service deployed:

✓ Deploying... Done.

✓ Creating Revision...

✓ Routing traffic...

✓ Setting IAM Policy...

Done.

Service [hello-dotnet5] revision [hello-dotnet5-00002-tux] has been deployed and is serving 100 percent of traffic.

Service URL: https://hello-dotnet5-dhmnie7yqa-ew.a.run.app

And visiting the service URL will display our page:

webapp_cloudrun

Of course .NET support is not limited to Cloud Run. Check out how we support .NET on Google Cloud.  

Related Article

Streamlining Cloud Run development with Cloud Code

Cloud Run is now integrated with Cloud Code, making it easier to create new Cloud Run services from your favorite IDE.

Read Article


Source: Google Cloud Blog