.NET 言語
別の .NET 言語(C#、Visual Basic、F#)を使用して関数を記述できます。コアタスクはすべての言語で同じで、Functions Framework インターフェースのいずれかを実装するクラスを作成します。
IHttpFunction
- 汎用
ICloudEvent<T>
- 汎用でない
ICloudEvent
このドキュメントでは、F# と Visual Basic の例を紹介します。
テンプレート
このドキュメントの例では、テンプレートを使用します。
.NET SDK をインストールします。
テンプレート パッケージをインストールします。
dotnet new install Google.Cloud.Functions.Templates
C#(デフォルト)、F#、Visual Basic の 3 種類の関数用のテンプレートが用意されています。テンプレートから新しいプロジェクトを作成するときは、-lang f#
を指定して F# プロジェクトを作成するか、-lang vb
を指定して Visual Basic プロジェクトを作成します。たとえば、Visual Basic 用に新しい型なしの CloudEvent 関数を作成するには、次のコマンドを実行します。
dotnet new gcf-untyped-event -lang vb
HTTP 関数の例
HTTP 関数は、HTTP(S) リクエストを介して関数を呼び出す必要のある場合に使用します。次の例では、"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
HTTP に関するプロジェクト ファイルの例
上記のサンプルのプロジェクト ファイルは次のとおりです。
F#
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <Compile Include="Function.fs" /> </ItemGroup> <ItemGroup> <PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.2.1" /> </ItemGroup> </Project>
Visual Basic
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <RootNamespace>HelloWorldVb</RootNamespace> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.2.1" /> </ItemGroup> </Project>
HTTP 関数のデプロイ
F#
gcloud functions deploy fsharp-helloworld --no-gen2 --entry-point HelloWorldFSharp.Function --runtime dotnet6 --trigger-http --allow-unauthenticated
Visual Basic
gcloud functions deploy vb-helloworld --no-gen2 --entry-point HelloWorldVb.CloudFunction --runtime dotnet6 --trigger-http --allow-unauthenticated
CloudEvent の例
Pub/Sub トピックのメッセージ、Cloud Storage バケットの変更、Firebase イベントなどの非同期イベントによって Cloud Run functions の関数を間接的に呼び出す場合は、CloudEvent 関数を使用します。
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
CloudEvent 関数のデプロイ
F#
gcloud functions deploy fsharp-hello-pubsub --no-gen2 --entry-point HelloPubSubFSharp.Function --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated
Visual Basic
gcloud functions deploy vb-hello-pubsub --no-gen2 --entry-point HelloPubSubVb.CloudFunction --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated
CloudEvent の例をテストする
次の手順に沿って、CloudEvent の例をテストできます。
Pub/Sub トピックにメッセージを公開して、関数をトリガーします。
gcloud pubsub topics publish my-topic --message Flurry
ログを確認します。
gcloud functions logs read --limit 10
Pub/Sub トピックに公開した名前を含むメッセージが次のように表示されます。
D my-function ... Function execution started
I my-function ... Hello Flurry!
D my-function ... Function execution took 39 ms, finished with status: 'ok'