Se stai creando una nuova funzione, consulta la guida rapida della console su Cloud Run. I contenuti di questa pagina si applicano solo alle funzioni legacy esistenti create con l'API Cloud Functions v1.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Linguaggi .NET
È possibile scrivere la funzione utilizzando diversi linguaggi .NET (C#, Visual Basic o F#). L'attività di base è la stessa in tutte le lingue: crei una classe che implementa una delle interfacce del framework Functions:
dotnet new install Google.Cloud.Functions.Templates
I modelli sono forniti per i tre tipi di funzioni in C# (valore predefinito), F# e Visual Basic. Quando crei un nuovo progetto da un modello, specifica -lang f# per creare un progetto F# o -lang vb per creare un progetto Visual Basic. Ad esempio, per creare una nuova funzione CloudEvent non tipizzata per Visual Basic, devi eseguire:
dotnetnewgcf-untyped-event-langvb
Esempi di funzioni HTTP
Utilizza le funzioni HTTP quando vuoi richiamare la funzione tramite una richiesta HTTP(S). Gli esempi seguenti stampano il messaggio
"Hello World!"
Utilizza le funzioni CloudEvent quando vuoi che la funzione Cloud Run venga richiamata indirettamente in risposta a un evento asincrono, ad esempio un messaggio in un argomento Pub/Sub, una modifica in un bucket Cloud Storage o un evento Firebase.
Pubblica un messaggio nell'argomento Pub/Sub per attivare la funzione:
gcloudpubsubtopicspublishmy-topic--messageFlurry
Esamina i log:
gcloudfunctionslogsread--limit10
Dovresti vedere qualcosa di simile, con un messaggio che include il nome che hai pubblicato nell'argomento 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'
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-03 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."]]