具有依赖注入槽构造函数的 Azure 函数找不到作业函数

具有依赖注入槽构造函数的 Azure 函数找不到作业函数

问题描述:

我已经使用 .NET 5 和依赖注入通过类的构造函数创建了 Azure Function 版本 3.见下面的虚拟代码:

I've created an Azure Function version 3 using .NET 5 and dependency injection trough the constructor of the class. See dummy code below:

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 类中添加了作用域.

Inside the Startup class the scopes are added.

[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 文件中包含这行代码:

Inside the .csproj file stands this line of code:

<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 Function 时.我收到了这个警告:

The problem is when I want to run the Azure Function. I've got this warning:

未找到工作职能.尝试公开您的作业类和方法.如果您正在使用绑定扩展(例如 Azure Storage、ServiceBus、Timers 等),请确保您已经在启动代码中调用了扩展的注册方法(例如builder.AddAzureStorage()builder.AddServiceBus()builder.AddTimers() 等).

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.).

我尝试了接下来的事情:

I've tried next things:

  1. 我已经在 Startup 类中添加了 builder.AddTimers()IFunctionsHostBuilder 不包含它的定义.即使我添加 Microsoft.Azure.Functions.Worker.Extensions.Timer.

  1. I've added builder.AddTimers() inside the Startup class but IFunctionsHostBuilder contains no definition for it. Even if I add Microsoft.Azure.Functions.Worker.Extensions.Timer.

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

Making everything static inside MyAzureFunction but don't work because the static constructors can't contain parameters.

还有 builder.Services.AddTimers() (就像在 文档) 未定义.

Also builder.Services.AddTimers() (like in the documentation) is not defined.

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

My question is now how could I use dependency injection using Azure Functions and .NET 5 using the constructor.

在 Program.cs 中做以下事情.(确保为命名空间添加引用)

In Program.cs do following things. ( Make sure you add refence for namespace)

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