TOC
Summary
How to use the App startup on Azure Functions?
Azure Functions provide the FunctionsStartup as App startup. The FunctionsStartup is made up assembly attribute and class in Microsoft.Azure.Functions.Extensions.DependencyInjection
namespace.
Let’s try to use it.
Prerequisites
- .NET Core v3.1
- Azure Functions v3
- Microsoft.NET.Sdk.Functions v3.0.3
- Microsoft.Azure.Functions.Extensions v1.1.0
- Visual Studio for Mac v8.10
- Azure Functions Core Tools v3.0.3873
- C# v8
- macOS v11.6
Steps
Install Microsoft.Azure.Functions.Extensions
package via NuGet.
Add Startup.cs
, then you write the following code:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(YourNameSpace.Startup))]
namespace YourNameSpace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
Console.WriteLine("hoge1");
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
Console.WriteLine("hoge2");
base.ConfigureAppConfiguration(builder);
}
}
}
Run your Azure Functions project, you will get the below log:
Azure Functions Core Tools
Core Tools Version: 3.0.3873 Commit hash: c546cd456f0e58058cf6c37365eb5ccc5bb0840b (64-bit)
Function Runtime Version: 3.3.1.0
[2021-10-21T05:34:42.388Z] Found /Path/to/your/azure-function-project.csproj. Using for user secrets file configuration.
[2021-10-21T05:34:43.475Z] Cannot create directory for shared memory usage: /dev/shm/AzureFunctions
[2021-10-21T05:34:43.475Z] System.IO.FileSystem: Access to the path '/dev/shm/AzureFunctions' is denied. Operation not permitted.
hoge2
hoge1
Workaround
An error occurs if you use Microsoft.Azure.Functions.Extensions v1.1.0 and Azure Function Core Tools v3.0.2358. The log is below:
[10/21/2021 2:00:01 AM] A host error has occurred during startup operation 'a14dd57a-7186-436a-a012-1fbaec51c59f'.
[10/21/2021 2:00:01 AM] System.Private.CoreLib: Could not load type 'Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup2' from assembly 'Microsoft.Azure.WebJobs.Host, Version=3.0.17.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Workaround are following:
- Workaround 1: Upgrade
Azure Functions Core Tools
to latest version- It just refresh the template for the Azure Functions when you click a refresh button in the “Configure your Azure Functions project” window which is displayed to create new Azure Functions with Visual Studio for Mac.
- Workaround 2: Downgrade
Microsoft.Azure.Functions.Extensions
to v1.0.0
See more the information:
References
- App startup in ASP.NET Core | Microsoft Docs
- Use dependency injection in .NET Azure Functions | Microsoft Docs
- FunctionsStartupAttribute.cs – Azure/azure-functions-dotnet-extensions – GitHub
- FunctionsStartup.cs – Azure/azure-functions-dotnet-extensions – GitHub
- Set assembly attributes | Microsoft Docs
- Could not load type ‘Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup2’ · Issue #54 · Azure/azure-functions-dotnet-extensions · GitHub