Entertainment at it's peak. The news is by your side.

A Tour of the .NET Functions Framework


Blow their non-public horns: the total code on this blog put up is available in my DemoCode GitHub repo, below Functions.

For many of 2020, one in all the initiatives I’ve been working on is the .NET Functions Framework. This is the .NET implementation of the Functions Framework Contract… nonetheless extra importantly to most readers, it’s “the formula to lag .NET code on Google Cloud Functions” (aka GCF). The categorical boundary between the Functions Framework and GCF is an spell binding topic, nonetheless I acquired’t be going into it on this blog put up, because I’m ceaselessly extra angry to bellow you the code.

The GitHub repository for the .NET Functions Framework already has a documentation plight to boot to a quickstart in the README, and there would possibly be .NET directions within the Google Cloud Functions documentation clearly… nonetheless this put up is extra of a tour from my non-public perspective. It’s “the stuff I’m angry to bellow you” better than anything. (It additionally highlights a couple of of the impact challenges, which you wouldn’t if truth be told query documentation to elevate out.) It’s liable to safe the root of any convention or consumer community talks I give on the Functions Framework, too. Oh, and in the event you hadn’t already realized – here’s a pleasant lengthy put up, so be warned!

Introduction to Functions as a Service (Faas)

This allotment is intentionally immediate, because I query many readers will already be the employ of FaaS both with .NET on a competing cloud platform, or doubtless with GCF and a definite language. There are limitless articles about FaaS which elevate out a higher job than I’d. I’ll appropriate impact two parts though.

At the muse, the lightbulb 2d for me spherical functions as a producing rate proposition came in a convention discuss (I’m in a position to’t take into accout whose, I’m timid) where the speaker emphasized that FaaS isn’t about what it is seemingly you’ll maybe possibly also elevate out with functions. There’s nothing (or even I have to bellow “very diminutive” to hedge my bets a diminutive) it is seemingly you’ll maybe possibly also elevate out with FaaS that you couldn’t elevate out by standing up a service in a Kubernetes cluster or the same. As a substitute, the essential motivating component is label. The extra you are away from the industry aspect of things, the much less that’s liable to impact to your pondering, nonetheless I elevate out mediate it makes an incredible distinction. I’ve seen this individually, which has helped my knowing: I even bear my bear Kubernetes cluster in Google Kubernetes Engine (GKE) which runs,, and a couple of alternative sites. The cluster has three nodes, and I pay a beautiful modest amount for it every month… nonetheless it’s working out of sources. I would possibly maybe possibly lower the redundancy a diminutive and impact any other tweaks, nonetheless essentially, adding a brand recent test net procedure for a particular experiment has change into complicated. Deploying a fair, nonetheless, is liable to be free (in consequence of the free tier) and can at worst be incremental.

Secondly, there’s a realistic component I hadn’t thought to be, which is that deploying a fair with the .NET Functions Framework is now my dash-to formula of standing up a easy server, even supposing it has nothing to elevate out with standard functions employ cases. Examples embody:

  • Working some (pretty immediate-working) query benchmarks for Datastore to analyze a buyer issue
  • Starting up a server locally as a easy formula of doing the OAuth2 dance when I modified into as soon as knowing how to put up to WordPress
  • Creating a if truth be told easy “new affairs aggregator” to plight a couple of websites that I chanced on myself going to time and yet again

Ok, I’m hugely biased having written the framework, and therefore shining it neatly – in addition to, I’m taken aback by the fluctuate of cases where having a easy formula to deploy easy code is ceaselessly mighty.

Anyway, ample with the background… let’s take a look at how easy it if truth be told is to safe started.

Getting started: allotment 1, putting in the templates

At the muse, you wish the .NET Core SDK version 3.1 or elevated. I believe that acquired’t rule out most of the readers of this blog 🙂

Essentially the most straightforward formula of getting started is to employ the templates NuGet equipment, so it is seemingly you’ll maybe possibly also then impact Functions initiatives the employ of dotnet recent. From a present line, install the templates equipment like this:

dotnet recent -i Google.Cloud.Functions.Templates::1.0.0-beta02

(The ::1.0.0-beta02 allotment is appropriate since it’s aloof in prerelease. When we’ve hit 1.0.0, you acquired’t want to specify the version.)

That installs three templates:

  • gcf-http (an HTTP-prompted fair)
  • gcf-tournament (a strongly-typed CloudEvent-prompted fair, the employ of PubSub events in the template)
  • gcf-untyped-tournament (an “untyped” CloudEvent-prompted fair, where you’d bear to deserialize the CloudEvent records payload your self)

