Most .NET developers think of Azure or AWS when considering cloud hosting. Azure, backed by Microsoft has excellent tooling and hosting options for .NET developers. AWS is an established cloud provider and the leader in the market. Many developers don’t realize that Google Cloud Platform or GCP for short, is also an excellent cloud platform for running .NET applications. Best of all, if you’re an ASP.NET developer, you don’t have to do anything different from what you’re use to. You can still use Visual Studio to build your app and choose GCP as your deployment target. Today, I’m going to explain how you would deploy an ASP.NET Core web application to Google App Engine Flex using Jetbrains Rider as your development Environment.
File New Solution
The first thing you do is open Jetbrains Rider and select New Solution->ASP.NET Core Web App (Model-View-Controller). You’re not limited to Jetbrains Rider. You can use VSCode to generate a new ASP.NET MVC Core app from the command line with the command: dotnet new mvc. You can also use Visual Studio for Mac or Visual Studio for Windows.
Complete the wizard and Jetbrains Rider will create a new ASP.NET Core Web Application.
Make a change to the index.cshtml page
<h1>Hello from Google App Engine Flex</h1>
Run the app and you’ll have a default ASP.NET Core app running in your browser.
Preparing for App Engine Flex
In order to run your ASP.NET Core app on Google App Engine Flex you have to choose the App Engine Flexible Environment. Let’s prepare our ASP.NET Core app for App Engine Flexible. Create an app.yaml file. Add the following configurations:
Next, modify your .csproj file. Add the following:
<None Include="app.yaml" CopyToOutputDirectory="PreserveNewest" />
The next step is to deploy to Google App Engine but first we’ll have to create a GCP account at https://cloud.google.com/ and create a project. Comment below if you would like to see a blog post on creating a project on Google Cloud Platform. The next steps will be performed at the command line:
- Make sure you have the gcloud command line tools installed. Comment below if you would like to see a blog post on installing the gcloud command line tools.
- Authenticate gcloud using this command: gcloud auth login. Follow the instructions and gcloud will be granted permissions on your behalf.
- Run the project one more time to ensure the project still works after the addition of the app.yaml and the modification to your .csproj file.
- Within your terminal window, ensure you’re in the root directory where your app.yaml file is located and run the following command to create a release: dotnet publish -c Release.
- The previous command will create a release package in your bin/Release/netcoreapp2.0/publish/ directory.
- In your terminal window navigate to bin/Release/netcoreapp2.0/publish/ and run the following commands to set gcloud to the default GCP project:
- gcloud config set project <project-name>
- gcloud app deploy
- gcloud app deploy will deploy your ASP.NET Core project to App Engine Flexible. You’ll need to choose the region where you want your app deployed. It’s best to choose a region closer to where you expect your users will use your app.
- gcloud will then create an app engine project and then ask you to confirm whether you want to continue. Choose ‘Y’.
If this is your first deployment, it will take a few minutes before your application is deployed to App Engine Flex.
When gcloud has successfully deployed your app, run the following command from your terminal: gcloud app browse. You’ll see your app running on App Engine Flex in your default browser with an app engine URL. For example https://your-project-id.appspot.com.
Running on App Engine Flex isn’t free so you should delete the project in order not to incur charges for your project. In a future blog post I’ll demonstrate how you can save money running your applications on Google App Engine Flexible. Please share this blog post with any .NET engineer wanting to get started with Google Cloud Platform. Comment below if you have questions.