http函数c#sql异步

neekobn8  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(169)

我有一个http触发器,它调用一个支持函数来对一些json执行sql更新。我在支持函数中犯了一个错误,无意中使用了command.executenonqueryasync()。这段代码在vsstudio本地运行得很好,100%的时间。但是,当我发布到azure时,这个函数只工作一次。使它再次工作的唯一方法是启动/停止函数。我执行了内存转储,看到有一个线程正在运行。http请求总是返回200,即使它只更改了一次数据。
现在是我的问题。azure http函数不等待线程完成后才退出?我只是想更好地理解。
谢谢

public static class SetNotificationConfig
{
    [FunctionName("SetNotificationConfig")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        try
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject<dynamic>(requestBody);
            var tem = data["NotificationConfig"];
            string notificationConfig = JsonConvert.SerializeObject(tem);
            SchedulerSupport.SetNotificationConfigJSON(1, notificationConfig);
        }
        catch (Exception ex)
        {
            return new BadRequestObjectResult("Error updating notification configuration: " + ex.Message);
        }
        return new OkResult();
    }
}

    public static void SetNotificationConfigJSON(int storeId, string notificationConfig)
    {
        if (notificationConfig.Length > 0)
        {
            using SqlConnection connection = new SqlConnection(SchedulerSupport.ConnectionStr());
            connection.Open();
            var queryString = "Update SchedulerConfig Set ConfigInfo = JSON_MODIFY(ConfigInfo, '$.NotificationConfig', JSON_QUERY(@notificationConfig)) Where StoreId = @StoreId";
            using SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                command.Parameters.Add(new SqlParameter("@StoreId", System.Data.SqlDbType.Int));
                command.Parameters["@StoreId"].Value = storeId;
                command.Parameters.AddWithValue("@NotificationConfig", notificationConfig);
                command.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题