Я знаю, что это старый, но у меня были такие же драмы. Начиная с более поздней версии веб-заданий, вы можете использовать экземпляры и методы экземпляров, а также передавать пользовательские экземпляры 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"), по крайней мере, для непрерывных веб-заданий.
Вы просто делаете свой класс функций нестатическим и добавляете конструктор для инъекции, а затем делаете свой метод обработки нестатическим. Это показано ниже.
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
Смотрите также
Внедрение зависимостей с помощью SDK Azure WebJobs?
И для непрерывных веб-заданий
http://www.ryansouthgate.com/2016/05/10/azure-webjobs-and-dependency-injection/
person
Troyka
schedule
27.07.2016