Setting up a .NET development environment

This tutorial shows how to prepare your local machine for developing .NET and .NET core apps that run on Google Cloud and call Google Cloud APIs.

Install a development environment

The recommended development environment for running the .NET tutorials and sample apps in this documentation is Visual Studio or Visual Studio Code.

Create a Google Cloud project

Create a Google Cloud project to run your apps. Google Cloud projects form the basis for creating, enabling, and using all Google Cloud services.

Authentication

During local development, your .NET app must authenticate itself in order to interact with Google Cloud APIs. The following instructions describe how to create a service account key to authenticate your app. Apps can use this method to authenticate locally and when deployed to Google Cloud. Read more about authentication, including how authentication is handled once your app is deployed.

Create a service account key for authentication

Console

Create a service account:

  1. In the console, go to the Create service account page.

    Go to Create service account
  2. Select your project.
  3. In the Service account name field, enter a name. The console fills in the Service account ID field based on this name.

    In the Service account description field, enter a description. For example, Service account for quickstart.

  4. Click Create and continue.
  5. To provide access to your project, grant the following role(s) to your service account: Project > Owner.

    In the Select a role list, select a role.

    For additional roles, click Add another role and add each additional role.

  6. Click Continue.
  7. Click Done to finish creating the service account.

    Do not close your browser window. You will use it in the next step.

Create a service account key:

  1. In the console, click the email address for the service account that you created.
  2. Click Keys.
  3. Click Add key, and then click Create new key.
  4. Click Create. A JSON key file is downloaded to your computer.
  5. Click Close.

gcloud

Set up authentication:

  1. Create the service account:

    gcloud iam service-accounts create NAME

    Replace NAME with a name for the service account.

  2. Grant roles to the service account. Run the following command once for each of the following IAM roles: roles/owner:

    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE

    Replace the following:

    • SERVICE_ACCOUNT_NAME: the name of the service account
    • PROJECT_ID: the project ID where you created the service account
    • ROLE: the role to grant
  3. Generate the key file:

    gcloud iam service-accounts keys create FILE_NAME.json --iam-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

    Replace the following:

    • FILE_NAME: a name for the key file
    • SERVICE_ACCOUNT_NAME: the name of the service account
    • PROJECT_ID: the project ID where you created the service account

Next, set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON key that was downloaded. In PowerShell, the commands are:

For this PowerShell session.

PS > $env:GOOGLE_APPLICATION_CREDENTIALS = "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json"

For all processes created after this command.

PS > [Environment]::SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json", "User")

(Optional) Install .NET tools and SDKs

Even though you can create and manage Google Cloud resources via your web browser using the Google Cloud console, if you'd prefer to manage your Google Cloud resources from within Visual Studio or from the command-line, you can consider installing the following tools:

  • Google Cloud CLI: The gcloud CLI is a command-line interface for Google Cloud products and services.

  • Tools for Visual Studio: Tools for Visual Studio enables development in Visual Studio on Google Cloud.

  • Tools for PowerShell: Tools for PowerShell is a set of cmdlets for Windows PowerShell that lets you manage Google Cloud resources.

What's next