在webjob中使用等待异步调用时的WindowsAzure.Storage.StorageException

Dav*_*Wei 5 queue asynchronous azure wait

我在azure网站上使用webjob来处理占用大量内存的繁重进程.该进程后端执行大量Web调用,因此将其设计为异步.我希望webjob只处理每个实例的一个请求.我已经设置了Queues.BatchSize = 1选项来禁用webjob中的并行.但是,当我使用await来调用异步方法时,下一条消息仍然会立即从队列中获取,因为await并没有真正阻止当前线程,这对UI应用程序很有用,但不适用于我的情况.

所以我尝试使用wait来阻止当前线程,直到所有后端进程完成.但是,在该过程完成后抛出异常.似乎webjob在等待后删除队列存在问题.

我附上了简化的测试代码和例外.有任何想法吗?

非常感谢您的帮助!

名称空间ProcessWebJob {

public class Functions
{
    public static void ProcessQueueMessage([QueueTrigger("testQueue")] string message, TextWriter log)
    {
        try
        {
            testAsync ts = new testAsync();
            var task = Task.Run(async () => { await ts.test(); });
            task.Wait();
        }catch(Exception e){
           Trace.TraceInformation("error on processing the queue message" + e.InnerException);
        } 

    }
}

public class testAsync {
   public async Task<int> test()  
   {
        await Task.Delay(10000); //10 seconds delay
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

[10/07/2015 17:57:14> 278796:错误]未处理的异常:Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(404)Not Found.---> System.Net.WebException:远程服务器返回错误:(404)Not Found.[10/07/2015 17:57:14> 278796:ERR]在Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException [T](HttpStatusCode expectedStatusCode,HttpStatusCode actualStatusCode,T retVal,StorageCommandBase1 cmd, Exception ex) [10/07/2015 17:57:14 > 278796: ERR ] at Microsoft.WindowsAzure.Storage.Blob.CloudBlob.<DeleteBlobImpl>b__29(RESTCommand1个CMD,HttpWebResponse RESP,异常前,的OperationContext CTX)[2015年10月7日17时57分14秒> 278796:ERR]在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse [T](IAsyncResult的getResponseResult) 10/07/2015 17:57:14> 278796:错误] --- Microsoft.WindowsAzure.Storage内部异常堆栈跟踪结束--- [10/07/2015 17:57:14> 278796:ERR]. Core.Executor.Executor.EndExecuteAsync [T](IAsyncResult的结果).[2015年10月7日17点57分14秒> 278796:ERR]在Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions <> c__DisplayClass4.b__3(IAsyncResult的ar)[10/07/2015 17:57:14> 278796:ERR] ---从抛出异常的上一个位置开始的堆栈跟踪--- [10/07/2015 17:57:14> 278796:ERR ]在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)[10/07/2015 17:57:14>278796:ERR]在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)[2015年10月7日17时57分十四秒> 278796:ERR]在Microsoft.Azure.WebJobs.Host.Protocols.PersistentQueueWriter1.<DeleteAsync>d__6.MoveNext() [10/07/2015 17:57:14 > 278796: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at Microsoft.Azure.WebJobs.Host.Loggers.CompositeFunctionInstanceLogger.<DeleteLogFunctionStartedAsync>d__e.MoveNext() [10/07/2015 17:57:14 > 278796: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<TryExecuteAsync>d__1.MoveNext() [10/07/2015 17:57:14 > 278796: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at Microsoft.Azure.WebJobs.Host.Queues.Listeners.QueueTriggerExecutor.<ExecuteAsync>d__0.MoveNext() [10/07/2015 17:57:14 > 278796: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/07/2015 17:57:14 > 278796: ERR ] at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()[2015年10月7日十七时57分14秒> 278796:ERR]在Microsoft.Azure.WebJobs.Host.Queues.Listeners.QueueListener.d__11.MoveNext()[2015年10月7日17点57分:14> 278796:ERR] ---从抛出异常的上一个位置开始的堆栈跟踪--- [10/07/2015 17:57:14> 278796:ERR]在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()[2015年10月7日17时57分14秒> 278796:ERR]在System.Threading.ExecutionContext.RunInternal(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)[2015年10月7日17时57分14秒> 278796:ERR]在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)[2015年10月7日17时57分14秒> 278796:ERR]在System.Threading.ExecutionContext.Run (ExecutionContext executionContext,在System.Threading.ThreadHelper.ThreadStart()[10/07/2015 17:57:14> 278796:SYS ERR]作业中的ContextCallback回调,对象状态)[10/07/2015 17:57:14> 278796:ERR]因退出代码-532462766而失败