The complete templates come in for C#, VB and F#, nonetheless I’ll only focal level on C# on this blog put up.

Within the new (October 2020) preview of Visual Studio 2019 (which I believe will dash GA in November with .NET 5) there’s an design to employ .NET Core templates in the “File -> Original Mission” skills, and the templates work with that. Or now not it is a long way a must to enable it in “Alternatives -> Environment -> Preview Facets -> Point to all .NET Core templates in the Original mission dialog”. The textual negate for the Functions templates needs a diminutive of an overhaul, nonetheless it’s tremendous to be capable to elevate out all the pieces from Visual Studio after putting in the templates. I’ll bellow the present strains for now though.

Getting started: allotment 2, hi there world

I take a look at no level in making an strive to be modern here: let’s start with a fair that appropriate prints Hello World or the same. As luck would bear it, that’s what the gcf-http template provides us, so we acquired’t if truth be told favor to write down any code in any admire.

But again, from a present line, lag these instructions:

mkdir HelloWorld
cd HelloWorld
dotnet recent gcf-http

It is top to take a look at a confirmation message:

The template “Google Cloud Functions HttpFunction” modified into as soon as created efficiently.

This would maybe bear created two info. First, HelloWorld.csproj:



And Feature.cs:

the employ of Google.Cloud.Functions.Framework;
the employ of Microsoft.AspNetCore.Http;
the employ of Gadget.Threading.Projects;

namespace HelloWorld
    public class Feature : IHttpFunction
        /// Good judgment to your fair goes here.
        /// The HTTP context, containing the assign a query to and the response.
        /// A role representing the asynchronous operation.
        public async Job HandleAsync(HttpContext context)
            watch for context.Response.WriteAsync("Hello, Functions Framework.");

Upright – you’re now willing to lag the fair. As soon as extra, from the present line:

dotnet lag

… the server have to start, with log messages which is liable to be very familiar to somebody with ASP.NET Core skills along with an introductory log message that’s particular to the Functions Framework.

[Google.Cloud.Functions.Hosting.EntryPoint] [info] Serving fair HelloWorld.Feature

Point a browser at http://localhost: 8080 and you’ll want to examine the message of “Hello, Functions Framework.” Astronomical!

You should be questioning precisely what’s occurring at this level, and I promise I’ll attain again to that. But first, let’s deploy this as a Google Cloud Feature.

Getting started: allotment 3, Google Cloud Functions (GCF)

There are a couple of necessities. You should possibly possibly like:

  • A Google Cloud Platform (GCP) mission, with billing enabled (though as I talked about earlier, experimentation with Functions is liable to all attain within the free tier)
  • The Cloud Functions and Cloud Originate APIs enabled
  • The Google Cloud SDK (gcloud)

Moderately than give the directions here, I counsel you dash to the Java GCF quickstart medical doctors and apply along there. I’ll update this put up when the .NET quickstart is available.

As soon as the total necessities come in, the explicit deployment is easy. From the present line:

gcloud functions deploy hi there-world --runtime=dotnet3 --entry-level=HelloWorld.Feature --trigger-http --allow-unauthenticated

That’s all on one line so that it’s easy to reduce and paste even into the Windows present line, nonetheless it breaks down like this:

  • gcloud functions deploy – the present we’re working (deploy a fair)
  • hi there-world – the name of the fair we’re growing, which is willing to appear in the Functions console
  • --runtime=dotnet3 – we would like to employ the .NET runtime within GCF
  • --entry-level=HelloWorld.Feature – this specifies the fully kindly name of the goal fair form.
  • --trigger-http – the fair is prompted by HTTP requests (in region of events)
  • --allow-unauthenticated – the fair would possibly maybe possibly additionally be prompted with out authentication

That present uploads your source code securely, builds it, then deploys it. (After I acknowledged that having the .NET Core SDK is a prerequisite, that’s appropriate for the template and working locally… nonetheless you don’t want the SDK installed to deploy to GCF.)

The fair will take a couple of minutes to deploy – possibly longer for the very first time, if some sources want to be created in the background – and at closing you’ll take a look at the total well-known parts of the fair written to the console. This is a diminutive of a wall of textual negate, nonetheless you wish to explore the httpsTrigger allotment and its url rate. Consult with that URL, and hi there presto, you’re working a fair.

