如何在 Azure .Net 5 独立函数中实现单例(或共享资源)?

How to Implement Singleton (or shared resources) in Azure .Net 5 Isolated Functions?

我正在尝试配置我的 .Net 5 Azure 函数(独立)以通过单例或 DI 共享资源,但在这篇文章的“依赖注入”下: https://docs.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide

我在实施 AddSingleton 时遇到错误。

错误: 'IServiceCollection' 不包含 'AddSingleton'

的定义

微软代码段:

.ConfigureServices(s =>
{
    s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
})

我的解读:

public static void Main()
{
    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(s =>
            {
                s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
            })
        .Build();

    host.Run();
}

Microsoft 声明您不需要 DI 的 Startup class,但是几乎没有 doco 因为它刚刚发布 (2021-03-21)。

此代码可在 github:

https://github.com/Azure/azure-functions-dotnet-worker-preview/blob/main/FunctionApp/Program.cs

在已知问题下它指出:

Deployment to Azure is currently limited to Windows plans. Note that some optimizations are not in place in the consumption plan and you may experience longer cold starts.

您 运行 是否在 Linux 应用服务计划下?