我有一个使用poll-async模式的Azure Logic应用程序,但是状态检查终结点从未被调用,即使通过PostMan调用也可以正常工作。 Azure中的Logic应用已配置为支持异步模式,并且起始终结点正在返回位置标头,其中包含状态检查终结点的正确路径。
这是示例代码
//State dictionary for sample - stores the state of the working thread
private static Dictionary<int, bool> runningTasks = new Dictionary<int, bool>();
/// <summary>
/// This is the method that starts the task running. It creates a new thread to complete the work on, and returns an ID which can be passed in to check the status of the job.
/// In a real world scenario your dictionary may contain the object you want to return when the work is done.
/// </summary>
/// <returns>HTTP Response with needed headers</returns>
[HttpPost]
[Route("startwork")]
public async Task<HttpResponseMessage> longrunningtask(int FileId)
{
runningTasks[FileId] = false; //Job isn't done yet
new Thread(() => ImportFileDataFromLogicAppRequest(FileId)).Start(); //Start the thread of work, but continue on before it completes
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
responseMessage.Headers.Add("location", String.Format("{0}://{1}/api/SocialNetworks/Facebook/status/{2}", Request.Scheme, Request.Host, FileId)); //Where the engine will poll to check status
responseMessage.Headers.Add("retry-after", "20"); //How many seconds it should wait (20 is default if not included)
return responseMessage;
}
/// <summary>
/// Method to check the status of the job. This is where the location header redirects to.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("status/{id}")]
//[Swashbuckle.Swagger.Annotations.SwaggerResponse(HttpStatusCode.BadRequest, "No job exists with the specified id")]
//[Swashbuckle.Swagger.Annotations.SwaggerResponse(HttpStatusCode.Accepted, "The job is still running")]
//[Swashbuckle.Swagger.Annotations.SwaggerResponse(HttpStatusCode.OK, "The job has completed")]
public HttpResponseMessage checkStatus(int id)
{
//If the job is complete
if (runningTasks.ContainsKey(id) && runningTasks[id])
{
runningTasks.Remove(id);
return new HttpResponseMessage(HttpStatusCode.OK)
{ ReasonPhrase = "Some data could be returned here" };
}
//If the job is still running
else if (runningTasks.ContainsKey(id))
{
HttpResponseMessage responseMessage =
new HttpResponseMessage(HttpStatusCode.Accepted);
responseMessage.Headers.Add("location", String.Format("{0}://{1}/api/SocialNetworks/Facebook/status/{2}",
Request.Scheme, Request.Host, id)); //Where the engine will poll to check status
responseMessage.Headers.Add("retry-after", "20");
return responseMessage;
}
else
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "No job exists with the specified ID"
};
}
}
/// <summary>
/// Imports file data from a request comming from a Logic App
/// </summary>
/// <param name="FileId"></param>
/// <returns></returns>
//[HttpPost]
//[Route("ImportFileDataFromLogicAppRequest")]
public async Task ImportFileDataFromLogicAppRequest(int FileId)
{
bool stop = false;
while (!stop)
{
await Task.Delay(50000);
}
runningTasks[FileId] = true;
}
目前,长时间运行的方法仅出于调试目的而具有无限循环,尽管即使没有,也永远不会调用状态检查端点。