Create and deploy an HTTP Cloud Function with .NET

This guide takes you through the process of writing a Cloud Function using the .NET runtime language C#. There are two types of Cloud Functions:

  • An HTTP function, which you invoke from standard HTTP requests.
  • An event-driven function, which is triggered by events in your Cloud infrastructure, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.

For more detail, read writing HTTP functions and writing event-driven functions.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  8. Install and initialize the Google Cloud SDK.
  9. Update and install gcloud components with the following command.
    gcloud components update
  10. Prepare your development environment.

    Go to the .NET setup guide

Create your function

This section describes how to create a .NET function manually. Alternatively, you can use the Functions Framework templates) to create your function.

To create a function:

  1. Create a directory on your local system for the function code:

    Windows

    mkdir %HOMEPATH%\helloworld
    cd %HOMEPATH%\helloworld
    

    Linux or Mac OS X

    mkdir ~/helloworld
    cd ~/helloworld
    
  2. Create a file called Function.cs in the helloworld directory with the following contents:

    using Google.Cloud.Functions.Framework;
    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;
    
    namespace HelloWorld;
    
    public class Function : IHttpFunction
    {
        public async Task HandleAsync(HttpContext context)
        {
            await context.Response.WriteAsync("Hello World!");
        }
    }

Specify dependencies

Cloud Functions functions need a set of libraries called the Functions Framework. To make the Functions Framework available to your build, perform the following steps:

  1. Change directory to the helloworld directory you just created:

     cd ~/helloworld
    
  2. Create a file called HelloWorld.csproj in the helloworld directory with the following contents:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.1.0" />
      </ItemGroup>
    </Project>

To learn how to make other libraries available to your build, either via project files or within the code through dependency injection, see Customization through Functions Startup classes.

Build and test your function locally

  1. Build and run your function locally with the following command:

    dotnet run
    
  2. Test your function by visiting http://localhost:8080 in a browser or by running curl localhost:8080 from another window. If you're using Cloud Shell, click the Web Preview Button icon and pick Preview on port 8080 to see your function output.

    See Sending requests to local functions for more detail.

Deploy your function

To deploy your function, run the following command in the helloworld directory:

  gcloud functions deploy csharp-http-function \
    --gen2 \
    --entry-point=HelloWorld.Function \
    --runtime=dotnet6 \
    --region=REGION \
    --source=. \
    --trigger-http \
    --allow-unauthenticated

Replace REGION with the name of the Google Cloud region where you want to deploy your function (for example, us-west1).

csharp-http-function is the registered name by which your function will be identified in the Google Cloud console, and --entry-point specifies your function's fully qualified class name (FQN).

The optional --allow-unauthenticated flag lets you reach your function without authentication.

Test your deployed function

  1. After the function deploys, note the uri property from the output of the gcloud functions deploy command, or retrieve it with the following command:

    gcloud functions describe csharp-http-function \
      --region=REGION
    

    Replace REGION with the name of the Google Cloud region where you deployed your function (for example, us-west1).

  2. Test your function by visiting http://localhost:8080 in a browser or by running curl localhost:8080 from another window.

    See Sending requests to local functions for more detail.

View your function's logs

View the logs with the command-line tool

You can review your function's logs with the Cloud Logging UI or via the Google Cloud CLI.

To view logs for your function with the gcloud CLI, use the logs read command:

  gcloud functions logs read \
    --gen2 \
    --limit=10 \
    --region=REGION \
    csharp-http-function

Replace REGION with the name of the Google Cloud region where you deployed your function (for example, us-west1).

The output resembles the following:

LEVEL: I
NAME: my-first-function
TIME_UTC: 2023-06-01 12:47:42.221
LOG: Request finished HTTP/1.1 GET http://my-first-function-l5vqvupfzq-uw.a.run.app/favicon.ico - - - 404 0 - 0.9393ms

LEVEL: I
NAME: my-first-function
TIME_UTC: 2023-06-01 12:47:42.220
LOG: Request starting HTTP/1.1 GET http://my-first-function-l5vqvupfzq-uw.a.run.app/favicon.ico - -

View logs with the logging dashboard

To view the logs for your function with the logging dashboard, open the Cloud Functions Overview page and click the name of your function from the list, then click the Logs tab.

Create a function with the template package

These instructions have helped you create a simple function. In practice, you will likely prefer to use the Functions Framework templates to create your functions.

To use the Functions Framework templates to create, build, and test a function:

  1. Install the .NET SDK. You can skip this step if you're using Cloud Shell.

  2. Install the template package:

    dotnet new install Google.Cloud.Functions.Templates
    
  3. Create a directory for your project, and an empty HTTP function:

    mkdir HelloFunctions
    cd HelloFunctions
    dotnet new gcf-http
    

    This creates HelloFunctions.csproj and Function.cs in the current directory. Open Function.cs to review the code, and provide a custom message if you want.

  4. Optionally, follow the preceding instructions to build and test your function locally.

  5. Perform the following gcloud deploy command to deploy your function:

    gcloud functions deploy HelloFunctions.Function \
      --gen2 \
      --entry-point=HelloWorld.Function \
      --runtime=dotnet6 \
      --region=REGION \
      --source=. \
      --trigger-http \
      --allow-unauthenticated
    

    Replace REGION with the name of the Google Cloud region where you deployed your function (for example, us-west1).

For more information about the Functions Framework, see the Functions Framework documentation.