首页 > 解决方案 > .netcore Azure 函数启动类未被调用

问题描述

我的 Azure 函数不会本地调用启动类。运行项目时,我的断点没有命中 DependencyRegistrations.Register 函数。

包 Microsoft.Azure.Functions.Extensions 已正确安装

using Microsoft.Azure.Functions.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            DependencyRegistrations.Register(builder.Services);
        }
    }
}

在此处输入图像描述

为什么没有调用启动类?

标签: azure.net-coreazure-functions

解决方案


我在您的代码片段中没有看到两件事。

1- [程序集:FunctionsStartup(typeof(MyNamespace.Startup))]

2-您确定正确安装了nuget包吗?(Microsoft.Azure.Functions.Extensions)

最终的启动代码应如下所示:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();

            builder.Services.AddSingleton<IMyService>((s) => {
                return new MyService();
            });

            builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
        }
    }
}

推荐阅读