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.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with Google Cloud Platform?

Novice Intermediate Proficient

Codelab-at-a-conference setup

The instructor will be sharing with you temporary accounts with existing projects that are already setup so you do not need to worry about enabling billing or any cost associated with running this codelab. Note that all these accounts will be disabled soon after the codelab is over.

Once you have received a temporary username and password to login from the instructor, log into Google Cloud Console: https://console.cloud.google.com/.

Here's what you should see once logged in :

Start Cloud Shell

From Google Cloud Platform Console, click on the "Activate Google Cloud Shell" icon in the top right hand corner of the header bar.


A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt. This might take a few seconds as Cloud Shell is spinning up a VM.



Wait until the $ prompt appears.

In Cloud Shell prompt, you can see that the dotnet command line tool is installed in Cloud Shell.

$ dotnet
Microsoft .NET Core Shared Framework Host
  Version  : 1.0.1
  Build    : cee57bf6c981237d80aa1631cfe83cb9ba329f12
...

Next, create a new project folder for our first ASP.NET Core app and navigate to that folder.

$ mkdir HelloWorldAspNetCore
$ cd HelloWorldAspNetCore

Create a skeleton ASP.NET Core web app using the dotnet command. Since this is the first time you use dotnet, you will see some initialization messages, followed by a message about project creation.

$ dotnet new -t web
Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs

..
Decompressing 100% 2568 ms
Expanding 100% 11018 ms
Created new C# project in /home/atameldev/HelloWorldAspNetCore.

This creates a number of files in your project folder. By default, ASP.NET Core apps use port 5000. Let's change that to port 8080.

Find Program.cs. Using your favorite editor (emacs, vim, nano etc.), change the main method, and add the UseUrls method to make the host bind to port 8080. The main method should look like this:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseUrls("http://*:8080")
                .Build();
    host.Run();
}

We're almost ready to run our app but we need to restore dependencies first. This will download all the NuGet dependencies for our app.

$ dotnet restore
...
log  : Restore completed in 16298ms.

Finally, run the app. You might see some warnings about dependencies that you can safely ignore.

$ dotnet run
...
Now listening on: http://*:8080
Application started. Press Ctrl+C to shut down.

To verify that the app is running, visit the web preview and select ‘Preview on port 8080'.

You'll see the default ASP.NET Core webpage in a new tab.

Now, publish the app to get a self-contained DLL using the dotnet publish command. Running publish displays a number of messages with a successfully published message at the end of the process.

$ dotnet publish -c Release
Publishing HelloWorldAspNetCore for .NETCoreApp,Version=v1.0
[16:47:53] Using gulpfile ~/HelloWorldAspNetCore/gulpfile.js
...
Published 1/1 projects successfully

Next, prepare your app to run on App Engine Flexible. The first step is to define the container and its contents. Don't worry, you won't need to install Docker -- App Engine flexible can build Docker images remotely as part of the deployment process.

Navigate to the publish directory and create a Dockerfile to define the Docker image.

$ cd bin/Release/netcoreapp1.0/publish/
$ touch Dockerfile

Add the following to Dockerfile using your favorite editor (vim, nano,or emacs).

FROM microsoft/dotnet:1.0.1-runtime
COPY . /app
WORKDIR /app

EXPOSE 8080/tcp
ENV ASPNETCORE_URLS http://*:8080

ENTRYPOINT ["dotnet", "HelloWorldAspNetCore.dll"]

Dockerfile builds on the official Microsoft image, which is already configured to run .NET Core apps and adds the app files and the tools necessary to run the app from the directory.

One important configuration included in our Dockerfile is the port on which the app listens for incoming traffic (8080), per App Engine flexible requirements. This is accomplished by setting the ASPNETCORE_URLS environment variable, which ASP.NET Core apps use to determine which port to listen to.

The app.yaml file describes how to deploy the app to App Engine, in this case, the App Engine flexible environment. You can use the gcloud command line tool of Google Cloud SDK to generate app.yaml. Run the following command in publish folder.

$ gcloud beta app gen-config --custom
Writing [app.yaml] to [/home/<project-id>/HelloWorldAspNetCore/bin/Release/netcoreapp1.0/publish].

Take a look at the generated app.yaml file. It specifies the environment as flexible and the runtime as custom.

env: flex
runtime: custom

Once you've saved the Dockerfile and app.yaml files 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 and to choose a region.

$ gcloud app deploy
...
Deployed service [default] to [https://<project-id>.appspot.com]

During deployment, you will be asked to choose a region for your application. Make sure you choose a region that support flexible environment.

Please choose a region for your application. After choosing a region, 
you cannot change it. Which region would you like to choose?
 [1] europe-west   (supports standard)
 [2] us-central    (supports standard and flexible)
 [3] us-east1      (supports standard and flexible)
 [4] asia-northeast1 (supports standard and flexible)
 [5] cancel

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.

Cleanup

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.

What we've covered

There! You've created an ASP.NET Core app, packaged it as a Docker container, and deployed it to Google App Engine Flexible.

Next Steps

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.