Whereas you’re following along nonetheless didn’t bear any of the necessities installed, which would possibly bear taken pretty a while – nonetheless in the event you’re already a GCP consumer, it’s if truth be told pleasing snappy.

Personal hide: I’d prefer it if we didn’t want to specify the entry level on the present line, for initiatives with only one fair. I’ve made that work when appropriate working dotnet lag, as we saw earlier, nonetheless for the time being you elevate out bear to specify the entry level. I even bear some possibly silly suggestions for making this extra wise – I’ll want to envision a quiz to the personnel how seemingly they are.

What’s occurring? Why don’t I’d like a Vital design?

Ok, time for some explanations… no lower than of the .NET aspect of things.

Let’s start with the programs involved. The Functions Framework ships four programs:

  • Google.Cloud.Functions.Framework
  • Google.Cloud.Functions.Web net hosting
  • Google.Cloud.Functions.Attempting out
  • Google.Cloud.Functions.Templates

We’ve already seen what the Templates equipment provides, and we’ll admire at Attempting out in a while.

The separation between the Web net hosting equipment and the Framework equipment is maybe a diminutive arbitrary, and I query it to be beside the level to most users. The Framework equipment contains the interfaces that functions want to enforce, and adapters between them. Whereas you desired to host a fair your self within one other net utility, as an illustration, it is seemingly you’ll maybe possibly depend appropriate on the Framework equipment, and your fair can bear precisely the identical code as it does in any other case.

The Web net hosting equipment is what configures and begins the server in the extra frail scenario, and here’s the equipment that the “standard” functions deployment scenario will depend on. (If at the mission file from earlier, you’ll take a look at that it depends on the Web net hosting equipment.)

Whereas the Web net hosting equipment has change genuine into a diminutive extra complex over the route of the alpha and beta releases, it’s essentially very diminutive pondering what it does – and that’s all since it builds on the root of ASP.NET Core. I will be capable to not stress this ample – with out the magnificent work of the ASP.NET Core personnel, we wouldn’t be on this express now. (Perchance we’d bear built something from scratch, I don’t know. I’m now not saying there wouldn’t be a product, appropriate that I if truth be told admire having this basis to impact on.)

None of that explains how we’re in a express to appropriate employ dotnet lag with out having a Program.cs or anything with a Vital design though. Sure, C# 9 has admire parts spherical top-stage applications, nonetheless that’s now not being veteran here. (I elevate out want to admire if there’s something we are in a position to elevate available, nonetheless that’s a definite matter.)

This is where Mission Dragonfruit is available in – inspirationally, no lower than. This is a moderately diminutive-known mission as allotment of the Gadget.CommandLine effort; Scott Hanselman’s blog put up on it items the scene pleasing neatly.

The cool thing about Mission Dragonfruit is that you write a Vital design that has the parameters you will need with the sorts that you will need. You should possibly aloof employ dotnet lag, and the total parsing happens magically earlier than it will get to your code. The magic is ceaselessly in the MSBuild targets that stretch as allotment of the NuGet equipment. They generate a diminutive of C# code that first calls the parser after which calls your Vital design, and enviornment that generated code because the entry level.

My JonSkeet.DemoUtil NuGet equipment (which I if truth be told ought to doc a while) does the identical thing, allowing me to impact a mission with as many Vital suggestions as I’d like, after which safe provided with a menu of them when I lag it. Pleasurable for demos in talks. (But again, here’s copying the idea from Mission Dragonfruit.)

And that’s ceaselessly what the Web net hosting equipment in the Functions Framework does. The Web net hosting equipment exposes an EntryPoint class with a StartAsync design, and there are MSBuild targets that automatically generate the entry level for you (if the drinking mission is an executable, and unless you disable it).

Yow will stumble on the generated entry level code in the relevant obj directory (e.g. obj/Debug/netcoreapp3.1) after constructing. The code appears to be like to be precisely like this, no matter your fair:

// This file modified into as soon as created automatically
the employ of Gadget.Runtime.CompilerServices;
the employ of Gadget.Threading.Projects;
interior class AutoGeneratedProgram
    public static Job Vital(string[] args) =>
        Google.Cloud.Functions.Web net hosting.EntryPoint.StartAsync(
             typeof(world::AutoGeneratedProgram).Assembly, args);

In general it calls EntryPoint.StartAsync and passes in “the assembly containing the fair” (and any present line arguments). Every thing else is performed by EntryPoint.

We’ll take a look at extra of the parts of the Web net hosting equipment in a while, nonetheless no lower than this has answered the quiz of how dotnet lag works with our HelloWorld fair.

