Application Development

Introducing .NET on Google Cloud Functions

#appdev

Cloud Functions is Google Cloud’s Function-as-a-Service platform that allows users to create single-purpose, stand-alone functions that respond to events, without having to manage a server or runtime environment. Cloud functions are a great fit for serverless applications, mobile or IoT backends, real-time data processing systems, video, image and sentiment analysis and even things like chatbots, or virtual assistants.

cloud functions.png

Today we’re bringing .NET Core 3.1, a free, cross-platform and open-source platform for Windows, Mac and Linux, to Cloud Functions. With this integration you can write cloud functions using your favorite .NET Core 3.1 runtime with our Functions Framework for .NET, for an idiomatic developer experience!

With Cloud Functions for .NET, now in Preview, you can use .NET Core 3.1 to build business-critical applications and integration layers, and deploy the function in a fully managed environment, complete with access to resources in a private VPC network. .NET functions scale automatically based on your load. You can write HTTP functions to respond to HTTP events, and CloudEvent functions to process events sourced from various cloud and Google Cloud services including Pub/Sub, Cloud Storage and Firestore.

You can develop functions using the Functions Framework for .NET, an open source functions-as-a-service framework for writing portable .NET functions. With Functions Framework you develop and run your functions locally, then deploy them to Cloud Functions, or to another .NET environment.

The Functions Framework for .NET supports HTTP functions and CloudEvent functions. A HTTP cloud function is very easy to write. Below, you’ll find a simple HTTP function for Webhook/HTTP use cases.

  using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace HelloWorld
{
    public class Function : IHttpFunction
    {
        public async Task HandleAsync(HttpContext context)
        {
            await context.Response.WriteAsync("Hello World!");
        }
    }
}

CloudEvent functions on the .NET runtime respond to industry standard CNCF CloudEvents. These events can be from various Google Cloud services, such as Pub/Sub, Cloud Storage and Firestore.

Here is a simple CloudEvent function working with Pub/Sub.

  using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

namespace HelloPubSub
{
    public class Function : ICloudEventFunction<MessagePublishedData>
    {
        private readonly ILogger _logger;

        public Function(ILogger<Function> logger) =>
            _logger = logger;

        public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
        {
            string nameFromMessage = data.Message?.TextData;
            string name = string.IsNullOrEmpty(nameFromMessage) ? "world" : nameFromMessage;
            _logger.LogInformation("Hello {name}", name);
            return Task.CompletedTask;
        }
    }
}

VB and F# Support

The Cloud Functions .NET runtime also supports VB and F#. The programming model is exactly the same, and there are examples in the GitHub repository. The .NET Functions Framework comes with a template package for use from the command line or Visual Studio, and these templates support VB and F# as well.

Try Cloud Functions for .NET today

Cloud Functions for .NET is ready for you to try today. Read the Quickstart guide, learn how to write your first functions, and try it out with a Google Cloud Platform free trial. If you want to dive a little bit deeper into the technical aspects, you can also read Jon Skeet’s blog on a tour of the .NET Functions Framework. If you’re interested in the open-source Functions Framework for .NET, please don’t hesitate to have a look at the project and potentially even contribute to it. In addition, we also have a codelab that you could try out to help you get familiarized with the runtime. We’re looking forward to seeing all the .NET functions you write!