.NET Functions Framework
Introducing the .NET Functions Framework.
In this blogpost, we’ll give an introduction to the .NET Functions Framework and show you how to easily deploy a secure function on Google Cloud.
Setup
Install the .NET Core SDK 3.1.
After installing, you should have access to the CLI, dotnet
including handy .NET templates to quickly get started.
Quickstart
Install the Google Cloud Functions template package into the .NET tooling:
dotnet new -i Google.Cloud.Functions.Templates::1.0.0-alpha07
Then create a new HTTP function using the .NET template:
mkdir HelloFunctions
cd HelloFunctions
dotnet new gcf-http
This will create HelloFunctions.csproj
and Function.cs
in the current directory.
Test Locally
Run the function locally:
dotnet run
Go to 127.0.0.1:8080
to invoke the function.
You’re now invoking an HTTP function using the .NET Functions Framework!
Add a Dockerfile
To run this application in a container, we require a new file, Dockerfile
:
This Dockerfile states we want our container to use .NET Core, install dependencies and application code, build and run our dll.
Deploy to Cloud Run
Deploying your function Google Cloud isn’t too hard. In our terminal, we’ll instruct gcloud
to execute to commands:
- Use Cloud Build to build the container
- Use Cloud Run to run the container
We’ll store our current project in bash variable GCP_PROJECT
for simplicity.
Let’s build and deploy the secure container, ensuring we use the same project:
Send an authenticated HTTP request
We deployed a secure web service that rejects unauthorized requests. That’s great for services like internal APIs and Cloud Pub/Sub handlers.
If we try to cURL our service’s URL, we’ll get an Error: Forbidden
message.
So how can we call our service?
We can simply pass an authorization bearer token in an HTTP header to our Cloud Run service:
Curling the URL, you’ll see the response:
Hello, Functions Framework.
Thanks for Reading
Thanks for taking a quick dive into the .NET functions framework.
You might now be interested in signing up for the Early Access Program for Google Cloud Functions on .NET:
Or you might be interested in browsing the source code or discovering more samples (such as F# and VB) for the .NET Functions Framework: