我正在实施 Azure WebJob ,当由服务总线消息触发时,会执行异步API调用外部Web服务。执行Web请求后,我的webjob发生了一些奇怪的事情,并且在方法结束之前 inputMessage已经处理。 下面是一个演示问题+输出的基本示例。
public async void HandleTestQueueMessage([ServiceBusTrigger("%TestQueue%")] BrokeredMessage inputMessage) {
Console.WriteLine($"Received message {inputMessage.MessageId}.");
Console.WriteLine("Starting web request.");
await TestHttpClientAsync();
Console.WriteLine("Finished web request.");
await inputMessage.CompleteAsync(); // --> Exception occurs here
Console.WriteLine($"Completed message {inputMessage.MessageId}.");
}
private async Task TestHttpClientAsync() {
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://google.com");
Console.WriteLine($"Got response, statuscode={response.StatusCode}.");
}
控制台输出:
Executing: 'ProblemTestHandler.HandleTestQueueMessage' - Reason: 'New ServiceBus message detected on 'TestQueue'.'
Received message a17cc4fce8c74b3c9f1b8f1a73b4ba1b.
Starting web request.
Executed: 'ProblemTestHandler.HandleTestQueueMessage' (Succeeded)
Got response, statuscode=OK.
Finished web request.
Unhandled Exception: System.ObjectDisposedException: BrokeredMessage has been disposed.
at Microsoft.ServiceBus.Messaging.BrokeredMessage.ThrowIfDisposed()
at Microsoft.ServiceBus.Messaging.BrokeredMessage.BeginComplete(AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions)
at Microsoft.ServiceBus.Common.Parallel.TaskHelpers.CreateTask(Func`3 begin, Action`1 end, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Wkb.eInvoicing.Worker.Handlers.ProblemTestHandler.<HandleTestQueueMessage>d__1.MoveNext() in C:\Dev\eInvoicing\src\Wkb.eInvoicing.Worker\Handlers\ProblemTestHandler.cs:line 43
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
正如您在输出中看到的那样,我的JobHost容器认为该方法在执行 httpClient.GetAsync()方法后立即成功完成:
Starting web request.
Executed: 'ProblemTestHandler.HandleTestQueueMessage' (Succeeded)
Got response, statuscode=OK.
当我用非异步替代方法替换Web请求时(例如使用RestSharp),问题不会发生。任何想法可能是这个问题的根本原因?
答案 0 :(得分:3)
你几乎不应该使用async void
,除非你正在编写一个事件处理程序,你应该使用async Task
代替,这让系统知道你的webjob还没有完成所以它不会尝试在资源完成之前强制关闭资源。