.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:

This document provides examples for F# and Visual Basic.

Templates

Note that to run the examples in this document, you will use the templates:

  1. Install .NET Core SDK 3.1.

  2. Install the template package:

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

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#

namespace HelloWorldFSharp

open Google.Cloud.Functions.Framework
open Microsoft.AspNetCore.Http

type Function() =
    interface IHttpFunction with
        member this.HandleAsync context =
            async {
                do! context.Response.WriteAsync "Hello World!" |> Async.AwaitTask
            } |> Async.StartAsTask :> _

Visual Basic

Imports Google.Cloud.Functions.Framework
Imports Microsoft.AspNetCore.Http

Public Class CloudFunction
    Implements IHttpFunction

    Public Async Function HandleAsync(context As HttpContext) As Task Implements IHttpFunction.HandleAsync
        Await context.Response.WriteAsync("Hello World!")
    End Function
End Class

Project files for HTTP examples

Here are the project files for the above samples:

F#

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Function.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" />
  </ItemGroup>
</Project>

Visual Basic

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>HelloWorldVb</RootNamespace>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" />
  </ItemGroup>
</Project>

Deploying the HTTP functions

F#

gcloud functions deploy fsharp-helloworld --entry-point HelloWorldFSharp.Function --runtime dotnet3 --trigger-http --allow-unauthenticated

Visual Basic

gcloud functions deploy vb-helloworld --entry-point HelloWorldVb.CloudFunction --runtime dotnet3 --trigger-http --allow-unauthenticated

CloudEvent examples

You use CloudEvent functions when you want to have your Cloud 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#

namespace HelloPubSubFSharp

open Google.Cloud.Functions.Framework
open Google.Events.Protobuf.Cloud.PubSub.V1
open Microsoft.Extensions.Logging
open System
open System.Threading.Tasks

type Function(logger: ILogger<Function>) =
    interface ICloudEventFunction<MessagePublishedData> with
        member this.HandleAsync(cloudEvent, data, cancellationToken) =
            let nameFromMessage = data.Message.TextData
            let name = if String.IsNullOrEmpty(nameFromMessage) then "world" else nameFromMessage
            logger.LogInformation("Hello {name}", name)
            Task.CompletedTask

Visual Basic

Imports System.Threading
Imports CloudNative.CloudEvents
Imports Google.Cloud.Functions.Framework
Imports Google.Events.Protobuf.Cloud.PubSub.V1
Imports Microsoft.Extensions.Logging

Public Class CloudFunction
    Implements ICloudEventFunction(Of MessagePublishedData)

    Private _logger As ILogger

    Public Sub New(ByVal logger As ILogger(Of CloudFunction))
        _logger = logger
    End Sub


    Public Function HandleAsync(cloudEvent As CloudEvent, data As MessagePublishedData, cancellationToken As CancellationToken) As Task _
        Implements ICloudEventFunction(Of MessagePublishedData).HandleAsync

        Dim nameFromMessage = data.Message?.TextData
        Dim name = If(String.IsNullOrEmpty(nameFromMessage), "world", nameFromMessage)
        _logger.LogInformation("Hello {name}", name)

        Return Task.CompletedTask
    End Function
End Class

Deploying the CloudEvent functions

F#

gcloud functions deploy fsharp-hello-pubsub --entry-point HelloPubSubFSharp.Function --runtime dotnet3 --trigger-topic my-topic --allow-unauthenticated

Visual Basic

gcloud functions deploy vb-hello-pubsub --entry-point HelloPubSubVb.CloudFunction --runtime dotnet3 --trigger-topic my-topic --allow-unauthenticated

Test the CloudEvent examples

You can test the CloudEvent examples as follows:

  1. Publish a message to your Pub/Sub Topic to trigger your function:

    gcloud pubsub topics publish my-topic --message Flurry
  2. 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'