Azure C#(.NET)计时器功能未按计划或手动运行
我有一个带有计时器触发器的Azure函数。时间表是“*/6****”(每六分钟运行一次)。我无法手动运行它,它也不会自动启动。下面是我的Azure C#(.NET)计时器功能未按计划或手动运行,azure,azure-functions,azure-app-service-plans,Azure,Azure Functions,Azure App Service Plans,我有一个带有计时器触发器的Azure函数。时间表是“*/6****”(每六分钟运行一次)。我无法手动运行它,它也不会自动启动。下面是我的函数.json: { "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.31", "configurationSource": "attributes", "bindings": [ {
函数.json
:
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.31",
"configurationSource": "attributes",
"bindings": [
{
"type": "timerTrigger",
"schedule": "%TimerTriggerPeriod%",
"useMonitor": true,
"runOnStartup": false,
"name": "myTimer"
}
],
"disabled": false,
"scriptFile": "../bin/AccessChangeMonitoring.dll",
"entryPoint": "Microsoft.IT.Security.AccessChangeMonitoring.AccessChangeMonitoring.InitiateChangeMonitoring"
}
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
"functionTimeout": "00:07:00"
}
%TimerTriggerPeriod%
在我的local.settings.json
文件(“TimerTriggerPeriod”:“0*/6****”
)中定义。查看仪表板上的应用程序函数计数度量显示,显示my函数已执行0次:
下面是我的host.json
:
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.31",
"configurationSource": "attributes",
"bindings": [
{
"type": "timerTrigger",
"schedule": "%TimerTriggerPeriod%",
"useMonitor": true,
"runOnStartup": false,
"name": "myTimer"
}
],
"disabled": false,
"scriptFile": "../bin/AccessChangeMonitoring.dll",
"entryPoint": "Microsoft.IT.Security.AccessChangeMonitoring.AccessChangeMonitoring.InitiateChangeMonitoring"
}
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
"functionTimeout": "00:07:00"
}
以下是功能代码:
[FunctionName("InitiateChangeMonitoring")]
public static async Task InitiateChangeMonitoring([TimerTrigger("%TimerTriggerPeriod%")] TimerInfo myTimer, ILogger log)
{
log.LogInformation("Change Monitoring started.");
// Reset the listing of app ids we need to retrieve delta role assignments for
oneAuthZAppIds = new List<string>();
await GetOneAuthZAppIdsAsync();
// Create the necessary Cosmos DB infastructure
await CreateDatabaseAsync();
await CreateContainerAsync();
await CreateDeltaAPICallDatabaseAsync();
await CreateDeltaAPICallContainerAsync();
await CreateManagerMappingDatabaseAsync();
await CreateManagerMappingContainerAsync();
// Compute the authentication token needed to access the PAP Service API
log.LogInformation("\nRetrieve PAPServiceAPIToken");
string PAPServiceAPIToken = await GetTokenAsync(Environment.GetEnvironmentVariable("OneAuthZAppUri"), Environment.GetEnvironmentVariable("OneAuthZAppId"),
PAPAuthenticationSecret);
log.LogInformation("PAPServiceAPIToken = " + PAPServiceAPIToken);
string GraphAPIAuthenticationToken = await GetTokenAsync(Environment.GetEnvironmentVariable("GraphAppUri"), Environment.GetEnvironmentVariable("GraphClientId"),
graphKey);
log.LogInformation("graphAPIAuthenticationToken = " + GraphAPIAuthenticationToken);
await runChangeMonitoringSystemAsync(PAPServiceAPIToken);
}
[FunctionName(“InitiateChangeMonitoring”)]
公共静态异步任务InitiateChangeMonitoring([TimerTrigger(“%TimerTriggerPeriod%”)TimerInfo myTimer,ILogger日志)
{
log.LogInformation(“已启动更改监视”);
//重置我们需要为其检索增量角色分配的应用ID列表
oneAuthZAppIds=新列表();
等待GetOneAuthZAppIdsAsync();
//创建必要的宇宙结构
等待CreateDatabaseAsync();
等待CreateContainerAsync();
等待CreateDeltaAPICallDatabaseAsync();
等待CreateDeltaAPICallContainerAsync();
等待CreateManagerMappingDatabaseAsync();
等待CreateManagerMappingContainerAsync();
//计算访问PAP服务API所需的身份验证令牌
log.LogInformation(“\n检索PAPServiceAPIToken”);
字符串PAPServiceAPIToken=await GetTokenAsync(Environment.GetEnvironmentVariable(“OneAuthZAppUri”)、Environment.GetEnvironmentVariable(“OneAuthZAppId”),
身份验证机密);
log.LogInformation(“PAPServiceAPIToken=“+PAPServiceAPIToken”);
字符串GraphAPIAuthenticationToken=await GetTokenAsync(Environment.GetEnvironmentVariable(“GraphAppUri”)、Environment.GetEnvironmentVariable(“GraphClientId”),
格拉夫基);
log.LogInformation(“graphAPIAuthenticationToken=“+graphAPIAuthenticationToken”);
等待runChangeMonitoringSystemAsync(PAPServiceAPIToken);
}
首先,我注意到您指定使用local.settings.json保存环境变量,同时在azure上显示度量
因此,我认为无法触发azure函数的第一个问题是,您没有在azure上设置环境变量
您应该在此处设置,而不是local.settings.json(因为当函数部署到Azure时,它永远不会从local.settings.json获取环境变量):
(不要忘记保存编辑。)
其次,正如Ivan所说,您的cron格式是错误的。timetrigger的格式应为以下格式:
{second} {minute} {hour} {day} {month} {day of week}
您的问题可能是因为您没有在azure中设置环境值。将azure函数部署到azure时,将不会上载local.settings.json。env变量将从此位置而不是local.settings.json获取:您测试过它吗?在我这方面,它是有效的,没有问题。