ASP.NET Core is a new open-source and cross-platform framework for building modern cloud-based and internet-connected applications using the C# programming language.
In this lab, you will deploy a simple ASP.NET Core app to App Engine flexible environment. This codelab builds on the Build and launch ASP.NET Core app from Google Cloud Shell codelab. You might want to do that lab first before attempting this lab.
Google App Engine applications are easy to create, maintain, and scale as your traffic and data storage needs change. With App Engine, there are no servers to manage. You simply upload your application and it's ready to go.
App Engine applications automatically scale based on incoming traffic. App Engine natively supports load balancing, microservices, authorization, SQL and NoSQL databases, Memcache, traffic splitting, logging, search, versioning, roll out and roll backs, and security scanning, all of which are highly customizable.
App Engine's environments, the standard environment and the flexible environment, support a host of programming languages, including C#, Java, Python, PHP, Node.js, Go, and more. The two environments give users maximum flexibility in how their application behaves, since each environment has certain strengths. For more information, read Choosing an App Engine Environment.
By using a kiosk at Google I/O, a test project has been created and can be accessed by using going to: https://console.cloud.google.com/.
These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.
Note that all these accounts will be disabled soon after the codelab is over.
Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.
When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :
While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.
From the GCP Console click the Cloud Shell icon on the top right toolbar:
Then click "Start Cloud Shell":
It should only take a few moments to provision and connect to the environment:
This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.
Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.
Run the following command in the cloud shell to confirm that you are authenticated:
gcloud auth list
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
gcloud config list project
[core] project = <PROJECT_ID>
If it is not, you can set it with this command:
gcloud config set project <PROJECT_ID>
Updated property [core/project].
In Cloud Shell prompt, you can verify that dotnet command line tool is already installed by checking its version. This should print the version of the installed dotnet command line tool:
Next, create a new skeleton ASP.NET Core web app.
dotnet new mvc -o HelloWorldAspNetCore
This should create a project and restore its dependencies. You should see a message similar to below.
Restore completed in 11.44 sec for HelloWorldAspNetCore.csproj. Restore succeeded.
We're almost ready to run our app. Navigate to the app folder.
Finally, run the app.
dotnet run --urls=http://localhost:8080
Application starts listening on port 8080.
Hosting environment: Production Content root path: /home/atameldev/HelloWorldAspNetCore Now listening on: http://[::]:8080 Application started. Press Ctrl+C to shut down.
To verify that the app is running, click on the web preview button on the top right and select ‘Preview on port 8080'.
You'll see the default ASP.NET Core webpage:
Once you verified that the app is running, press Ctrl+C to shut down the app.
Now, publish the app to get a self-contained DLL using the
dotnet publish command.
dotnet publish -c Release
publish displays some messages with a successfully published DLL at the end of the process.
... HelloWorldAspNetCore -> /home/atameldev/HelloWorldAspNetCore/bin/Release/netcoreapp2.1/HelloWorldAspNetCore.dll
Navigate to the the publish folder for the next step.
app.yaml file describes how to deploy the app to App Engine, in this case, the App Engine flexible environment. Create an
app.yaml file inside
Edit the generated
app.yaml file and specify the environment as flex and runtime as aspnetcore.
env: flex runtime: aspnetcore
Once you've saved the
app.yaml file to the
publish directory, you're ready to deploy your app to App Engine flexible using
gcloud. Just follow the prompts to create an App Engine application.
gcloud app deploy --version v0
During deployment, you will be asked to choose a region for your application. Chose a region where you want your app to run in.
Please choose a region for your application. After choosing a region, you cannot change it. Which region would you like to choose?  europe-west (supports standard and flexible)  us-central (supports standard and flexible)  us-east1 (supports standard and flexible)  asia-northeast1 (supports standard and flexible)  cancel
This will create an image for your application in the cloud, save that image to Google Container Registry and deploy to App Engine. During deployment, you can actually see the container image being built:
Operation completed over 1 objects/571.8 KiB. BUILD Step #0: Pulling image: gcr.io/gcp-runtimes/aspnetcorebuild@sha256:d7b7975acb374fc3a9655a4e529993e6270cfa78023885684626528bc379f8eb Step #0: sha256:d7b7975acb374fc3a9655a4e529993e6270cfa78023885684626528bc379f8eb: Pulling from gcp-runtimes/aspnetcorebuild
In the end, you should see that the app is deployed.
... Deployed service [default] to [https://<project-id>.appspot.com]
After you've deployed the application,visit it by opening the URL
http://<project-id>.appspot.com in your web browser.
You'll see the default ASP.NET Core webpage in a new tab.
You can also take a look at the container image created for you in the cloud. In cloud console, go to Container Registry > Images and then in appengine folder, you should see the image for your application.
At some point, the application that you've deployed to production will require bug fixes or additional features. App Engine is here to help you deploy a new version to production without impacting your users.
First, let's modify the application. Open the code editor from Cloud Shell.
Pages folder of
HelloWorldAspNetCore and update one of the default messages:
Learn how to build ASP.NET apps that can run anywhere.
And change it to this.
Learn how to build ASP.NET apps that can run on Google Cloud!
Save the changes and then go back to Cloud Shell. Inside
HelloWorldAspNetCore,publish the app to get a self-contained DLL.
dotnet publish -c Release
Navigate to the publish directory.
You can now deploy a new version of your app (
v1 in this case).
gcloud app deploy --version v1
Once it's deployed, you can go to App Engine versions section of Google Cloud Console to see the new version of your app serving all of the traffic with the new message.
In App Engine, under Dashboard section, you can see a number of dashboards for your application for latency, CPU etc. Explore them on your own.
Under Versions section, you can see the deployed versions of your app and you can split traffic between different versions in Traffic Splitting section. Let's split the traffic between two versions:
It's time to shutdown the app to save on cost and to be an overall good cloud citizen.
Go to the versions section of App Engine.
Select the version and stop it.
Once the version is stopped, the backing instances will be deleted and you should see instance count to drop down to zero.
There! You've created an ASP.NET Core app, packaged it as a Docker container, and deployed it to Google App Engine Flexible.
This work is licensed under a Creative Commons Attribution 2.0 Generic License.