Attempting out HelloWorld

Ok, so we’ve purchased HelloWorld to lag locally, and we’ve deployed it efficiently… nonetheless are we convinced it works? Effectively yes, I’m pleasing sure it does, in addition to, it is a long way also tremendous to examine that.

I’m a astronomical fan of “testing” programs – extra NuGet programs to impact it more straightforward to employ code that works with that core equipment. So as an illustration, with NodaTime there’s a NodaTime.Attempting out equipment, which we’ll if truth be told employ in a while this blog put up. I don’t know where I purchased the name “testing” from – it can most likely had been an interior Google convention that I good to employ from NodaTime – nonetheless the idea is ceaselessly helpful.

As I talked about earlier, there’s a Google.Cloud.Functions.Attempting out equipment, and now I’ve explained the naming convention it is seemingly you’ll maybe possibly also doubtless guess that it’s going to safe involved.

The Attempting out equipment provides:

  • An in-memory ILogger and ILoggerProvider so it is seemingly you’ll maybe possibly also with out issues unit test functions that employ logging, including testing the logs which is liable to be written. (IMO this have to if truth be told be something available in ASP.NET Core out of the field.)
  • A easy formula of growing a test server (the employ of Microsoft.AspNetCore.TestHost), which automatically installs the in-memory logger.
  • A vulgar class for tests that automatically creates a test server for a fair, and exposes general operations similar to “impact a GET assign a query to and retrieve the textual negate returned”.

Arguably it’s a diminutive unconventional to bear a vulgar class for tests like this. It’s fully that it is seemingly you’ll maybe possibly also mediate of to employ composition in region of inheritance. But my skills writing the samples for the Functions Framework led me to abominate the boilerplate code that came with composition. I don’t thoughts the bit of a code scent of the employ of a vulgar class, when it outcomes in easy tests.

I acquired’t battle by the full parts intimately, nonetheless let’s admire at the test for HelloWorld. There’s if truth be told now not considerable to examine, provided that there’s no conditional logic – we appropriate want to bellow that after we impact a assign a query to to the server, it writes out “Hello, Functions Framework.” in the response.

Honest appropriate-attempting for fluctuate, I’ve made up our minds to employ NUnit in the sample code for this blog put up. Most of my tests for work code employ xUnit on this day and age, nonetheless nothing in the Attempting out equipment depends on staunch testing programs, so it is a long way going to work with any test framework you will need.

Take a look at lifecycle hide: diversified test frameworks employ diversified lifecycle devices. In xUnit, a brand recent test class instance is created for every test case, so we safe a “smooth” server at any time when. In NUnit, a single test fixture instance is created and veteran for all tests, meaning there’s a single server, too. The server is anticipated to be mostly stateless, nonetheless in the event you’re testing in opposition to log entries in NUnit, you in all likelihood want a setup design. There’s an example later.

So we are in a position to enviornment up the mission merely:

mkdir HelloWorld.Tests
cd HelloWorld.Tests
dotnet recent nunit -f netcoreapp3.1
dotnet add equipment Google.Cloud.Functions.Attempting out --prerelease
dotnet add reference ../HelloWorld/HelloWorld.csproj

(I’d normally elevate out all of this within Visual Studio, nonetheless the present line shows you all the pieces you wish by formula of mission setup. Blow their non-public horns that I’ve specified netcoreapp3.1 because the goal framework fair because I’ve purchased the preview of .NET 5 installed, which outcomes in a default goal of net5… and that’s incompatible with the fair mission.)

With the mission in region, we are in a position to add the test itself:

the employ of Google.Cloud.Functions.Attempting out;
the employ of NUnit.Framework;
the employ of Gadget.Threading.Projects;

namespace HelloWorld.Tests
    public class FunctionTest : FunctionTestBase
        public async Job RequestWritesMessage()
            string textual negate = watch for ExecuteHttpGetRequestAsync();
            Enlighten.AreEqual("Hello, Functions Framework.", textual negate);

The simplicity of testing is one in the entirety I’m most overjoyed with in the Functions Framework. In this particular case I’m cushy to employ the default URI (“sample-uri”) and a GET assign a query to, nonetheless there are other suggestions in FunctionTestBase to impact extra complex requests, or to impact CloudEvent functions.

So is that this a unit test or an integration test? For my half I’m now not too afflicted by the terminology, nonetheless I’d name this an integration test in that it does take a look at the integration by the Functions stack. (It doesn’t test integration with anything else since the fair doesn’t integrate with anything.) But it runs if truth be told mercurial, and here’s my “default” safe of test for functions now.

