Create and deploy an HTTP Cloud Run function with .NET (1st gen)
This guide takes you through the process of writing a Cloud Run function using the .NET runtime language C#. There are two types of Cloud Run functions:
- An HTTP-triggered function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the gcloud CLI.
- Update and install
gcloud
components:gcloud components update
- Prepare your development environment.
Create a function
This section describes how to manually create a function from scratch. Alternatively, you can use the templates provided by the template package.
To create a function:
Create a directory on your local system for the function code:
Windows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
Linux or Mac OS X
mkdir ~/helloworld cd ~/helloworld
Create a file called
Function.cs
with the contents below:This example function outputs the greeting "Hello World!"
Specify dependencies
The next step is to set up dependencies. There are two different types of dependencies you can set up in C# Cloud Run functions:
- Make the Functions Framework available. That is the task you are doing in this section.
- Make other libraries available, both in terms of project files and within the code via dependency injection. To learn more, see Customization through Functions Startup classes.
To make the Functions Framework available, change directory to the helloworld
directory you created above:
cd ~/helloworld
Then create a file called HelloWorld.csproj
with the contents below:
Deploy the function
To deploy the function with an HTTP trigger, run the following command in the
helloworld
directory:
gcloud functions deploy --no-gen2 my-first-function --entry-point HelloWorld.Function --runtime dotnet8 --trigger-http --allow-unauthenticated
where my-first-function
is the registered name by which your function
will be identified in the Google Cloud console, and --entry-point
specifies
your function's fully qualified class name (FQN).
Test the deployed function
When the function finishes deploying, take note of the
httpsTrigger.url
property or find it using the following command:gcloud functions describe my-first-function
It should look like this:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/my-first-function
Visit this URL in your browser. You should see a
Hello World!
message.
View logs
Logs for Cloud Run functions are viewable using the Google Cloud CLI, and in the Cloud Logging UI.
Use the command-line tool
To view logs for your function with the gcloud CLI, use the
logs read
command, followed by
the name of the function:
gcloud functions logs read my-first-function
The output should resemble the following:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.791 Function execution started D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.958 Function execution took 168 ms, finished with status code: 200 ...
Use the Logging dashboard
You can also view logs for Cloud Run functions from the Google Cloud console.
Use the template package
The sections above tell you how to manually create a function from scratch. Going forward, you will likely use the templates to create new functions.
To use the templates to create, build, and test a function:
Install the .NET SDK.
Install the template package:
dotnet new install Google.Cloud.Functions.Templates
Next, create a directory for your project, and use
dotnet new
to create a new HTTP function:mkdir HelloFunctions cd HelloFunctions dotnet new gcf-http
This creates
HelloFunctions.csproj
andFunction.cs
in the current directory. OpenFunction.cs
to review the code, and provide a custom message if you want.Build your function locally as follows:
dotnet run
Once the server is running, browse to
http://localhost:8080
to invoke the function. Press Ctrl-C in the console to stop the server.This function displays the message "Hello Functions Framework."
Alternatively, you can send requests to this function using curl
from another
terminal window:
curl localhost:8080
# Output: Hello Functions Framework