如何在 Azure Functions 中混合使用自定义参数绑定和依赖项注入?
How do I mix custom parameter binding with dependency injection in Azure Functions?
我有一个 Azure Functions 应用程序,它使用在 [WebJobsStartup] 中初始化的自定义参数绑定 - 如何将它们迁移到新的 [FunctionsStartup] 做事方式?
在 [WebJobsStartup] 中,绑定是通过使用 .AddBindingRule
调用 builder.AddExtension 注册的
现在新的 Azure SDK 说要使用 [FunctionsStartup] - 我如何在其中挂接(或替换).AddBindingRule?
FunctionsStartup 实现如下所示:-
[assembly: FunctionsStartup(typeof(EventSourcingOnAzureFunctions.Common.Startup))]
namespace EventSourcingOnAzureFunctions.Common
{
public class Startup
: FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.AddAppSettingsToConfiguration();
// Initialise any common services
CQRSAzureBindings.InitializeServices(builder.Services);
builder.Services.AddWebJobs(ConfigureWebJobs );
}
public void ConfigureWebJobs(JobHostOptions hostOptions)
{
}
}
它用来注册绑定规则的方式是这样的:-
/// <summary>
/// Initailise any common dependency injection configuration settings
/// </summary>
/// <param name="context"></param>
public static void InitializeInjectionConfiguration(ExtensionConfigContext context)
{
// Set up the dependency injection stuff for the custom bindings
// 1: EventStream
context
.AddBindingRule<EventStreamAttribute>()
.BindToInput<EventStream>(BuildEventStreamFromAttribute)
;
}
如果我 运行 函数应用程序作为绑定规则代码不会被调用,结果函数索引失败,例如
Microsoft.Azure.WebJobs.Host: Error indexing method 'DepositMoney'.
Microsoft.Azure.WebJobs.Host: Cannot bind parameter
'bankAccountEvents' to type EventStream. Make sure the parameter Type
is supported by the binding. 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.).
为此,您需要两个单独的启动 classes。一个继承自 FunctionsStartup,另一个实现 IWebJobsStartup 接口。 WebJobsStartup 首先运行,然后是 FunctionsStartup。
这是我的测试方法。
WebJobsStartup class
namespace CustomBinding
{
public class WebJobsStartup : IWebJobsStartup
{
void IWebJobsStartup.Configure(IWebJobsBuilder builder)
{
Debug.WriteLine("WebJobsStartup.Configure");
}
}
}
程序集信息class
using Microsoft.Azure.WebJobs.Hosting;
[assembly: WebJobsStartup(typeof(CustomBinding.WebJobsStartup))]
启动Class
[assembly: FunctionsStartup(typeof(CustomBinding.Startup))]
namespace CustomBinding
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
Debug.WriteLine("FunctionsStartup.Configure");
}
}
}
您可以获得 [WebJobsStartup] 的实例以在 Startup.cs
中调用 builder.AddExtension
就我而言,我需要使用 NuGet 包的扩展
using Microsoft.Azure.WebJobs.Extensions.Kafka;
但是我和你经历了同样的情况,但最后,我成功了:)
[assembly:
FunctionsStartup(typeof(EventSourcingOnAzureFunctions.Common.Startup))]
namespace EventSourcingOnAzureFunctions.Common
{
public class Startup
: FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var serviceProvider = builders.Services.BuildServiceProvider();
var env = serviceProvider.GetRequiredService<IHostEnvironment>();
var appDirectory = serviceProvider.GetRequiredService<IOptions<ExecutionContextOptions>>().Value.AppDirectory;
// IWebJobsBuilders instance
var wbBuilder = builder.Services.AddWebJobs(x => { return; });
wbBuilder.AddKafka();
// And now you can use AddExtension
// wbBuilder.AddExtension
}
}
我有一个 Azure Functions 应用程序,它使用在 [WebJobsStartup] 中初始化的自定义参数绑定 - 如何将它们迁移到新的 [FunctionsStartup] 做事方式?
在 [WebJobsStartup] 中,绑定是通过使用 .AddBindingRule
调用 builder.AddExtension 注册的现在新的 Azure SDK 说要使用 [FunctionsStartup] - 我如何在其中挂接(或替换).AddBindingRule?
FunctionsStartup 实现如下所示:-
[assembly: FunctionsStartup(typeof(EventSourcingOnAzureFunctions.Common.Startup))]
namespace EventSourcingOnAzureFunctions.Common
{
public class Startup
: FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.AddAppSettingsToConfiguration();
// Initialise any common services
CQRSAzureBindings.InitializeServices(builder.Services);
builder.Services.AddWebJobs(ConfigureWebJobs );
}
public void ConfigureWebJobs(JobHostOptions hostOptions)
{
}
}
它用来注册绑定规则的方式是这样的:-
/// <summary>
/// Initailise any common dependency injection configuration settings
/// </summary>
/// <param name="context"></param>
public static void InitializeInjectionConfiguration(ExtensionConfigContext context)
{
// Set up the dependency injection stuff for the custom bindings
// 1: EventStream
context
.AddBindingRule<EventStreamAttribute>()
.BindToInput<EventStream>(BuildEventStreamFromAttribute)
;
}
如果我 运行 函数应用程序作为绑定规则代码不会被调用,结果函数索引失败,例如
Microsoft.Azure.WebJobs.Host: Error indexing method 'DepositMoney'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'bankAccountEvents' to type EventStream. Make sure the parameter Type is supported by the binding. 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.).
为此,您需要两个单独的启动 classes。一个继承自 FunctionsStartup,另一个实现 IWebJobsStartup 接口。 WebJobsStartup 首先运行,然后是 FunctionsStartup。
这是我的测试方法。
WebJobsStartup class
namespace CustomBinding
{
public class WebJobsStartup : IWebJobsStartup
{
void IWebJobsStartup.Configure(IWebJobsBuilder builder)
{
Debug.WriteLine("WebJobsStartup.Configure");
}
}
}
程序集信息class
using Microsoft.Azure.WebJobs.Hosting;
[assembly: WebJobsStartup(typeof(CustomBinding.WebJobsStartup))]
启动Class
[assembly: FunctionsStartup(typeof(CustomBinding.Startup))]
namespace CustomBinding
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
Debug.WriteLine("FunctionsStartup.Configure");
}
}
}
您可以获得 [WebJobsStartup] 的实例以在 Startup.cs
中调用 builder.AddExtension就我而言,我需要使用 NuGet 包的扩展
using Microsoft.Azure.WebJobs.Extensions.Kafka;
但是我和你经历了同样的情况,但最后,我成功了:)
[assembly:
FunctionsStartup(typeof(EventSourcingOnAzureFunctions.Common.Startup))]
namespace EventSourcingOnAzureFunctions.Common
{
public class Startup
: FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var serviceProvider = builders.Services.BuildServiceProvider();
var env = serviceProvider.GetRequiredService<IHostEnvironment>();
var appDirectory = serviceProvider.GetRequiredService<IOptions<ExecutionContextOptions>>().Value.AppDirectory;
// IWebJobsBuilders instance
var wbBuilder = builder.Services.AddWebJobs(x => { return; });
wbBuilder.AddKafka();
// And now you can use AddExtension
// wbBuilder.AddExtension
}
}