{
  "$type": "site.standard.document",
  "canonicalUrl": "https://johnnyreilly.com/posts/c-sharp-9-azure-functions-in-process",
  "description": "Learn to use C# 9 with .NET Core 3.1 Azure Functions in this step-by-step guide by adding new elements to your .csproj file.",
  "path": "/posts/c-sharp-9-azure-functions-in-process",
  "publishedAt": "2021-07-01T00:00:00.000Z",
  "site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
  "tags": [
    "c#",
    "azure functions",
    "asp.net"
  ],
  "textContent": "C9 has some amazing features. Azure Functions are have two modes: isolated and in-process. Whilst isolated supports .NET 5 (and hence C9), in-process supports .NET Core 3.1 (C8). This post shows how we can use C9 with in-process Azure Functions running on .NET Core 3.1.\n\n\n\nAzure Functions: in-process and isolated\n\nHistorically .NET Azure Functions have been in-process. This changed with .NET 5 where a new model was introduced named \"isolated\". To quote from the roadmap:\n\n> Running in an isolated process decouples .NET functions from the Azure Functions host—allowing us to more easily support new .NET versions and address pain points associated with sharing a single process.\n\nHowever, the initial launch of isolated functions does not have the full level of functionality enjoyed by in-process functions. This will happen, according the roadmap:\n\n> Long term, our vision is to have full feature parity out of process, bringing many of the features that are currently exclusive to the in-process model to the isolated model. We plan to begin delivering improvements to the isolated model after the .NET 6 general availability release.\n\nIn the future, in-process functions will be retired in favour of isolated functions. However, it will be .NET 7 (scheduled to ship in November 2022) before that takes place:\n\nAs the image taken from the roadmap shows, when .NET 5 shipped, it did not support in-process Azure Functions. When .NET 6 ships in November, it should.\n\nIn the meantime, we would like to use C9.\n\nSetting up a C8 project\n\nWe're have the Azure Functions Core Tools installed, so let's create a new function project:\n\nThe above command scaffolds out a .NET Core 3.1 Azure function project which contains a single Azure function. The --worker-runtime dotnet parameter is what causes an in-process .NET Core 3.1 function being created. You should have a .csproj file that looks like this:\n\nWe're running with C8 and .NET Core 3.1 at this point. What does it take to get us to C9?\n\nWhat does it take to get to C9?\n\nThere's a great post on Reddit addressing using C9 with .NET Core 3.1 which says:\n\n> You can use <LangVersion>9.0</LangVersion>, and VS even includes support for suggesting a language upgrade.\n>\n> However, there are three categories of features in C#:\n>\n> 1. features that are entirely part of the compiler. Those will work.\n> 2. features that require BCL additions. Since you're on the older BCL, those will need to be backported. For example, to use init; and record, you can use https://github.com/manuelroemer/IsExternalInit.\n> 3. features that require runtime additions. Those cannot be added at all. For example, default interface members in C8, and covariant return types in C9.\n\nOf the above, 1 and 2 add a tremendous amount of value. The features of 3 are great, but more niche. Speaking personally, I care a great deal about Record types. So let's apply this.\n\nAdding C9 to the in-process function\n\nTo get Cinto the mix, we want to make two changes:\n\n- add a <LangVersion>9.0</LangVersion> to the <PropertyGroup> element of our .csproj file\n- add a package reference to the IsExternalInit\n\nThe applied changes look like this:\n\nIf we used dotnet add package IsExternalInit, we might be using a different syntax in the .csproj. Be not afeard - that won't affect usage.\n\nMaking a C9 program\n\nNow we can theoretically use C9.... Let's use C9. We'll tweak our HelloRecord.cs file, add in a simple record named MessageRecord and tweak the Run method to use it:\n\nIf we kick off our function with func start:\n\nWe can see we can compile, and output is as we might expect and hope. Likewise if we try and debug in VS Code, we can:\n\nBest before...\n\nSo, we've now a way to use C9 (or most of it) with in-process .NET Core 3.1 apps. This should serve until .NET 6 ships in November 2021 and we're able to use C9 by default.",
  "title": "C# 9 in-process Azure Functions"
}