C# Azure函数HTTP在一次执行中执行多次
我有一个相对简单的Azure函数,在消费计划下运行,超时设置为4-4分钟 当我通过门户执行时,它会被执行多次,运行彼此重叠,即使我只执行了一次 例如:C# Azure函数HTTP在一次执行中执行多次,c#,azure,azure-function-app,C#,Azure,Azure Function App,我有一个相对简单的Azure函数,在消费计划下运行,超时设置为4-4分钟 当我通过门户执行时,它会被执行多次,运行彼此重叠,即使我只执行了一次 例如: 执行1 00:36:52.917 00:41:25.750 执行2 00:40:41.793 00:41:25.700 执行3 00:44:27.430 00:48:30.857 对于多个和重叠的执行时间没有任何逻辑 知道发生了什么吗 using System; using System.Threading.Tasks; u
- 执行1
- 00:36:52.917
- 00:41:25.750
- 执行2
- 00:40:41.793
- 00:41:25.700
- 执行3
- 00:44:27.430
- 00:48:30.857
using System;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using Microsoft.Data.SqlClient;
namespace FMPApiCall
{
public static class FMPTestAPI
{
#region Private Data Members
private static readonly HttpClient Client = new HttpClient();
#endregion
[FunctionName("FMPTestAPI")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req, ILogger log)
{
log.LogInformation("FMPTestAPI triggered - BEGIN");
List<string> symbolsList = FMPCallAPI.CommonFunctions.GetSymbolsFromDB(); //gets as list of IDs from the database
int counter = 0;
using SqlConnection dbconnection = new SqlConnection(Environment.GetEnvironmentVariable("SqlServerConnectionString"));
{
dbconnection.Open();
foreach (string symbol in symbolsList)
{
counter++;
var apiRequest =
$"https://myapi.com/api/v3/dataset1/{symbol}?period=quarter&limit=4&apikey=123";
var response = await Client.GetAsync(apiRequest);
var symbolsData = await response.Content.ReadAsStringAsync();
var sqlStr = $"INSERT INTO [dbo].[_azure] (symbol,runid, response,lastupdated) VALUES ( '{symbol}' , {counter} ,'{response}', getutcdate() )";
using (SqlCommand cmd = new SqlCommand(sqlStr, dbconnection))
{
var rows = cmd.ExecuteNonQuery();
}
}
dbconnection.Close();
}
log.LogInformation("FMPTestAPI triggered - END");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult(new { Hello = name })
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
使用系统;
使用System.Threading.Tasks;
使用System.Net.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.Extensions.Logging;
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用System.IO;
使用Microsoft.Data.SqlClient;
命名空间调用
{
公共静态类FMPTestAPI
{
#区域私有数据成员
私有静态只读HttpClient客户端=新HttpClient();
#端区
[功能名称(“FMPTestAPI”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]
HttpRequest请求,ILogger日志)
{
日志信息(“FMPTestAPI触发-开始”);
List symbolsList=FMPCallAPI.CommonFunctions.GetSymbolsFromDB();//从数据库获取ID列表
int计数器=0;
使用SqlConnection dbconnection=newsqlconnection(Environment.GetEnvironmentVariable(“SqlServerConnectionString”);
{
dbconnection.Open();
foreach(符号列表中的字符串符号)
{
计数器++;
var API请求=
$"https://myapi.com/api/v3/dataset1/{symbol}?期间=季度&限额=4&apikey=123”;
var response=await Client.GetAsync(apirest);
var symbolsData=await response.Content.ReadAsStringAsync();
var sqlStr=$“插入[dbo].[u azure](符号,runid,response,lastupdated)值(“{symbol}',{counter},{response}',getutcdate())”;
使用(SqlCommand cmd=newsqlcommand(sqlStr,dbconnection))
{
var rows=cmd.ExecuteNonQuery();
}
}
dbconnection.Close();
}
日志信息(“FMPTestAPI触发-结束”);
字符串名称=请求查询[“名称”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
返回名称!=null
?(ActionResult)新建OkObjectResult(新建{Hello=name})
:new BadRequestObjectResult(“请在查询字符串或请求正文中传递名称”);
}
}
}
eee请共享实际日志(屏蔽任何敏感信息)。这几乎是不可能的,除非对某些失败进行重试。这让我走上了正确的方向。执行大约3分50秒Azure会抛出503错误,并以不同的ID启动另一个执行。因此503可能来自您在函数中执行的某个API调用。你查过日志了吗?没有这一点,没有人能在这里提供帮助。