简体   繁体   English

将依赖注入槽构造函数与 Azure Functions 结合使用

[英]Use dependance injection trough constructor with Azure Functions

I've created an Azure Function version 3 using .NET 5 and dependency injection trough the constructor of the class.我已经使用 .NET 5 和依赖注入通过类的构造函数创建了 Azure Function 版本 3。 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...
    }
}

Inside the Startup class the scopes are added.Startup类中添加了作用域。

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

The Program file looks as follow:程序文件如下所示:

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

        host.Run();
    }
}

Inside the .csproj file stands this line of code:.csproj文件中,有这行代码:

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
</PropertyGroup>

The problem is when I want to run the Azure Function.问题是当我想运行 Azure 函数时。 I've got this warning:我有这个警告:

No job functions found.没有找到工作职能。 Try making your job classes and methods public.尝试公开您的作业类和方法。 If you're using binding extensions (eg Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (eg builder.AddAzureStorage() , builder.AddServiceBus() , builder.AddTimers() , etc.) .如果您使用绑定扩展(例如 Azure Storage、ServiceBus、Timers 等),请确保您已在启动代码中调用了扩展的注册方法(例如builder.AddAzureStorage()builder.AddServiceBus()builder.AddTimers()等)

I've tried next things:我已经尝试了接下来的事情:

  1. I've added builder.AddTimers() inside the Startup class but IFunctionsHostBuilder contains no definition for it.我在Startup类中添加了builder.AddTimers()IFunctionsHostBuilder包含它的定义。 Even if I add Microsoft.Azure.Functions.Worker.Extensions.Timer .即使我添加Microsoft.Azure.Functions.Worker.Extensions.Timer

  2. Making everything static inside MyAzureFunction but don't work because the static constructors can't contain parameters.MyAzureFunction所有内容MyAzureFunction静态但不起作用,因为静态构造函数不能包含参数。

  3. Also builder.Services.AddTimers() (like in the documentation ) is not defined.此外builder.Services.AddTimers() (如在文档中未定义。

My question is now how could I use dependency injection using Azure Functions and .NET 5 using the constructor.我现在的问题是如何使用 Azure Functions 和 .NET 5 使用构造函数使用依赖项注入。

In Program.cs do following things.在 Program.cs 中做以下事情。 ( 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();
        }
    }
}

You need to add package - Microsoft.Azure.Functions.Extensions - Microsoft.Azure.Functions.Worker - Microsoft.Azure.Functions.Worker.Sdk您需要添加包 - Microsoft.Azure.Functions.Extensions - Microsoft.Azure.Functions.Worker - Microsoft.Azure.Functions.Worker.Sdk

Then make sure the previous packages for net core 3 are removed然后确保删除了之前用于 net core 3 的包

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

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