Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
.NET-Sprachen
Sie können die Funktion in verschiedenen .NET-Sprachen (C#, Visual Basic oder F#) schreiben. Die Hauptaufgabe ist in allen Sprachen gleich: Sie erstellen eine Klasse, die eine der Functions Framework-Schnittstellen implementiert:
dotnet new install Google.Cloud.Functions.Templates
Für die drei Arten von Funktionen in C# (Standardeinstellung), F# und Visual Basic werden Vorlagen bereitgestellt. Geben Sie beim Erstellen eines neuen Projekts aus einer Vorlage -lang f# an, um ein F#-Projekt zu erstellen, oder -lang vb, um ein Visual Basic-Projekt zu erstellen. Wenn Sie beispielsweise eine neue CloudEvent-Funktion ohne Typ für Visual Basic erstellen möchten, führen Sie Folgendes aus:
dotnetnewgcf-untyped-event-langvb
Beispiele für HTTP-Funktionen
Wenn Sie Ihre Funktion über eine HTTP(S)-Anfrage aufrufen möchten, verwenden Sie HTTP-Funktionen. In den folgenden Beispielen wird die Nachricht "Hello World!" ausgegeben.
Sie verwenden CloudEvent-Funktionen, wenn die Cloud Run Functions-Funktion indirekt als Reaktion auf ein asynchrones Ereignis wie eine Nachricht in einem Pub/Sub-Thema, eine Änderung in einem Cloud Storage-Bucket oder ein Firebase-Ereignis aufgerufen werden soll.
Veröffentlichen Sie eine Nachricht in Ihrem Pub/Sub-Thema, um die Funktion auszulösen:
gcloudpubsubtopicspublishmy-topic--messageFlurry
Sehen Sie sich die Logs an:
gcloudfunctionslogsread--limit10
Sie sollten in etwa so etwas mit einer Nachricht mit dem Namen sehen, den Sie im Pub/Sub-Thema veröffentlicht haben:
D my-function ... Function execution started
I my-function ... Hello Flurry!
D my-function ... Function execution took 39 ms, finished with status: 'ok'
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-19 (UTC)."],[[["\u003cp\u003e.NET functions can be written in C#, Visual Basic, or F#, all requiring the implementation of either the \u003ccode\u003eIHttpFunction\u003c/code\u003e, generic \u003ccode\u003eICloudEvent<T>\u003c/code\u003e, or non-generic \u003ccode\u003eICloudEvent\u003c/code\u003e interface.\u003c/p\u003e\n"],["\u003cp\u003eTemplates for creating .NET function projects in C#, F#, and Visual Basic can be installed via the command \u003ccode\u003edotnet new install Google.Cloud.Functions.Templates\u003c/code\u003e, using \u003ccode\u003e-lang f#\u003c/code\u003e or \u003ccode\u003e-lang vb\u003c/code\u003e to generate the desired language.\u003c/p\u003e\n"],["\u003cp\u003eHTTP functions are invoked by HTTP(S) requests and shown through F# and Visual Basic examples that output "Hello World!".\u003c/p\u003e\n"],["\u003cp\u003eCloudEvent functions are triggered by asynchronous events, demonstrated with F# and Visual Basic examples responding to a message on a Pub/Sub topic.\u003c/p\u003e\n"],["\u003cp\u003eCloudEvent function testing involves publishing a message to a Pub/Sub topic and checking the logs to see if the function was triggered and what the output is.\u003c/p\u003e\n"]]],[],null,["# .NET Languages\n==============\n\nIt's possible to write your function in using different .NET languages (C#,\nVisual Basic, or F#). The core task is the same in all languages---you\ncreate a class implementing one of the Functions Framework interfaces:\n\n- [IHttpFunction](https://github.com/GoogleCloudPlatform/functions-framework-dotnet/blob/master/src/Google.Cloud.Functions.Framework/IHttpFunction.cs)\n- The generic [ICloudEvent\u003cT\u003e](https://github.com/GoogleCloudPlatform/functions-framework-dotnet/blob/master/src/Google.Cloud.Functions.Framework/ICloudEventFunction.cs)\n- The non-generic [ICloudEvent](https://github.com/GoogleCloudPlatform/functions-framework-dotnet/blob/master/src/Google.Cloud.Functions.Framework/ICloudEventFunction.cs)\n\nThis document provides examples for F# and Visual Basic.\n\nTemplates\n---------\n\nNote that to run the examples in this document, you will use the\n[templates](https://github.com/GoogleCloudPlatform/functions-framework-dotnet#functions-framework-for-net):\n\n1. Install the [.NET SDK](https://dotnet.microsoft.com/download).\n\n2. Install the template package:\n\n dotnet new install Google.Cloud.Functions.Templates\n\n| **Note:** versions of the .NET SDK earlier than .NET 7 use `dotnet new -i` instead of `dotnet new install`.\n\nTemplates are provided for the three kinds of functions in C# (the default), F#,\nand Visual Basic. When creating a new project from a template, specify\n`-lang f#` to create an F# project, or `-lang vb` to create a Visual Basic\nproject. For example, to create a new untyped CloudEvent function for Visual\nBasic, you would run: \n\n dotnet new gcf-untyped-event -lang vb\n\n| **Note:** `Function` is a keyword in Visual Basic, so for Visual Basic, the templates create a class with the name `CloudFunction`.\n\nHTTP function examples\n----------------------\n\nYou use [HTTP functions](/functions/1stgendocs/writing/write-http-functions) when you want to invoke\nyour function via an HTTP(S) request. The following examples output the message\n`\"Hello World!\"` \n\n### F#\n\n```f#\nnamespace HelloWorldFSharp\n\nopen Google.Cloud.Functions.Framework\nopen Microsoft.AspNetCore.Http\n\ntype Function() =\n interface IHttpFunction with\n member this.HandleAsync context =\n async {\n do! context.Response.WriteAsync \"Hello World!\" |\u003e Async.AwaitTask\n } |\u003e Async.StartAsTask :\u003e _\n```\n\n### Visual Basic\n\n```vb.net\nImports Google.Cloud.Functions.Framework\nImports Microsoft.AspNetCore.Http\n\nPublic Class CloudFunction\n Implements IHttpFunction\n\n Public Async Function HandleAsync(context As HttpContext) As Task Implements IHttpFunction.HandleAsync\n Await context.Response.WriteAsync(\"Hello World!\")\n End Function\nEnd Class\n```\n\n### Project files for HTTP examples\n\nHere are the project files for the above samples: \n\n### F#\n\n```f#\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n \u003cPropertyGroup\u003e\n \u003cOutputType\u003eExe\u003c/OutputType\u003e\n \u003cTargetFramework\u003enet6.0\u003c/TargetFramework\u003e\n \u003c/PropertyGroup\u003e\n\n \u003cItemGroup\u003e\n \u003cCompile Include=\"Function.fs\" /\u003e\n \u003c/ItemGroup\u003e\n\n \u003cItemGroup\u003e\n \u003cPackageReference Include=\"Google.Cloud.Functions.Hosting\" Version=\"2.2.1\" /\u003e\n \u003c/ItemGroup\u003e\n\u003c/Project\u003e\n```\n\n### Visual Basic\n\n```vb.net\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n \u003cPropertyGroup\u003e\n \u003cOutputType\u003eExe\u003c/OutputType\u003e\n \u003cRootNamespace\u003eHelloWorldVb\u003c/RootNamespace\u003e\n \u003cTargetFramework\u003enet6.0\u003c/TargetFramework\u003e\n \u003c/PropertyGroup\u003e\n\n \u003cItemGroup\u003e\n \u003cPackageReference Include=\"Google.Cloud.Functions.Hosting\" Version=\"2.2.1\" /\u003e\n \u003c/ItemGroup\u003e\n\u003c/Project\u003e\n```\n\n### Deploying the HTTP functions\n\n### F#\n\n gcloud functions deploy fsharp-helloworld --no-gen2 --entry-point HelloWorldFSharp.Function --runtime dotnet6 --trigger-http --allow-unauthenticated\n\n### Visual Basic\n\n gcloud functions deploy vb-helloworld --no-gen2 --entry-point HelloWorldVb.CloudFunction --runtime dotnet6 --trigger-http --allow-unauthenticated\n\nCloudEvent examples\n-------------------\n\nYou use [CloudEvent functions](https://github.com/GoogleCloudPlatform/functions-framework-dotnet#cloud-event-functions) when you want\nto have your Cloud Run function invoked indirectly in response to an asynchronous\nevent, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket,\nor a Firebase event. \n\n### F#\n\n```f#\nnamespace HelloPubSubFSharp\n\nopen Google.Cloud.Functions.Framework\nopen Google.Events.Protobuf.Cloud.PubSub.V1\nopen Microsoft.Extensions.Logging\nopen System\nopen System.Threading.Tasks\n\ntype Function(logger: ILogger\u003cFunction\u003e) =\n interface ICloudEventFunction\u003cMessagePublishedData\u003e with\n member this.HandleAsync(cloudEvent, data, cancellationToken) =\n let nameFromMessage = data.Message.TextData\n let name = if String.IsNullOrEmpty(nameFromMessage) then \"world\" else nameFromMessage\n logger.LogInformation(\"Hello {name}\", name)\n Task.CompletedTask\n```\n\n### Visual Basic\n\n```vb.net\nImports System.Threading\nImports CloudNative.CloudEvents\nImports Google.Cloud.Functions.Framework\nImports Google.Events.Protobuf.Cloud.PubSub.V1\nImports Microsoft.Extensions.Logging\n\nPublic Class CloudFunction\n Implements ICloudEventFunction(Of MessagePublishedData)\n\n Private _logger As ILogger\n\n Public Sub New(ByVal logger As ILogger(Of CloudFunction))\n _logger = logger\n End Sub\n\n\n Public Function HandleAsync(cloudEvent As CloudEvent, data As MessagePublishedData, cancellationToken As CancellationToken) As Task _\n Implements ICloudEventFunction(Of MessagePublishedData).HandleAsync\n\n Dim nameFromMessage = data.Message?.TextData\n Dim name = If(String.IsNullOrEmpty(nameFromMessage), \"world\", nameFromMessage)\n _logger.LogInformation(\"Hello {name}\", name)\n\n Return Task.CompletedTask\n End Function\nEnd Class\n```\n\n### Deploying the CloudEvent functions\n\n### F#\n\n gcloud functions deploy fsharp-hello-pubsub --no-gen2 --entry-point HelloPubSubFSharp.Function --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated\n\n### Visual Basic\n\n gcloud functions deploy vb-hello-pubsub --no-gen2 --entry-point HelloPubSubVb.CloudFunction --runtime dotnet6 --trigger-topic my-topic --allow-unauthenticated\n\n### Test the CloudEvent examples\n\nYou can test the CloudEvent examples as follows:\n\n1. Publish a message to your Pub/Sub topic to trigger your function:\n\n ```bash\n gcloud pubsub topics publish my-topic --message Flurry\n ```\n2. Look at the logs:\n\n ```bash\n gcloud functions logs read --limit 10\n ```\n\nYou should see something like this, with a message that includes the name you\npublished to the Pub/Sub topic: \n\n D my-function ... Function execution started\n I my-function ... Hello Flurry!\n D my-function ... Function execution took 39 ms, finished with status: 'ok'\n\n| **Note:** log messages may be delayed by a few minutes."]]