【发布时间】:2019-03-12 00:32:46
【问题描述】:
我需要一些帮助来弄清楚为什么 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 多分钟后也会返回一个值。我没有使用等待,因为我希望在调用 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 块中所做的事情。就像我提到的,如果服务运行时间很短,它就可以工作。我尝试了 Camilo 的添加 await 的建议,但 continueWith 代码没有被执行。我还尝试摆脱 continueWith 并在“await client.PostAsync(...)”之后添加代码。
【问题讨论】:
-
如果您的方法是
async,为什么还要使用ContinueWith?这完全没有意义。await调用PostAsync然后做你在ContinueWith调用中所做的任何事情 -
嗨,卡米洛。我尝试添加等待,但没有奏效。我在上面的帖子中添加了更多背景信息。
标签: c# asynchronous httpclient azure-functions azure-logic-apps