How to run Hello World with .NET

This tutorial describes how to use Visual Studio to deploy an ASP.NET Framework based Helloworld application from a Windows development environment to a Google Compute Engine instance running Windows Server 2016.

This tutorial assumes that you have Visual Studio 2015 or later installed and are familiar with the .NET Framework and the C# language.

You may also want to review our Quickstart for .NET in the App Engine Flexible Environment for the following reasons:

  • You would prefer to deploy an ASP.NET Core based Helloworld application.
  • You want to deploy an application to the Beta App Engine Flexible environment which provides automatic up and down scaling as well as load balancing.
  • You would prefer to deploy a Helloworld application from the command line without using Visual Studio.

This is part of the Getting Started with .NET on Google Cloud Platform series . After you complete the prerequisites listed under Before you begin, the tutorial takes about 15 minutes to complete.

Before you begin

Check off each step as you complete it.

  1. check_box_outline_blank check_box Create a project in the Google Cloud Platform Console.
    If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
    1. Open the Cloud Platform Console.
    2. In the drop-down menu at the top, select Create a project.
    3. Click Show advanced options.
    4. Give your project a name.
    5. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
  2. check_box_outline_blank check_box Enable billing for your project, and sign up for a free trial.

    If you haven't already enabled billing for your project, enable billing now, and sign up for a free trial. Enabling billing allows the application to consume billable resources such as running instances and storing data. During your free trial period, you won't be billed for any services.

Download and run the app

We've created a simple Hello World app using .NET so you can quickly get a feel for deploying an app to Google Cloud Platform. After you've completed the prerequisites, you can download and deploy the Hello World sample app. This section guides you through getting the code and running the application locally.

Get the Hello World app

Download the sample as a zip and extract it.

Alternatively, you can clone the git repository:

$ git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git

Run the app on your local computer

  1. Double click aspnet\1-hello-world\1-hello-world.sln so it opens in Visual Studio 2015.

  2. In Visual Studio, press F5 to build and run the application.

You can see the Hello, World! message from the sample app displayed in the page. This page is delivered by a web server running on your computer.

When you are ready to move forward, press Shift-F5 to stop the application.

Hello World code review

The code is a simple ASP.NET application.

        /// <summary>
        /// The simplest possible HTTP Handler that just returns "Hello World."
        /// </summary>
        public class HelloWorldHandler : HttpMessageHandler
        {
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                return Task.FromResult(new HttpResponseMessage()
                {
                    Content = new ByteArrayContent(Encoding.UTF8.GetBytes("Hello World."))
                });
            }
        };

        public static void Register(HttpConfiguration config)
        {
            var emptyDictionary = new HttpRouteValueDictionary();
            // Add our one HttpMessageHandler to the root path.
            config.Routes.MapHttpRoute("index", "", emptyDictionary, emptyDictionary,
                new HelloWorldHandler());
        }

Running Hello World on Google Cloud Platform

Create and configure a new Compute Engine instance

First, use the Google Cloud Launcher to create a new Compute Engine instance that has Windows Server 2016, Microsoft IIS, ASP.NET, and SQL Express preinstalled:

  1. In the Cloud Platform Console, go to the Cloud Launcher ASP.NET Framework page.

  2. Set your deployment name and preferred Compute Engine zone.

  3. Click Deploy to deploy the Compute Engine instance.

After the deployment process finishes, the next step is to install Google Cloud Tools for Visual Studio.

Install Cloud Tools for Visual Studio

To install Cloud Tools for Visual Studio, complete the following installation process:

  • Install the extension from the Extensions and Updates dialog in Visual Studio: Extensions and Updates dialog

    1. Select Tools > Extensions and Updates.

    2. Select the Online tab from the left pane.

    3. Search for Google Cloud extension for Visual Studio.

    4. Click Download and follow the prompts.

    5. Restart Visual Studio for the new extension to be loaded.

Configure the Google Cloud Extension for Visual Studio to use your account

  1. In Visual Studio, launch Cloud Explorer by going to Tools > Google Cloud Tools > Show Google Cloud Explorer.

  2. Add your Google account to access Google Cloud platform resources:

    1. Click Select or Create Account.

    Select Create Account

    1. Click Add.

    Manage Accounts

Add a default Windows user to your new Compute Engine instance:

  1. In Google Cloud Explorer, select the newly created project and expand Google Compute Engine.

  2. Right-click on the VM instance that you created above with Cloud Launcher.

  3. Select Manage Windows credentials.

    Manage Accounts

  4. Click Add credentials.

  5. Enter a username.

  6. Ensure that Create a password for me is selected and click Save.

  7. Click Reset when prompted with the confirmation question: "Reset the password for [your username] on the instance [instance you created]?"

  8. Then click the Close button to close the password window.

  9. Next click the Close button to close the Windows Credentials window.

Deploy the application to your Windows instance

  1. In the Visual Studio Solution Explorer pane, right click your application.

  2. In the context menu, click Publish to Google Cloud...

    Publish to Google Cloud

  3. In the Publish dialog box, choose Compute Engine.

    Choose to publish to Compute Engine

  4. Click Publish to build and deploy the sample application. After publishing completes, Visual Studio opens the application in your default web browser:

    Test application

Congratulations! You now have a working ASP.NET application running on a Compute Engine instance.

Cleaning up

If you're done with the tutorials and want to clean up resources that you've allocated, see Cleaning Up.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...