Google Cloud Functions in C#

Google Cloud Functions is an event-driven serverless compute platform. Cloud Functions allows you to write your code without worrying about provisioning resources or scaling to handle changing requirements.

There are two types of Cloud Functions:

  • HTTP functions respond to HTTP requests.
  • Background functions are triggered by events, like a message being published to Cloud Pub/Sub or a file being uploaded to Cloud Storage.

d59d66e678290f9e.png

This codelab will walk you through creating your own Cloud Functions in C#. More specifically, you will deploy C# functions responding to HTTP and CloudEvents from various Google Cloud sources.

What you'll learn

  • Functions Framework for .NET.
  • How to write an HTTP Function.
  • How to write a CloudEvent Function responding to Cloud Storage events.
  • How to write a CloudEvent Function responding to Cloud Pub/Sub events.
  • How to write a CloudEvent Function responding to any type of event.

Self-paced environment setup

  1. Sign in to Cloud Console and create a new project or reuse an existing one. (If you don't already have a Gmail or G Suite account, you must create one.)

H_hgylo4zxOllHaAbPKJ7VyqCKPDUnDhkr-BsBIFBsrB6TYSisg6LX-uqmMhh4sXUy_hoa2Qv87C2nFmkg-QAcCiZZp0qtpf6VPaNEEfP_iqt29KVLD-gklBWugQVeOWsFnJmNjHDw

dcCPqfBIwNO4R-0fNQLUC4aYXOOZhKhjUnakFLZJGeziw2ikOxGjGkCHDwN5x5kCbPFB8fiOzZnX-GfuzQ8Ox-UU15BwHirkVPR_0RJwl0oXrhqZmMIvZMa_uwHugBJIdx5-bZ6Z8Q

jgLzVCxk93d6E2bbonzATKA4jFZReoQ-fORxZZLEi5C3D-ubnv6nL-eP-iyh7qAsWyq_nyzzuEoPFD1wFOFZOe4FWhPBJjUDncnTxTImT3Ts9TM54f4nPpsAp52O0y3Cb19IceAEgQ

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

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

Running through this codelab shouldn't cost much, if anything at all. Be sure to to follow any instructions in the "Cleaning up" section which advises you how to shut down resources so you don't incur billing beyond this tutorial. New users of Google Cloud are eligible for the $300USD Free Trial program.

Start Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.

From the GCP Console click the Cloud Shell icon on the top right toolbar:

STgwiN06Y0s_gL7i9bTed8duc9tWOIaFw0z_4QOjc-jeOmuH2TBK8l4udei56CKPLoM_i1yEF6pn5Ga88eniJQoEh8cAiTH79gWUHJdKOw0oiBZfBpOdcEOl6p29i4mvPe_A6UMJBQ

It should only take a few moments to provision and connect to the environment. When it is finished, you should see something like this:

r6WRHJDzL-GdB5VDxMWa67_cQxRR_x_xCG5xdt9Nilfuwe9fTGAwM9XSZbNPWvDSFtrZ7DDecKqR5_pIq2IJJ9puAMkC3Kt4JbN9jfMX3gAwTNHNqFmqOJ-3iIX5HSePO4dNVZUkNA

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on Google Cloud, greatly enhancing network performance and authentication. All of your work in this lab can be done with simply a browser.

Functions Framework for .NET is an open source FaaS (Function as a Service) framework for writing portable .NET functions – brought to you by the Google Cloud Functions team.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

  • Google Cloud Functions
  • Your local development machine
  • Cloud Run and Cloud Run on GKE
  • Knative-based environments

In this codelab, you will use Functions Framework for .NET and its templates to create and deploy Cloud Functions in C#.

Inside Cloud Shell, run the following command to install Cloud Functions templates for dotnet:

dotnet new --install Google.Cloud.Functions.Templates::1.0.0-beta04

This installs 3 templates for dotnet. Each template is available in C#, F# and VB (but you'll only use C# in this lab). You can verify that templates are installed by running:

dotnet new gcf

Templates                                                 Short Name            
-----------------------------------------------------------------------
Google Cloud Functions CloudEvent Function                gcf-event
Google Cloud Functions CloudEvent Function (Untyped)      gcf-untyped-event
Google Cloud Functions HttpFunction                       gcf-http

You will create and deploy an HTTP Function responding to HTTP requests.

Create an HTTP Function using the gcf-http template:

mkdir HelloHttpFunction
cd HelloHttpFunction
dotnet new gcf-http

This creates a project and a Function.cs file responding to HTTP requests:

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

Deploy the function,using dotnet3 runtime and trigger-http flag:

gcloud functions deploy hello-http-function \
    --runtime dotnet3 \
    --trigger-http \
    --entry-point HelloHttpFunction.Function \
    --allow-unauthenticated

After a minute or two, you should see the Cloud Function deployed in Cloud Console:

8b9b29f636abf418.png

You can trigger the function by sending an HTTP request with gcloud functions call:

gcloud functions call hello-http-function

executionId: tp5h7d6t2vpl
result: Hello, Functions Framework.

You will create and deploy an CloudEvent Function responding to Cloud Storage events.

First, create a Cloud Storage bucket. This is the bucket you will listen events from later:

export BUCKET_NAME="cloud-functions-bucket-$(gcloud config get-value core/project)"
gsutil mb gs://${BUCKET_NAME}

Create an CloudEvent Function using the gcf-event template:

mkdir HelloCloudEventStorageFunction
cd HelloCloudEventStorageFunction
dotnet new gcf-event

This creates a project and a Function.cs file responding to CloudEvent requests. It also parses the data of CloudEvent into StorageObjectData:

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.SystemTextJson.Cloud.Storage.V1;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace HelloCloudEventStorageFunction
{
    public class Function : ICloudEventFunction<StorageObjectData>
    {
        public Task HandleAsync(CloudEvent cloudEvent, StorageObjectData data, CancellationToken cancellationToken)
        {
            ...

Deploy the function, using the dotnet3 runtime and trigger-event, trigger-resource flags:

gcloud functions deploy hello-cloudevent-storage-function \
    --runtime dotnet3 \
    --entry-point HelloCloudEventStorageFunction.Function \
    --trigger-event google.storage.object.finalize \
    --trigger-resource ${BUCKET_NAME} \
    --allow-unauthenticated

After a couple of minutes, the function should be visible in Cloud Console:

80b9843cfc912c7d.png

Trigger the function by uploading a file to the storage bucket:

echo "Hello from Storage" > random.txt
gsutil cp random.txt gs://${BUCKET_NAME}

Verify that the function was triggered by reading the logs:

gcloud functions logs read hello-cloudevent-storage-function

You will create and deploy an CloudEvent Function responding to Cloud Pub/Sub events.

First, create a Cloud Pub/Sub topic that will emit events:

export TOPIC_NAME=cloud-functions-topic
gcloud pubsub topics create ${TOPIC_NAME}

Create an CloudEvent Function using the gcf-event template:

mkdir HelloCloudEventPubSubFunction
cd HelloCloudEventPubSubFunction
dotnet new gcf-event

This creates a project and a Function.cs file responding to CloudEvent requests. It also parses the data of CloudEvent into StorageObjectData by default. Change StorageObjectData to MessagePublishedData to parse Pub/Sub messages instead and print it out.

In the end, your function should look like this:

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.SystemTextJson.Cloud.PubSub.V1;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace HelloCloudEventPubSubFunction
{
    public class Function : ICloudEventFunction<MessagePublishedData>
    {
        public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
        {
            Console.WriteLine("Message published data:");
            Console.WriteLine($"  Message: {data.Message}");
            Console.WriteLine($"  Subscription: {data.Subscription}");
            Console.WriteLine($"  Message.MessageId: {data.Message.MessageId}");
            Console.WriteLine($"  Message.TextData: {data.Message.TextData}");
            Console.WriteLine("Cloud event information:");
            Console.WriteLine($"  ID: {cloudEvent.Id}");
            Console.WriteLine($"  Source: {cloudEvent.Source}");
            Console.WriteLine($"  Type: {cloudEvent.Type}");
            Console.WriteLine($"  Subject: {cloudEvent.Subject}");
            Console.WriteLine($"  DataSchema: {cloudEvent.DataSchema}");
            Console.WriteLine($"  DataContentType: {cloudEvent.DataContentType}");
            Console.WriteLine($"  Time: {cloudEvent.Time?.ToUniversalTime():yyyy-MM-dd'T'HH:mm:ss.fff'Z'}");
            Console.WriteLine($"  SpecVersion: {cloudEvent.SpecVersion}");
            
            // In this example, we don't need to perform any asynchronous operations, so the
            // method doesn't need to be declared async.
            return Task.CompletedTask;
        }
    }
}

Deploy the function, using the dotnet3 runtime and trigger-topic flag:

gcloud functions deploy hello-cloudevent-pubsub-function \
    --runtime dotnet3 \
    --entry-point HelloCloudEventPubSubFunction.Function \
    --trigger-topic ${TOPIC_NAME} \
    --allow-unauthenticated

After a couple of minutes, the function should be visible in Cloud Console:

6e36fd4b312078d7.png

Trigger the function by publishing a message to the topic:

gcloud pubsub topics publish ${TOPIC_NAME} --message="Hello World"

Verify that the function was triggered by reading the logs:

gcloud functions logs read hello-cloudevent-pubsub-function

If you are experimenting with CloudEvents and don't yet have a payload data model you wish to commit to, or you want your function to be able to handle any Cloud Event, you can use an untyped CloudEvent function.

Create an CloudEvent Function using the gcf-untyped-event template:

mkdir HelloCloudEventUntypedFunction
cd HelloCloudEventUntypedFunction
dotnet new gcf-untyped-event

This creates a project and a Function.cs file responding to CloudEvent requests without any attempt to parse the data of the CloudEvent:

using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace HelloCloudEventUntypedFunction
{
    public class Function : ICloudEventFunction
    {
        public Task HandleAsync(CloudEvent cloudEvent, CancellationToken cancellationToken)

Deploy the function, using the dotnet3 runtime and trigger-event flag:

gcloud functions deploy hello-cloudevent-untyped-function \
    --runtime dotnet3 \
    --entry-point HelloCloudEventUntypedFunction.Function \
    --trigger-event google.storage.object.finalize \
    --trigger-resource ${BUCKET_NAME} \
    --allow-unauthenticated

The function will trigger when a file is uploaded to a storage bucket.

After a couple of minutes, the function should be visible in Cloud Console:

cae13b14d93318f7.png

Trigger the function by uploading a file to the storage bucket:

echo "Hello from Storage" > random.txt
gsutil cp random.txt gs://${BUCKET_NAME}

Verify that the function was triggered by reading the logs:

gcloud functions logs read hello-cloudevent-untyped-function

Congratulations for completing the codelab.

What we've covered

  • Functions Framework for .NET.
  • How to write an HTTP Cloud Function.
  • How to write a CloudEvent Function responding to Cloud Storage events.
  • How to write a CloudEvent Function responding to Cloud Pub/Sub events.
  • How to write a CloudEvent Function responding to any type of event.