gpt4 book ai didi

c# - 天蓝色函数 : Difference between IWebJobsStartup and FunctionsStartup

转载 作者:行者123 更新时间:2023-12-05 02:28:58 25 4
gpt4 key购买 nike

我正在使用 Azure Function(v3),我可以在 startup.cs 中使用 IWebJobsStartup 和 FunctionsStartup 注册依赖项。

但是我们应该使用哪一个呢?

向 IWebJobsStartup 注册依赖项

[assembly: WebJobsStartup(typeof(Startup))]
namespace Notifications.Receiver
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddTransient<IEventValidator, EventValidator>();
builder.Services.AddTransient<IEventReceiverHandler, EventReceiverHandler>();

builder.Services.AddTransient<IEventHandler<InvoiceResultDto,InvoiceMessageEvent>, InvoiceMessageEventHandler>();
builder.Services.AddTransient<IEventHandler<InvoiceResultDto, InvoiceFileEvent>, InvoiceFileEventHandler>();
builder.Services.AddSingleton<IMessageBusFactory, AzureServiceBusFactory>();

builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
}
}
}

同样的依赖我也可以用FunctionsStartup注册

[assembly: FunctionsStartup(typeof(Startup))]
namespace Notifications.Receiver
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IEventValidator, EventValidator>();
builder.Services.AddTransient<IEventReceiverHandler, EventReceiverHandler>();

builder.Services.AddTransient<IEventHandler<InvoiceResultDto,InvoiceMessageEvent>, InvoiceMessageEventHandler>();
builder.Services.AddTransient<IEventHandler<InvoiceResultDto, InvoiceFileEvent>, InvoiceFileEventHandler>();
builder.Services.AddSingleton<IMessageBusFactory, AzureServiceBusFactory>();

builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
}
}
}

最佳答案

据我所知,在 Azure Functions 中,现在进行依赖注入(inject)的首选方法是使用 FunctionsStartup

是的,如果我们在Startup.cs中实现IWebJobsStartup接口(interface),我们必须实现Configure(IWebJobsBuilder builder)方法 类。

current MSFT Doc表示从 FunctionsStartup 类继承的 Startup 类以及 Configure 方法采用 IFunctionsHostBuilder .

这个类似thread解释了在 Azure Functions .NET Applications 中使用 FunctionsStartupIWebJobsStartup 读取请求中的 HTTP header 的问题以及最后建议使用 FunctionsStartup更好的方法

关于c# - 天蓝色函数 : Difference between IWebJobsStartup and FunctionsStartup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72404752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com