.NET Languages
It's possible to write your function in using different .NET languages (C#, Visual Basic, or F#). The core task is the same in all languages—you create a class implementing one of the Functions Framework interfaces:
IHttpFunction
- The generic
ICloudEvent<T>
- The non-generic
ICloudEvent
This document provides examples for F# and Visual Basic.
Templates
Note that to run the examples in this document, you will use the templates:
Install the .NET SDK.
Install the template package:
dotnet new install Google.Cloud.Functions.Templates
Templates are provided for the three kinds of functions in C# (the default), F#,
and Visual Basic. When creating a new project from a template, specify
-lang f#
to create an F# project, or -lang vb
to create a Visual Basic
project. For example, to create a new untyped CloudEvent function for Visual
Basic, you would run:
dotnet new gcf-untyped-event -lang vb
For F# and Visual Basic examples, see .NET Languages.
HTTP function examples
You use HTTP functions when you want to invoke
your function via an HTTP(S) request. The following examples output the message
"Hello World!"
F#
Visual Basic
Project files for HTTP examples
Here are the project files for the above samples:
F#
Visual Basic
Deploying the HTTP functions
F#
gcloud functions deploy fsharp-helloworld --entry-point HelloWorldFSharp.Function --runtime dotnet6 --trigger-http --allow-unauthenticated
Visual Basic
gcloud functions deploy vb-helloworld --entry-point HelloWorldVb.CloudFunction --runtime dotnet6 --trigger-http --allow-unauthenticated
CloudEvent examples
You use CloudEvent functions when you want to have your Cloud Run function invoked indirectly in response to an asynchronous event, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.
F#
Visual Basic
Deploying the CloudEvent functions
F#
gcloud functions deploy fsharp-hello-pubsub --entry-point HelloPubSubFSharp.Function --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated
Visual Basic
gcloud functions deploy vb-hello-pubsub --entry-point HelloPubSubVb.CloudFunction --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated
Test the CloudEvent examples
You can test the CloudEvent examples as follows:
Publish a message to your Pub/Sub topic to trigger your function:
gcloud pubsub topics publish my-topic --message Flurry
Look at the logs:
gcloud functions logs read --limit 10
You should see something like this, with a message that includes the name you published to the Pub/Sub topic:
D my-function ... Function execution started
I my-function ... Hello Flurry!
D my-function ... Function execution took 39 ms, finished with status: 'ok'