Past hi there world: what’s the time?

Let’s switch from a trivial fair to a reducing-edge, ultra-complex, safe-willing-for-thoughts-melting fair… we’re going to bellow the new time. Better than that, we’re going to optionally bellow the time in a particular time zone. (You knew I’d elevate time zones into this someway, appropriate?)

Traditional readers would possibly be unsurprised that I’m going to employ NodaTime for this. This immediate fair will cease up demonstrating a lot of parts:

  • Dependency injection by a “Feature Startup class”
  • Logger injection
  • Logger behaviour locally vs in GCF
  • Attempting out a fair that uses dependency injection
  • Attempting out log output

Let’s start with the code itself. We’ll admire at it in three aspects.

First, the fair class:

public class Feature : IHttpFunction
    non-public readonly IClock clock;
    non-public readonly ILogger logger;

    // Receive and take into accout the dependencies.
    public Feature(IClock clock, ILogger logger) =>
        (this.clock, this.logger) = (clock, logger);

    public async Job HandleAsync(HttpContext context)
        // Implementation code we will admire at later

Diverse than the attribute, this needs to be very familiar code to ASP.NET Core builders – our two dependencies (a clock and a logger) are provided in the constructor, and remembered as fields. We are in a position to then employ them in the HandleAsync design.

For any readers now not conscious of NodaTime, IClock is an interface with a single design: Rapid GetCurrentInstant(). Any time it is seemingly you’ll maybe possibly possibly name DateTime.UtcNow in DateTime-oriented code, you wish to employ a clock in NodaTime. That formula, your time-composed code is testable. There’s a singleton implementation which merely delegates to the device clock, so that’s what we would like to configure by formula of the dependency for our fair, when working in manufacturing as in opposition to in tests.

Dependency injection with Functions startup classes

Dependency injection is configured in the .NET Functions Framework the employ of Functions startup classes. These are a diminutive bit bit like the idea of the identical name in Azure Functions, nonetheless they’re a diminutive bit extra flexible (in my scrutinize, anyway).

Functions startup classes bear to catch from Google.Cloud.Functions.Web net hosting.FunctionsStartup (which is a typical class; the attribute is named FunctionsStartupAttribute, nonetheless C# enables you to practice the attribute appropriate the employ of FunctionsStartup and it provides the suffix).

FunctionsStartup is an abstract class, nonetheless it doesn’t bear any abstract members. As a substitute, it has four virtual suggestions, every with a no-op implementation:

  • void ConfigureAppConfiguration(WebHostBuilderContext context, IConfigurationBuilder configuration)
  • void ConfigureServices(WebHostBuilderContext context, IServiceCollection products and providers)
  • void ConfigureLogging(WebHostBuilderContext context, ILoggingBuilder logging)
  • void Configure(WebHostBuilderContext context, IApplicationBuilder app)

These will doubtless be familiar to ASP.NET Core builders – they’re the identical configuration suggestions that exist on IWebHostBuilder.

A Functions startup class overrides one or extra of these suggestions to configure the kindly component of the server. Blow their non-public horns that the final design (Configure) is veteran to add middleware to the assign a query to pipeline, nonetheless the Functions Framework expects that the fair itself would possibly maybe possibly be the closing stage of the pipeline.

Essentially the most in kind technique to override (in my skills so a long way, anyway) is ConfigureServices, in bellow to configure dependency injection. That’s what we would like to elevate out in our example, and here’s the class:

public class Startup : FunctionsStartup
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection products and providers) =>
        products and providers.AddSingleton(SystemClock.Instance);

This is the form referred to by the attribute on the fair class:


In incompatibility to “standard” ASP.NET Core startup classes (which is liable to be expected to configure all the pieces), Functions startup classes would possibly maybe possibly additionally be composed. Every startup that has been specified both on the fair form, or its based fully mostly kinds, or the assembly, is veteran. In bellow for you the startups to be utilized in a particular bellow, it is seemingly you’ll maybe possibly also specify that in the attribute.

Handiest the fair form that is that if truth be told being served is queried for attributes. You’ve got two functions in the identical mission, and every of them bear diversified startup class attributes… along with assembly attributes specifying any startup classes that both functions want.

Blow their non-public horns: when working from the present line, it is seemingly you’ll maybe possibly also specify the fair to support as a present line argument or an surroundings variable. The framework will fail to start (with a great error) in the event you strive to lag a mission with a couple of functions, nonetheless with out specifying which one you wish to support.

