Create and deploy an HTTP Cloud Run function with .NET
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 function, which you invoke from standard HTTP requests.
- An event-driven function, which is triggered by events in your Cloud infrastructure, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.
For more detail, read writing HTTP functions and writing event-driven functions.
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, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging 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, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
- Install and initialize the Google Cloud SDK.
- Update and install
gcloud
components with the following command.gcloud components update
-
Prepare your development environment.
Create your function
This section describes how to create a .NET function manually. Alternatively, you can use the Functions Framework templates) to create your function.
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
in thehelloworld
directory with the following contents:
Specify dependencies
Cloud Run functions functions need a set of libraries called the Functions Framework. To make the Functions Framework available to your build, perform the following steps:
Change directory to the
helloworld
directory you just created:cd ~/helloworld
Create a file called
HelloWorld.csproj
in thehelloworld
directory with the following contents:
To learn how to make other libraries available to your build, either via project files or within the code through dependency injection, see Customization through Functions Startup classes.
Build and test your function locally
Build and run your function locally with the following command:
dotnet run
Test your function by visiting
http://localhost:8080
in a browser or by runningcurl localhost:8080
from another window. If you're using Cloud Shell, click the icon and pick Preview on port 8080 to see your function output.See Sending requests to local functions for more detail.
Deploy your function
To deploy your function, run the following command in the
helloworld
directory:
gcloud functions deploy csharp-http-function \
--gen2 \
--entry-point=HelloWorld.Function \
--runtime=dotnet8 \
--region=REGION \
--source=. \
--trigger-http \
--allow-unauthenticated
Replace REGION with the name of the Google Cloud region where you want to deploy your function
(for example, us-west1
).
csharp-http-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).
The optional --allow-unauthenticated
flag lets you reach your function
without authentication.
Test your deployed function
After the function deploys, note the
uri
property from the output of thegcloud functions deploy
command, or retrieve it with the following command:gcloud functions describe csharp-http-function \ --region=REGION
Replace REGION with the name of the Google Cloud region where you deployed your function (for example,
us-west1
).Test your function by visiting
http://localhost:8080
in a browser or by runningcurl localhost:8080
from another window.See Sending requests to local functions for more detail.
View your function's logs
View the logs with the command-line tool
You can review your function's logs with the Cloud Logging UI or via the Google Cloud CLI.
To view logs for your function with the gcloud CLI, use the
logs read
command:
gcloud functions logs read \
--gen2 \
--limit=10 \
--region=REGION \
csharp-http-function
Replace REGION with the name of the Google Cloud region where you deployed your function
(for example, us-west1
).
The output resembles the following:
LEVEL: I
NAME: my-first-function
TIME_UTC: 2023-06-01 12:47:42.221
LOG: Request finished HTTP/1.1 GET http://my-first-function-l5vqvupfzq-uw.a.run.app/favicon.ico - - - 404 0 - 0.9393ms
LEVEL: I
NAME: my-first-function
TIME_UTC: 2023-06-01 12:47:42.220
LOG: Request starting HTTP/1.1 GET http://my-first-function-l5vqvupfzq-uw.a.run.app/favicon.ico - -
View logs with the logging dashboard
To view the logs for your function with the logging dashboard, open the Cloud Run functions Overview page and click the name of your function from the list, then click the Logs tab.
Create a function with the template package
These instructions have helped you create a simple function. In practice, you will likely prefer to use the Functions Framework templates to create your functions.
To use the Functions Framework templates to create, build, and test a function:
Install the .NET SDK. You can skip this step if you're using Cloud Shell.
Install the template package:
dotnet new install Google.Cloud.Functions.Templates
Create a directory for your project, and an empty 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.Optionally, follow the preceding instructions to build and test your function locally.
Perform the following
gcloud deploy
command to deploy your function:gcloud functions deploy HelloFunctions.Function \ --gen2 \ --entry-point=HelloWorld.Function \ --runtime=dotnet8 \ --region=REGION \ --source=. \ --trigger-http \ --allow-unauthenticated
Replace REGION with the name of the Google Cloud region where you deployed your function (for example,
us-west1
).
For more information about the Functions Framework, see the Functions Framework documentation.