Hug*_*mps 5 c# entity-framework-core asp.net-core autodesk-forge asp.net-core-webapi
我正在创建一个ASP.NET Core WebApi,它使用Autodesk Model Derivative API将Revit文件转换为另一种文件格式。上传文件后,Autodesk API将在后台开始工作,可能需要几分钟才能完成其工作。
我想监视Autodesk API的状态,以了解转换是否已经完成并通知用户。我正在寻找监视作业状态的最佳方法,而无需“等待”并使请求挂起几分钟。
我试过只是异步运行任务而不等待结果。直到我想要在数据库上下文中更新一个值的时候,该方法才起作用,因为由于请求结束,该值已被删除。
我还研究了实现后台服务的几种方法,但是还没有找到一种清晰的方法。
public async Task<ActionResult<Response<JobResponse>>> UploadFile(string bucketKey, IFormFile file)
{
// ....
// File has been uploaded
Response<JobResponse> response
= await NetworkManager.PostAsync<JobResponse>(URI.Job.Url, JsonConvert.SerializeObject(jobData));
// The job has been created in the Autodesk API, so I create a record in my own database
var job = new Job(urn, file.FileName);
context.Jobs.Add(job);
await context.SaveChangesAsync();
// This method is what I want to do in the background
MonitorStatus(job);
return Respond(response);
}
private async Task MonitorStatus(Job job)
{
bool isDone = false;
while (!isDone)
{
isDone = await IsDone(job.Urn);
if (!isDone)
await Task.Delay(10000);
}
string guid = await new JobRepository(job).GetGuid();
// The line underneath throws an error because the context has been disposed
(await context.Jobs.FindAsync(job.Id)).Finish(guid);
await context.SaveChangesAsync();
// ...
// Notify the user
}
Run Code Online (Sandbox Code Playgroud)
Model Derivative API中的文件翻译可归结为两个主要端点:
如果您自己发出 HTTP 请求,则只需轮询清单即可,直到看到翻译已完成。
如果您使用Forge .NET SDK ,则可以使用Translate方法触发翻译,并使用GetManifest方法轮询结果。
| 归档时间: |
|
| 查看次数: |
273 次 |
| 最近记录: |