The composition component enables third events to integrate with the .NET Functions Framework cleanly. As an illustration, Steeltoe would possibly maybe provide a Steeltoe.GoogleCloudFunctions equipment containing a bunch of startup classes, and it is seemingly you’ll maybe possibly appropriate specify (in attributes) which ones you desired to employ for any given fair.

Our Startup class only configures the IClock dependency. It doesn’t want to configure ILogger, because ASP.NET Core does this automatically.

Ultimately, we are in a position to write down the explicit fair body. This is moderately easy. (Yes, it’s virtually 30 strains lengthy, nonetheless it’s aloof easy.)

public async Job HandleAsync(HttpContext context)
    // Acquire the new rapid in time by the clock.
    Rapid now = clock.GetCurrentInstant();

    // Always write out UTC.
    watch for WriteTimeInZone(DateTimeZone.Utc);

    // Write out the new time in as many zones because the consumer has specified.
    foreach (var zoneId in context.Keep a query to.Demand["zone"])
        var zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(zoneId);
        if (zone is null)
            logger.LogWarning("Particular person provided invalid time zone '{identity}'", zoneId);
            watch for WriteTimeInZone(zone);

    Job WriteTimeInZone(DateTimeZone zone)
        string time = LocalDateTimePattern.GeneralIso.Layout(now.InZone(zone).LocalDateTime);
        return context.Response.WriteAsync($"Unique time in {zone.Identity}: {time}n");

I haven’t afflicted to alert the consumer to the invalid time zone they’ve provided, though the code to elevate out so would possibly maybe possibly be easy. I bear logged a warning – mostly so I’m in a position to bellow logging.

Using DateTimeZoneProviders.Tzdb is a a diminutive lazy selection here, by the formula. I would possibly maybe possibly inject an IDateTimeZoneProvider as neatly, taking into legend tests with customized time zones. That’s doubtless overkill on this case though.

Logging locally and in manufacturing

So, let’s take a look at what happens after we lag this.

The warning appears to be like to be as if this:

2020-10-21T09: 53: 45.334Z [ZoneClock.Function] [warn] Particular person provided invalid time zone 'The united states/Metropolis'

This is all on one line: the console logger veteran by default by the .NET Functions Framework when working locally is a diminutive bit extra compact than the default console logger.

But what happens after we lag in Google Cloud Functions? Let’s strive


gcloud functions deploy zone-clock --runtime=dotnet3 --entry-level=ZoneClock.Feature --allow-unauthenticated --trigger-http

Whereas you dash to the Cloud Functions Console and desire the zone-clock fair, it is seemingly you’ll maybe possibly also scrutinize the logs. Here are two requests:

(Click on on every image for the paunchy-sized screenshot.)

Warning logs in Functions console

Blow their non-public horns how the default “info” logs are differentiated from the “warning” log in regards to the zone ID now not being chanced on.

Within the Cloud Logging Console (which is additionally linked from the Functions console, with a link that will filter the logs to appropriate the ones for the fair you’re taking a admire at) it is seemingly you’ll maybe possibly also develop the log entry for extra well-known parts:

Warning logs in Logging console

The .NET Functions Framework detects when it’s working in a Knative surroundings, and writes structured JSON to the console in region of easy textual negate. This is then picked up and processed by the logging infrastructure.

Attempting out with dependencies

So, it appears to be like to be as if our fair does what we would like it to, nonetheless it is a long way also appropriate to bear tests to hide it. If we appropriate employ a FunctionTestBase like earlier than, with out anything, we’d aloof safe the manufacturing dependency being injected though, which would impact it laborious to write down sturdy tests.

As a substitute, we would like to specify diversified Functions startup classes for our tests. We want to employ a definite IClock implementation – a FakeClock from the NodaTime.Attempting out equipment. That lets us impact an IClock with any time we would like. Let’s enviornment it to June third 2015, 20: 25: 30 UTC:

class FakeClockStartup : FunctionsStartup
    public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection products and providers) =>
        products and providers.AddSingleton(recent FakeClock(Rapid.FromUtc(2015, 6, 3, 20, 25, 30)));

So how will we repeat the test to employ that startup? We would possibly maybe possibly manually create a FunctionTestServer and enviornment the startups that formula… nonetheless it’s considerable extra convenient to employ the identical FunctionsStartupAttribute as earlier than, nonetheless this time utilized to the test class:

public class FunctionTest : FunctionTestBase
    // Tests here

