繁体   English   中英

HttpClient.PostAsync continueWith 不执行

[英]HttpClient.PostAsync continueWith not executing

我需要一些帮助来弄清楚为什么 continueWith 块中的以下代码没有在长时间运行的服务调用中执行。

     public static async void postServiceAsync(string json, string postServiceUrl, string callbackUrl, string clientId, 
                                                  string tenant, string secret, string d365Environment, TraceWriter log)
        {
            HttpClient client = new HttpClient();

            //Get authorization header
            string authHeader = await D365Authorization.getAccessToken(clientId, tenant, secret, d365Environment);
            client.DefaultRequestHeaders.Add("Authorization", authHeader);
            var httpContent = new StringContent(json);

            client.Timeout = TimeSpan.FromMinutes(90);
            client.PostAsync(postServiceUrl, httpContent).ContinueWith(async (result) =>
            {
               //call callback URL
               //This is not executed after a long running service that runs for 20 minutes.
             }
         }

如果服务执行时间很短,则 continueWith 代码确实会运行。 我认为这是一个超时问题,所以我添加了 client.Timeout 值。 我尝试在 Postman 中调用该服务,即使等待 20 多分钟后仍返回一个值。 我没有使用 await,因为我希望在调用 PostAsync 后继续执行。 我只想在长时间运行的服务执行完成后执行 continueWith 回调。 谢谢你的帮助!

上述称为 postServiceAsync 的方法是从 Azure 函数调用的,该函数正从 Azure 逻辑应用 http webhook 操作调用。 这是 Azure 函数:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            ...

            PostServiceAsync.postServiceAsync(json, shipServiceUrl, callbackUrl, clientId, tenant, secret, d365Environment, log);

            var resp = req.CreateResponse(HttpStatusCode.Accepted);
            return resp;
        }
    }

在 Azure 函数中,我需要立即返回 Accepted 状态代码。 在我使用 PostAsync 调用长时间运行的服务之后,我需要发布到回调 URL,这就是我在 continueWith 块中所做的。 就像我提到的,如果服务运行时间很短,它就可以工作。 我尝试了卡米洛关于添加 await 的建议,但是 continueWith 代码没有被执行。 我也尝试摆脱 continueWith 并在“await client.PostAsync(...)”之后添加代码。

事实证明,在没有响应的情况下,http 调用有一个 Azure 函数 230 秒超时。 我可能无法将 Azure 函数用于我的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM