C# 使用NoAutomaticTrigger时Azure WebJobs SDK中的依赖项注入?

C# 使用NoAutomaticTrigger时Azure WebJobs SDK中的依赖项注入?,c#,azure,dependency-injection,azure-webjobs,azure-webjobssdk,C#,Azure,Dependency Injection,Azure Webjobs,Azure Webjobssdk,我正在构建一个将持续运行的Azure WebJob。我希望WebJob使用与ASP.NET Core相同的DI样式。我找到了这个关于如何使用自定义IJobActivator使DI/IoC工作的答案()。我在使用NoAutomaticTrigger配置连续运行的Web作业时也找到了这个答案() 问题在于,NoAutomaticTrigger方法使用静态方法,这与DI/IoC不兼容。我假设这是因为没有任何东西会导致使用NoAutomaticTrigger方法的服务得到解析(比如队列消息)。我觉得IT

我正在构建一个将持续运行的Azure WebJob。我希望WebJob使用与ASP.NET Core相同的DI样式。我找到了这个关于如何使用自定义IJobActivator使DI/IoC工作的答案()。我在使用NoAutomaticTrigger配置连续运行的Web作业时也找到了这个答案()

问题在于,NoAutomaticTrigger方法使用静态方法,这与DI/IoC不兼容。我假设这是因为没有任何东西会导致使用NoAutomaticTrigger方法的服务得到解析(比如队列消息)。我觉得ITypeLocator可能是一条前进的道路,但这只是一种预感

在其他NoAutomaticTrigger示例中使用的JobHost.Call方法仅限于静态方法。我如何拥有一个通过DI解析的服务,该DI通过Program.cs中对JobHost(或其他方法)的调用进行解析和调用


伊扎·埃迪·森·阿特苏(Iza Eddi son Atsou)在下面提出的解决方案有效,但有一个缺点。我习惯于通过服务上的接口注册我的服务。差不多

serviceCollection.AddSingleton<IApplication, Application>();
serviceCollection.AddSingleton();
问题是,如果接口上有NoAutomaticTrigger属性,SDK就会崩溃。解决方案是将属性添加到类中,并将该类注册为自身

serviceCollection.AddSingleton<Application>();
serviceCollection.AddSingleton();

一旦做出了更改,答案中的解决方案就会非常有效。

好吧,NoAutomaticTrigger属性在两种情况下很有用:带有触发器的函数可以防止自动调用触发器,允许手动轮询。没有其他属性的函数将该函数标记为可用的作业函数。在这两种情况下,JobHost永远不会自动调用标记有此属性的函数(在RunAndBlock期间)。相反,必须使用Call方法手动调用它们。而且,该属性也可以用于非静态方法

以下是一些关于使用Azure WebJobs和依赖项注入的相关文章,您可以参考它们:
而且

AFAIK,NoAutomaticTrigger属性在两种情况下很有用:带有触发器的函数防止自动调用触发器,允许手动轮询。没有其他属性的函数将该函数标记为可用的作业函数。在这两种情况下,JobHost永远不会自动调用标记有此属性的函数(在RunAndBlock期间)。相反,必须使用Call方法手动调用它们。而且,该属性也可以用于非静态方法

以下是一些关于使用Azure WebJobs和依赖项注入的相关文章,您可以参考它们:
而且

WebJobs SDK现在支持触发实例方法,而不仅仅是静态方法。NoAutomaticTrigger方法不再需要是静态的。更新SDK,使用您找到的自定义IJobActivator方法,并记住注册您的函数类,当然还有其他依赖项

示例程序类:

class Program
{

    static void Main()
    {
        var container = new UnityContainer();

        //the instance to be injected
        var systemClient = new JobSystemClient
        {
            UserName = "admin",
            PassWord = "admin1234"
        };

        container.RegisterInstance<ISystemClient>(systemClient);

        //Registration of the Functions class
        container.RegisterType<Functions>();            

        var activator = new UnityJobActivator(container);

        var config = new JobHostConfiguration();
        config.JobActivator = activator;            

        var host = new JobHost(config);
        // The following code will invoke a function called ManualTrigger and 
        // pass in data (value in this case) to the function
        host.Call(typeof(Functions).GetMethod("ManualTrigger"), new { value = 20 });

        host.RunAndBlock();

    }
}
以下是输出:

Found the following functions:
TestWebJob.Functions.ManualTrigger
Executing 'Functions.ManualTrigger' (Reason='This function was programmatically called via the host APIs.', Id=bf9aedc0-89d1-4ba0-a33e-9b23e0d7b8a2)
Function is invoked with value=20
Following message will be written on the Queue=20
username:admin and password:admin1234
Executed 'Functions.ManualTrigger' (Succeeded, Id=bf9aedc0-89d1-4ba0-a33e-9b23e0d7b8a2)
Job host started

WebJobs SDK现在支持触发实例方法,而不仅仅是静态方法。NoAutomaticTrigger方法不再需要是静态的。更新SDK,使用您找到的自定义IJobActivator方法,并记住注册您的函数类,当然还有其他依赖项

示例程序类:

class Program
{

    static void Main()
    {
        var container = new UnityContainer();

        //the instance to be injected
        var systemClient = new JobSystemClient
        {
            UserName = "admin",
            PassWord = "admin1234"
        };

        container.RegisterInstance<ISystemClient>(systemClient);

        //Registration of the Functions class
        container.RegisterType<Functions>();            

        var activator = new UnityJobActivator(container);

        var config = new JobHostConfiguration();
        config.JobActivator = activator;            

        var host = new JobHost(config);
        // The following code will invoke a function called ManualTrigger and 
        // pass in data (value in this case) to the function
        host.Call(typeof(Functions).GetMethod("ManualTrigger"), new { value = 20 });

        host.RunAndBlock();

    }
}
以下是输出:

Found the following functions:
TestWebJob.Functions.ManualTrigger
Executing 'Functions.ManualTrigger' (Reason='This function was programmatically called via the host APIs.', Id=bf9aedc0-89d1-4ba0-a33e-9b23e0d7b8a2)
Function is invoked with value=20
Following message will be written on the Queue=20
username:admin and password:admin1234
Executed 'Functions.ManualTrigger' (Succeeded, Id=bf9aedc0-89d1-4ba0-a33e-9b23e0d7b8a2)
Job host started

谢谢在通过接口注册包含函数的类时,我不得不做一些修改。谢谢。。我缺少了必须将
函数添加到服务集合中的部分。。。然后砰的一声。所有人都开始工作了。很不错的!谢谢,如果您提供您要求更新的SDK版本,+1会更好。现在有两个版本,一个用于.NETCore(3.x),另一个用于.Net(2.x),谢谢!在通过接口注册包含函数的类时,我不得不做一些修改。谢谢。。我缺少了必须将
函数添加到服务集合中的部分。。。然后砰的一声。所有人都开始工作了。很不错的!谢谢,如果您提供您要求更新的SDK版本,+1会更好。目前有两个版本,一个用于.NETCore(3.x),另一个用于.Net(2.x)