我想在Azure Batch中执行一个简单的任务,等待它完成并获得结果:
using (var client = _CreateBatchClient())
{
var monitor = client.Utilities.CreateTaskStateMonitor();
var task = new CloudTask(Guid.NewGuid().ToString(), "echo hello world");
await client.JobOperations.AddTaskAsync("Test", task);
await monitor.WhenAll(new List<CloudTask> { task }, TaskState.Completed, _timeout);
var result = task.ExecutionInformation.Result;
}而WhenAsync行抛出了System.InvalidOperationException: 'This operation is forbidden on unbound objects.'
这条消息相当晦涩,而我离tutorial不远。怎么啦?
发布于 2019-06-11 20:58:49
这段代码并不明显,但实际上Azure Batch并不知道如何在这里识别任务。作业包含任务,但任务没有对其运行的作业的引用。并且任务Id也不是全局标识任务,它只需要在作业中是唯一的。
这可能就是这里“未绑定对象”的含义。监视器就是不知道要看什么。实际上,如果注释了WhenAsync行,下一行将抛出类似的InvalidOperationException: 'The property ExecutionInformation cannot be read while the object is in the Unbound state.'
因此,正确的方法是通过作业引用任务:
using (var client = _CreateBatchClient())
{
var monitor = client.Utilities.CreateTaskStateMonitor();
var id = Guid.NewGuid().ToString();
var taskToAdd = new CloudTask(id, "echo hello world");
await client.JobOperations.AddTaskAsync("Test", taskToAdd);
var taskToTrack = await client.JobOperations.GetTaskAsync("Test", id);
await monitor.WhenAll(new List<CloudTask> { taskToTrack }, TaskState.Completed, _timeout);
}比较:


要获得结果信息,需要再次“查找”作业中的任务,否则将为空。
https://stackoverflow.com/questions/56544319
复制相似问题