.NET 6 on Cloud Functions (2nd gen)

Back in August, we announced the 2nd generation of Cloud Functions with longer request processing times, larger instances, new event sources with Eventarc, and more.

A few weeks ago, .NET 6 support (public preview) was silently added to Cloud Functions.

Let's see how to deploy some .NET 6 functions to Cloud Functions 2nd gen.

Functions Framework for .NET

Functions Framework for .NET is the easiest way to create .NET functions for consuming HTTP or CloudEvent requests.

Install the template package into the .NET tooling:

dotnet new -i Google.Cloud.Functions.Templates

This installs three templates for .NET. You can verify that templates are installed by running:

dotnet new --list
Templates                                               Short Name
--------------------------------------------------------------------
Google Cloud Functions CloudEvent Function gcf-event
Google Cloud Functions CloudEvent Function (Untyped) gcf-untyped-event
Google Cloud Functions HttpFunction gcf-http

Note: Functions Framework for .NET currently supports .NET Core 3.1, but .NET 6.0 support is coming soon. You need to change the target framework of generated projects to .NET 6.0 later (see below).

HTTP Function

Create a function that handles HTTP requests using the gcf-http template:

mkdir HelloHttp 
cd HelloHttp
dotnet new gcf-http

This creates a project and a Function.cs file for responding to HTTP requests.

Change the target framework to net6.0 in the .csproj file:

<TargetFramework>net6.   0</TargetFramework>

Deploy to Cloud Functions using --trigger-http --gen2 flags and the dotnet6 runtime:

gcloud functions deploy hello-http-function \
--allow-unauthenticated \
--entry-point HelloHttp.Function \
--gen2 \
--region us-central1 \
--runtime dotnet6 \
--trigger-http

After a minute or two, you should see the Cloud Function deployed in Cloud Console:

You can call the function by sending an HTTP request with gcloud:

gcloud functions call hello-http-function \
--gen2 \
--region us-central1

CloudEvent Function

Alternatively, you can create a function that consumes CloudEvent requests using the gcf-event template:

mkdir HelloGcs
cd HelloGcs
dotnet new gcf-event

This creates a project and a Function.cs file for responding to CloudEvent requests. The function also parses the data of the CloudEvent into StorageObjectData.

Change the target framework to net6.0 in the .csproj file:

<TargetFramework>net6.0</TargetFramework>

Deploy to Cloud Functions using --trigger-event --gen2 flags and the dotnet6 runtime:

gcloud functions deploy hello-gcs-function \
--allow-unauthenticated \
--entry-point HelloGcs.Function \
--gen2 \
--region us-central1 \
--runtime dotnet6 \
--trigger-event google.storage.object.finalize \
--trigger-resource ${BUCKET_NAME}

In this case, you specify google.storage.object.finalize in --trigger-event and the bucket name in--trigger-resource to listen for new file creations in a bucket.

After a couple of minutes, the function should be visible in Cloud Console:

You can trigger the function by uploading a file to the storage bucket:

echo "Hello from Storage" > random.txt
gsutil cp random.txt gs://${BUCKET_NAME}

If you want to learn more about .NET 6 and Cloud Functions, you can find more samples in our .NET on Google Cloud Functions repo on GitHub and try out the Google Cloud Functions in C# codelab.

For questions or feedback, feel free to reach out to me on Twitter @meteatamel.

Originally published at https://atamel.dev.


.NET 6 on Cloud Functions (2nd gen) was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more