在静态函数作用域中使用WebJobs为DbContext注入依赖项Ninject



有没有一种方法可以挂接到WebJobs函数的执行中,这样我们就可以为每个函数指定一个作用域?类似这样的东西:

kernel.Bind<MyDbContext>().ToSelf().InWebJobFunctionScope();

我想使用Ninject中的InScope(),但我不知道在哪里可以找到类似于静态HttpContext.Current的东西,但适用于当前运行的WebJob。

我知道这是一部老剧,但我也有同样的戏剧。由于是最新版本的web作业,您可以使用实例和实例方法,并传入自定义的IJobActivator实例。它出奇地容易。

它与Ninject完美配合。我还没有看到任何Ninject的例子,所以。。。

public class MyJobActivator : IJobActivator
{
    protected readonly IKernel _kernel;
    public MyJobActivator(IKernel kernel)
    {
        _kernel = kernel;
    }
    public T CreateInstance<T>()
    {
        return _kernel.Get<T>();
    }
}

public class MyBindings : NinjectModule
{
    public override void Load()
    {     
        Bind(typeof(DbContext)).To(typeof(MyEntities));
    }
}
class Program
{        
    static void Main()
    {
        using (IKernel kernel = new StandardKernel(new MyBindings()))
        {
            var jobHostConfiguration = new JobHostConfiguration
            {
                JobActivator = new MyJobActivator(kernel)
            };
            var host = new JobHost(jobHostConfiguration);
            // The following code will invoke a function called ManualTrigger and 
            // pass in data (value in this case) to the function
            host.Call(typeof(Reminders).GetMethod("ManualTrigger"), new { value = 20 });
        }
    }
}

public class Reminders
{
    private readonly IMyService _myService;
    public Reminders(IMyService myService)
    {
        _myService = myService;
    }
    // This function will be triggered based on the schedule you have set for this WebJob
    // This function will enqueue a message on an Azure Queue called queue
    [NoAutomaticTrigger]
    public async Task ManualTrigger(TextWriter log, int value, TextWriter logger)
    {
        try
        {   
            // process the notification request
            await _myService.FindAndSendReminders();
            await _myService.SaveChangesAsync();
        }
        catch (Exception e)
        {
            logger.WriteLine(e.Message);
            Console.WriteLine(e.Message);
            throw;
        }
    }
}

编辑:除了以上我最近看到的了解,你可能不需要使用主机。调用(typeof(Reminders).GetMethod("ManualTrigger"),至少对于连续的web作业。

您只需使Function类非静态,并添加一个用于注入的构造函数,然后使处理方法非静态。这如下图所示。

public class Program
{        
    static void Main()
    {
        using (IKernel kernel = new StandardKernel(new MyBindings()))
        {
            var jobHostConfiguration = new JobHostConfiguration
            {
                JobActivator = new MyJobActivator(kernel)
            };
            var host = new JobHost(jobHostConfiguration);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }
    }
}

public class Functions
{
    private readonly IMyService _myService;
    public Functions(IMyService myService)
    {
        _myService = myService;
    }        
    public async Task ProcessReminders([QueueTrigger("reminder-requests")] string notificationMessage, TextWriter logger)
    {
        try
        {   
            // process the notification request
            await _myService.FindAndSendReminders();
            await _myService.SaveChangesAsync();
        }
        catch (Exception e)
        {
            logger.WriteLine(e.Message);
            Console.WriteLine(e.Message);
            throw;
        }
    }
}

我改编了我为Autofac 找到的一篇文章中的原始代码

http://www.jerriepelser.com/blog/dedependency-injection-with-autofac-and-webjobs

另请参见

使用Azure WebJobs SDK进行依赖项注入?

对于连续的网络作业

http://www.ryansouthgate.com/2016/05/10/azure-webjobs-and-dependency-injection/

相关内容

  • 没有找到相关文章

最新更新