gpt4 book ai didi

azure - Azure WebJobs 的 JobHostingConfiguration 的 AddService 的用途是什么

转载 作者:行者123 更新时间:2023-12-03 04:20:35 25 4
gpt4 key购买 nike

我有以下 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 的方法,并且我尝试使用它注入(inject)我的 appSettings,但它失败并出现错误“异常绑定(bind)参数”。

那么 CancellationToken 如何包含在实例化中以及 JobHostingConfiguration AddService 的用途是什么?

最佳答案

how does CancellationToken get included in the instantiation

您可以使用WebJobsShutdownWatcher 类,因为它有一个 Register 函数,该函数在取消 token 被取消时(即 Web 作业停止时)被调用。

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();
}

what is JobHostingConfiguration AddService used for?

<强> Add Services :通过调用 AddService<> 覆盖默认服务。要覆盖的常见服务是 ITypeLocator 和 IJobActivator。

这是一个自定义IJobActivator允许你使用DI,你可以引用它来支持实例方法。

关于azure - Azure WebJobs 的 JobHostingConfiguration 的 AddService 的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49535406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com