有没有办法 Hook 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;
        }
    }
}

编辑:除了上述内容,我最近了解到您可能不需要使用 host.Call(typeof(Reminders).GetMethod("ManualTrigger"),至少对于连续的 Web 作业而言。

您只需将 Functions 类设为非静态并添加用于注入(inject)的构造函数,然后使您的处理方法成为非静态。这如下图所示。
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

也可以看看

Dependency injection using Azure WebJobs SDK?

对于连续的网络作业

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

关于dependency-injection - 静态函数作用域中 DbContext 的依赖注入(inject) Ninject 与 WebJobs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36579697/

10-13 04:39