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.

Google Cloud Shell is a browser-based command line tool to access Google Cloud Platform resources. Cloud Shell makes it really easy to manage your Cloud Platform Console projects and resources without having to install the Google Cloud SDK and other tools on your system. With Cloud Shell, the Cloud SDK gcloud command and other utilities such as .NET Core command line interface tools are always available when you need them.

In this lab, you will learn about how to build and launch an ASP.NET Core app from Google Cloud Shell — without ever leaving the browser.

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

Self-paced environment setup

If you don't already have a Google account (Gmail or GSuite), you must create one. Then, sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud Platform projects. It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in Google Cloud Console in order to use Google Cloud Platform resources.

Start Cloud Shell

Google Cloud Shell is the browser-based command line tool to access Google Cloud Platform resources. Cloud Shell makes it really easy to manage your Cloud Platform Console projects and resources without having to install the Google Cloud SDK and other tools on your system. With Cloud Shell, the Cloud SDK gcloud command and other utilities such as .NET Core command line interface tools are always available when you need them.

To get started:

  1. Visit the Google Cloud Platform Console
  2. Click on the "Activate Google Cloud Shell" icon in 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 a VM.



Wait until the $ prompt appears.

ASP.NET Core is a significant redesign of the traditional Windows-only ASP.NET framework. You can develop and run your ASP.NET Core apps cross-platform on Windows, Mac and Linux.

ASP.NET Core comes with the .NET Core command-line interface (CLI), a new foundational cross-platform toolchain for developing .NET Core applications. This toolchain is already installed in Google Cloud Shell.

In the next section, we use the .NET Core command-line interface to create our first ASP.NET Core app.

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

dotnet

You should see the .NET Core version installed.

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.

mkdir HelloWorldAspNetCore

Navigate to that folder.

cd HelloWorldAspNetCore

Create a skeleton ASP.NET Core web app using the dotnet command.

dotnet new -t web

Since this is the first time you use dotnet, you will see some initialization messages, followed by a message about project creation.

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.

By default, ASP.NET Core web apps run on port 5000. Let's change that to a more conventional port for web apps, port 8080. To do that, we will use Cloud Shell's integrated online editor Eclipse Orion.

Open Eclipse Orion by clicking ‘Files' and then ‘Launch code editor' from the Cloud Shell menu.

Once the code editor is open, find Program.cs in the HelloWorldAspNetCore folder.

In the Main method, a host is built and run. 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.

dotnet restore

This will download all the NuGet dependencies for our app and you should see restore completed message at the end.

...
log  : Restore completed in 16298ms.

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

dotnet run

Application starts listening on port 8080.

...
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.

There! You've just created and launched an ASP.NET Core app from inside Cloud Shell, without once leaving your browser. But that's not all you can do. You can take your newly created ASP.NET Core app, containerize it using Docker and deploy it to Google App Engine. Or you can let Kubernetes manage it all for you in Google Container Engine.

What we've covered

Next Steps