我有以下WebJob功能......
public class Functions
{
[NoAutomaticTrigger]
public static void Emailer(IAppSettings appSettings, TextWriter log, CancellationToken cancellationToken)
{
// Start the emailer, it will stop on dispose
using (IEmailerEndpoint emailService = new EmailerEndpoint(appSettings))
{
// Check for a cancellation request every 3 seconds
while (!cancellationToken.IsCancellationRequested)
{
Thread.Sleep(3000);
}
log.WriteLine("Emailer: Canceled at " + DateTime.UtcNow);
}
}
}
我一直在研究如何通过简单的调用来实例化这个...
host.Call(typeof(Functions).GetMethod("MyMethod"), new { appSettings = settings })
然而,它让我想知道TextWriter和CancellationToken是如何包含在实例化中的。我发现JobHostingConfiguration有AddService的方法,我试图使用此方法注入我的appSettings,但它已经失败并出现错误'异常绑定参数'。
那么CancellationToken如何包含在实例化中以及JobHostingConfiguration AddService用于什么?
答案 0 :(得分:0)
CancellationToken如何包含在实例化中
您可以使用 WebJobsShutdownWatcher 类,因为它有取消令牌取消时调用的注册功能,换句话说,当webjob是停止。
static void Main()
{
var cancellationToken = new WebJobsShutdownWatcher().Token;
cancellationToken.Register(() =>
{
Console.Out.WriteLine("Do whatever you want before the webjob is stopped...");
});
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
什么是JobHostingConfiguration AddService用于?
Add Services :通过调用AddService<>覆盖默认服务。要覆盖的常见服务是ITypeLocator和IJobActivator。
这是一个自定义IJobActivator允许您使用DI,您可以参考它来支持实例方法。