DEV Community

Matt Grofsky
Matt Grofsky

Posted on

Building Scaleable .NET Apps Without Windows

“I built a .NET Web App on macOS in Visual Studio and deployed a Linux Docker container to Google App Engine.”

That is a phrase you don’t hear too often, if at all. It wasn’t much more than a few years ago that .NET core was released. This open-source project allowed .NET developers to free themselves from the Windows platform, and allowed Microsoft to expand into what was once unfriendly territory. Fast forward to current technology trends, and you will find .NET developers expanding out to Linux and Mac Based machines. This once paradoxical development flow led me on a personal path to see how these technologies would fit into my Google Cloud, Mac-centric world. As it turns out, it feels quite natural.

Paradigms around which cloud provider is right for what purpose have persisted over the past few years. When engineers think about deploying a Windows web application in the cloud, they automatically think of Microsoft Azure. When Data Scientists want best-of-breed in Machine Learning and Analytics, they automatically think of Google Cloud. Amazon AWS is known for its global reach, maturity, and reputation. Over the past few years, these lines of distinction for building cloud-native applications have started to blur within not just cloud providers, but within the tools and operating systems used to create them. The following information contains a brief breakdown on how you or your team can start working with .NET outside of the Microsoft Windows ecosystem.

Deploying .NET in Google App Engine
For those that are not familiar with App Engine, it is a fully managed serverless application platform that takes advantage of Google’s years of building resilient and scalable architectures. It is an excellent solution if you want to start playing with .NET using Docker and don’t want to deal with Kubernetes.

Prerequisites:

  • Download Visual Studio for Mac

  • Sign up for a Google Cloud Platform account. You will get $300 in free usage.

  • Install the Google Cloud SDK.

  • Create a project in Google Cloud.

  • Open Terminal in Mac and run the following command to set your default project:

gcloud confid set project <PROJECT-NAME>
Enter fullscreen mode Exit fullscreen mode

The following link contains sample code for those interested in trying out a pre-built Visual Studio Solution. Included is one example each for deploying a Docker container to Google App Engine as well as Kubernetes.

Visual Studio comes with the needed Dockerfile templates to help any developer jump into building a container ready application. When created, the Dockerfile will expose the necessary ports so that the Web App is reachable. One essential item here is that when designing and deploying a custom runtime, the App Engine front end will route incoming requests to the appropriate module on port 8080, and you must be sure that your application code is listening on that port.

The default EXPOSE when adding in Docker support to a .NET web application is:

    FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
Enter fullscreen mode Exit fullscreen mode

You will want to change this to:

    FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
    WORKDIR /app
    EXPOSE 8080
    ENV ASPNETCORE_URLS=http://*:8080
Enter fullscreen mode Exit fullscreen mode

Everything else will remain the same. You may then go ahead and create your .NET web application as usual. The final step before deploying to App Engine is to specify the custom runtime. Create a file called app.yaml and place it in your root directory.

    runtime: custom
    env: flex
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10

    service: service-test
Enter fullscreen mode Exit fullscreen mode

The sample app.yaml above incurs costs to run on the App Engine flexible environment. The settings are to reduce costs during testing and are not appropriate for production use.

Remember that the flex environment does not scale down to 0 and could become costly if you supply the wrong resources and forget about it.

For more information, see: https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml

In Terminal on your Mac, browse to the root directory containing your app.yaml file and run the command:

gcloud app deploy
Enter fullscreen mode Exit fullscreen mode

You can preview your running application by browsing to App-Engine>Services in the Google Cloud Platform Console.

You just deployed a highly resilient and scalable .NET application without running a Windows Client machine or bringing up any Windows Servers.

Sample Visual Studio Solution

.NET Web App in Google App Engine Flexible w/ Docker Support


app.yaml

This is required to deploy to GAE Flexible. Runtime will be custom and env will be flex.

You can either add your service into the app.yaml or specify it in the gcloud app deploy command.

runtime: custom
env: flex

# This sample incurs costs to run on the App Engine flexible environment. 
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

service: matt-test

Remember that the flex environment does not scale down to 0 and could become costly if you supply the wrong resources and forget about it.

Dockerfile

One key item here is that when creating and deploying a custom runtime, the App Engine front end will route incoming requests to…




Translated for Ukrainian audiences:

Top comments (0)