(In my sample code, FakeClockStartup is a nested class at some level of the test class, whereas the manufacturing Startup class is a top-stage class. There’s no particular trigger of this, though it feels moderately natural to me. You should possibly enviornment up your startup classes nonetheless you love.)

Whereas it’s good to bear any startup classes which needs to be veteran by the total tests to your test mission, it is seemingly you’ll maybe possibly also practice FunctionsStartupAttribute to the test assembly.

The tests themselves take a look at two things:

  • The output that’s written to the HTTP response
  • The log entries written by the fair (nonetheless now not by other loggers)

But again, FunctionTestBase makes the latter easy, with a GetFunctionLogEntries() design. (You should possibly safe at all the logs in the event you in point of fact want to, clearly.)

I’ve if truth be told purchased three tests, nonetheless one will suffice to bellow the sample:

public async Job InvalidCustomZoneIsIgnoredButLogged()
    string actualText = watch for ExecuteHttpGetRequestAsync("?zone=The united states/Metropolis&zone=Europe/London");
    // We aloof print UTC and Europe/London, nonetheless The united states/Metropolis is now not if truth be told talked about in any admire.
    string[] expectedLines =
        "Unique time in UTC: 2015-06-03T20: 25: 30",
        "Unique time in Europe/London: 2015-06-03T21: 25: 30"
    var actualLines = actualText.Destroy up('n', StringSplitOptions.RemoveEmptyEntries);
    Enlighten.AreEqual(expectedLines, actualLines);

    var logEntries = GetFunctionLogEntries();
    Enlighten.AreEqual(1, logEntries.Count);
    var logEntry = logEntries[0];
    Enlighten.AreEqual(LogLevel.Warning, logEntry.Level);
    StringAssert.Contains("The united states/Metropolis", logEntry.Message);

As a aspect-hide, I normally clutch NUnit over xUnit, nonetheless I if truth be told desired to

be in a express to write down:

// Could maybe well maybe be legitimate in xUnit...
var logEntry = Enlighten.Single(GetFunctionLogEntries());

As illustrious earlier, we additionally want to make certain the logs are cleared earlier than the initiating of every test, which we are in a position to elevate out with a setup design:

public void ClearLogs() => Server.ClearLogs();

