简体   繁体   English

如何在 Azure Functions 中将自定义参数绑定与依赖项注入混合使用?

[英]How do I mix custom parameter binding with dependency injection in Azure Functions?

I have an Azure Functions app that uses custom parameter bindings that were initialised in the [WebJobsStartup] - how do I migrate these to the new [FunctionsStartup] way of doing things?我有一个 Azure Functions 应用程序,它使用在 [WebJobsStartup] 中初始化的自定义参数绑定 - 如何将它们迁移到新的[FunctionsStartup] 工作方式?

In the [WebJobsStartup] the bindings were registered by the call to builder.AddExtension with .AddBindingRule在 [WebJobsStartup] 中,绑定是通过调用 builder.AddExtension 和 .AddBindingRule 注册的

Now the new Azure SDK says to use [FunctionsStartup] - how do I hook in (or replace) .AddBindingRule in this?现在新的 Azure SDK 说要使用 [FunctionsStartup] - 我如何在这个中挂钩(或替换) .AddBindingRule

The FunctionsStartup implementation looks like:- 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)
        {

        }
}

And the way it used to register the binding rules is like:-它用来注册绑定规则的方式是这样的:-

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

If I run the function app as is the binding rules code does not get called and as a result the function indexing fails with eg如果我按原样运行函数应用程序,则不会调用绑定规则代码,因此函数索引失败,例如

Microsoft.Azure.WebJobs.Host: Error indexing method 'DepositMoney'. Microsoft.Azure.WebJobs.Host:索引方法“DepositMoney”时出错。 Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'bankAccountEvents' to type EventStream. Microsoft.Azure.WebJobs.Host:无法将参数“bankAccountEvents”绑定到类型 EventStream。 Make sure the parameter Type is supported by the binding.确保绑定支持参数类型。 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() 等)。

To do this you would have two separate startup classes.为此,您将拥有两个单独的启动类。 One that inherits from FunctionsStartup and the other that implements the IWebJobsStartup interface.一个继承自 FunctionsStartup,另一个实现 IWebJobsStartup 接口。 The WebJobsStartup runs first, then the FunctionsStartup. WebJobsStartup 首先运行,然后是 FunctionsStartup。

Here's how I tested it.这是我测试它的方法。

WebJobsStartup class WebJobsStartup 类

namespace CustomBinding
{
    public class WebJobsStartup : IWebJobsStartup
    {
        void IWebJobsStartup.Configure(IWebJobsBuilder builder)
        {
            Debug.WriteLine("WebJobsStartup.Configure");
        }
    }
}

AssemblyInfo class装配信息类

using Microsoft.Azure.WebJobs.Hosting;

[assembly: WebJobsStartup(typeof(CustomBinding.WebJobsStartup))]

Startup Class创业班

[assembly: FunctionsStartup(typeof(CustomBinding.Startup))]

namespace CustomBinding
{

    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            Debug.WriteLine("FunctionsStartup.Configure");
        }        
    }   
}

You can get an instance of [WebJobsStartup] to call builder.AddExtension in your Startup.cs您可以获得 [WebJobsStartup] 的实例来调用 Startup.cs 中的 builder.AddExtension

In my case, I need to use an extension from a NuGet package就我而言,我需要使用 NuGet 包中的扩展

using Microsoft.Azure.WebJobs.Extensions.Kafka;

But I passed through the same case as you, but finally, I made it work :)但我和你经历了同样的情况,但最后,我成功了:)

[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

        }
 }

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

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