简体   繁体   English

.netcore Azure 函数启动 class 未被调用

[英].netcore Azure functions startup class is not called

My Azure function doesn't calls the startup class localy.我的 Azure function 不会在本地调用启动 class。 When running the project, my brekpoint doesn't hit the DependencyRegistrations.Register function.运行项目时,我的断点没有命中 DependencyRegistrations.Register function。

Package Microsoft.Azure.Functions.Extensions is correctly installed Package 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);
        }
    }
}

在此处输入图像描述

Why is the startup class not called?为什么没有调用启动class?

Two things I'm not seeing in your code snippet.我在您的代码片段中没有看到两件事。

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

2- Are you sure the nuget package was properly installed? 2- 您确定 nuget package 已正确安装吗? (Microsoft.Azure.Functions.Extensions) (Microsoft.Azure.Functions.Extensions)

The final startup code should look like the following:最终的启动代码应该如下所示:

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>();
        }
    }
}

I face the same issue, i have to remove my project from my solution and recreate an new to have the statup to be called...我面临同样的问题,我必须从我的解决方案中删除我的项目并重新创建一个新的以调用状态...

I suspect a version mistake somewhere我怀疑某个地方有版本错误

Just in case you're running v4, Startup is not used.以防万一您正在运行 v4,不使用 Startup。

Perform the dependency injection setup in Program.cs:在 Program.cs 中执行依赖注入设置:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(builder =>
    {
        builder.AddTransient<IUserService, UserService>();
        builder.AddTransient<ICompetitionService, CompetitionService>();
        builder.AddTransient<ICompetitionRepository, CompetitionRepository>();
    })
    .Build();

host.Run();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM