HttpClient.PostAsync continueWith不执行

时间:2019-03-12 00:32:47

标签: c# asynchronous httpclient azure-functions azure-logic-apps

我需要一些帮助来弄清楚为什么长时间运行的服务调用未执行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函数是从Azure Logic App 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函数中,我需要立即返回“接受”状态代码。使用PostAsync调用长期运行的服务后,我需要发布到回调URL,这是我在continueWith块中所做的事情。就像我提到的,如果服务运行时间很短,它就可以工作。我尝试了Camilo的添加await的建议,但是continueWith代码没有得到执行。我还尝试摆脱了continueWith,并在“ await client.PostAsync(...)”之后添加了代码。

0 个答案:

没有答案