通过构造函数进行依赖注入的 Azure Functions 找不到作业函数

Azure Functions with dependance injection trough constructor don't find job functions

我已经使用 .NET 5 和依赖注入通过 class 的构造函数创建了一个 Azure Function 版本 3。请参阅下面的虚拟代码:

public class MyAzureFunction
{
    private readonly IMyRepository _myRepository;

    public MyAzureFunction(IMyRepository myRepository) 
    {
        _myRepository = myRepository;
    }

    [Function("MyAzureFunction")]
    public async Task Run([TimerTrigger("0 */15 * * * *")] TimerInfo myTimer, FunctionContext context)
    {
        ILogger logger = context.GetLogger("MyAzureFunction");
        logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        List<object> result = await _myRepository.GetAllAsync();

        // Keep going...
    }
}

Startup class 中添加了作用域。

[assembly: FunctionsStartup(typeof(MyNamespace.Functions.Startup))]
namespace MyNamespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services
                .AddScoped<IMyRepository, MyRepository>();
        }
    }
}

程序文件如下所示:

public class Program
{
    public static void Main()
    {
        IHost host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .Build();

        host.Run();
    }
}

.csproj 文件中有这行代码:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <AzureFunctionsVersion>v3</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
    </ItemGroup>
    <!-- Skiped some lines -->
</Project>

问题是当我想要 运行 Azure 函数时。我收到此警告:

No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

我尝试了接下来的事情:

  1. 我在 Startup class 中添加了 builder.AddTimers()IFunctionsHostBuilder 没有包含它的定义。即使我添加 Microsoft.Azure.Functions.Worker.Extensions.Timer.

  2. 使内部的所有内容都变为静态 MyAzureFunction 但不起作用,因为静态构造函数不能包含参数。

  3. builder.Services.AddTimers() (如the documentation未定义。

我现在的问题是如何使用 Azure Functions 和 .NET 5 使用构造函数来使用依赖注入。

在 Program.cs 中执行以下操作。 (确保添加命名空间的引用)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace FunctionApp2
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureServices(services =>
                {
                    services.AddScoped<IMyRepository, MyRepository>();
                })
                .ConfigureFunctionsWorkerDefaults()
                .Build();

            host.Run();
        }
    }
}

您需要添加包:

  • Microsoft.Azure.Functions.Extensions

  • Microsoft.Azure.Functions.Worker

  • Microsoft.Azure.Functions.Worker.Sdk

然后确保删除以前的 .NET Core 3 包。