(The Server property in FunctionTestBase is the test server that it


Ok, so that’s HTTP functions… what else will we elevate out?

CloudEvent functions

Functions and events dash together very naturally. Google Cloud Functions would possibly maybe possibly additionally be prompted by diversified events, and in the .NET Functions Framework these are represented as CloudEvent functions.

CloudEvents is a CNCF mission to standardize the format in which events are propagated and delivered. It isn’t opinionated in regards to the payload records, or how the events are kept and plenty others, nonetheless it provides a general “envelope” model, and particular requirements of how events are represented in transports similar to HTTP.

This means that it is seemingly you’ll maybe possibly also write no lower than some code to tackle “any tournament”, and the general constructing needs to be familiar even in the event you switch between (bellow) Microsoft-generated and Google-generated events. As an illustration, if both Google Cloud Storage and Azure Blob Storage can emit events (e.g. when an object/blob is created or deleted) then it needs to be easy ample to employ that tournament from Azure or Google Cloud Platform respectively. I wouldn’t query it to be the identical code for both sorts of tournament, nonetheless no lower than the deserialization allotment of “I even bear an HTTP assign a query to; give me the tournament info” would possibly maybe possibly be the identical. In C#, that’s handled by the C# CloudEvents SDK.

Whereas you’re cushy deserializing the records allotment your self, that’s all you wish, and it is seemingly you’ll maybe possibly also write an untyped CloudEvent fair like this:

public class Feature : ICloudEventFunction
    public Job HandleAsync(CloudEvent cloudEvent, CancellationToken cancellationToken)
        // Feature body

Blow their non-public horns how there’s no assign a query to and response: there’s appropriate the tournament.

That’s all very neatly, nonetheless what in the event you don’t want to deserialize the records your self? I don’t want users to bear to write down their bear illustration of (bellow) our Cloud Pub/Sub message tournament records. I’d like to impact it as easy as that it is seemingly you’ll maybe possibly also mediate of to employ Pub/Sub messages in functions.

That’s where two other repositories attain in:

The latter repository provides two programs for the time being: Google.Events and Google.Events.Protobuf. You should possibly add a dependency to your functions mission to Google.Events.Protobuf, after which write a typed CloudEvent fair like this:

public class Feature : ICloudEventFunction
    public Job HandleAsync(CloudEvent cloudEvent, MessagePublishedData records, CancellationToken cancellationToken)
        // Feature body

Your fair is aloof provided with the customary CloudEvent so it will entry metadata, nonetheless the records itself is deserialized automatically.

Serialization library alternate alternatives

There’s an spell binding impact issue here. The schemas for the tournament records are in the muse in protobuf format, and we’re additionally changing them to JSON schema. It would possibly maybe possibly impact sense to be capable to deserialize with any of:

  • Google.Protobuf
  • Gadget.Textual negate.Json
  • Newtonsoft.Json

Whereas you’re already the employ of one of these dependencies in diversified areas to your code, you in all likelihood don’t want to add one other of them. So the new thought is to present three diversified programs, one for every deserialization library. All of them practice general attributes from the Google.Events equipment, which has no dependencies itself other than the CloudEvents SDK, and is what the Functions Framework depends on.

At the moment we’ve only utilized the protobuf-based fully mostly option, nonetheless I elevate out want to safe to the others.

(Blow their non-public horns that for the time being the CloudEvents SDK itself depends on Newtonsoft.Json, nonetheless I’m hoping we are in a position to take away that dependency earlier than we free up version 2.0 of the CloudEvents SDK, which I’m working on collectively with Microsoft.)

That each one sounds astronomical, nonetheless it design we’ve purchased three diversified representations of MessagePublishedData – one for every serialization skills. It would possibly maybe possibly be if truth be told tremendous if lets bear appropriate one illustration, which all of them deserialized to, based fully mostly on which serialization equipment you took place to employ. That’s an issue I haven’t solved yet.

I’m hoping that in the realm of functions that acquired’t matter too considerable, nonetheless clearly CloudEvents would possibly maybe possibly additionally be produced and consumed in appropriate about any code… and at the very least, it’s a diminutive bit annoying.

Writing CloudEvent functions

I’m now not going to new the identical form of “hi there world” skills for CloudEvent functions as for HTTP functions, fair because they’re much less “fingers on”. Even I don’t safe too angry by publishing a Pub/Sub message and seeing a log entry that claims “I purchased a Pub/Sub message with at this timestamp.”

As a substitute, I’ll device your consideration to an example with paunchy code in the .NET Functions Framework repository.

It’s an example which is in some ways pretty standard of how I take a look at CloudEvent functions being veteran – effectively as plumbing between other APIs. This particular examples listens for Google Cloud Storage events where an object has been created or updated, and integrates it with the Google Cloud Imaginative and prescient API to impact image recognition and annotation. The steps involved are:

  • The object is created or updated in a Storage bucket
  • An tournament is generated, which triggers the CloudEvent fair
  • The fair checks the negate form and filename, to admire whether or now not it’s doubtless an image. (If it isn’t, it stops at this level.)
  • It asks the Imaginative and prescient API to impact some standard image recognition, procuring for faces, textual negate, landmarks and plenty others.
  • The result’s summarised in a “textual negate file object” which is created alongside the customary image file.

The consumer skills is that they’ll tumble an image into Storage bucket, and a couple of seconds later there’s a 2d file new with info in regards to the image… all in a moderately diminutive amount of code.

The example needs to be easy to enviornment up, assuming it’s good to bear both Storage and Imaginative and prescient APIs enabled – it’s then very easy to examine. Whereas you’re taking a admire at that example, I again you to admire at the opposite examples in the repository, as they bellow any other parts I haven’t covered.

For sure, the total identical testing parts for HTTP functions come in for CloudEvent functions too, and there are helper suggestions in FunctionTestBase to impact the fair based fully mostly on an tournament and plenty others. Admittedly API-like dependencies are inclined to be extra mighty to take out than IClock, nonetheless the fair-particular mechanisms are aloof the identical.

It’s been so considerable fun to checklist what I’ve been working on, and the design I’ve tried to predict standard employ cases and impact them easy to enforce with the .NET Functions Framework.

The framework is now in beta, meaning there’s aloof time to impact some changes if we would like to… nonetheless we acquired’t know the changes are required unless we safe feedback. So I strongly again you to present it a strive, whether or now not it’s good to bear skills of FaaS on other platforms or now not.

Feedback is most productive left by factors on the GitHub repository – I’d like to be swamped!

I’m sure there’ll be extra to discuss about in future blog posts, nonetheless this one is already pleasing colossal, so I’ll dash away it there for now…

Read More

Leave A Reply

Your email address will not be published.