# .NET 6 on Cloud Functions (2nd gen)

Author: [Mete Atamel](https://atamel.dev/) | Published: 2022-10-03 | Topics: Cloud Functions, Serverless, Google Cloud Platform, How To | Canonical: https://atamel.dev/posts/2022/10-03_dotnet6_cloud_functions/


Back in August, we
[announced](https://cloud.google.com/blog/products/serverless/cloud-functions-2nd-generation-now-generally-available)
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](https://github.com/GoogleCloudPlatform/functions-framework-dotnet) is the
easiest way to create .NET functions for consuming HTTP or CloudEvent requests.

Install the template package into the .NET tooling:

```sh
dotnet new -i Google.Cloud.Functions.Templates
```

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

```sh
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:

```sh
mkdir HelloHttp
cd HelloHttp
dotnet new gcf-http
```

This creates a project and a
[Function.cs](https://github.com/meteatamel/cloud-functions-dotnet/blob/master/HelloHttp/Function.cs)
file for responding to HTTP requests.

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

```xml
<TargetFramework>net6.0</TargetFramework>
```

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

```sh
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:

![HTTP Cloud Function](https://codelabs.developers.google.com/static/codelabs/cloud-functions-csharp/img/6711b32e72277a5c.png)

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

```sh
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:

```sh
mkdir HelloGcs
cd HelloGcs
dotnet new gcf-event
```

This creates a project and a
[Function.cs](https://github.com/meteatamel/cloud-functions-dotnet/blob/master/HelloGcs/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:

```sh
<TargetFramework>net6.0</TargetFramework>
```

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

```sh
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:

![CloudEvent Cloud Function](https://codelabs.developers.google.com/static/codelabs/cloud-functions-csharp/img/c28654d74bb31420.png)

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

```sh
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](https://github.com/meteatamel/cloud-functions-dotnet) repo on GitHub
and try out the [Google Cloud Functions in
C#](https://codelabs.developers.google.com/codelabs/cloud-functions-csharp)
codelab.

For questions or feedback, feel free to reach out to me on Twitter
[@meteatamel](https://twitter.com/meteatamel).

