gpt4 book ai didi

.net - HttpClient GetAsync 处置 Webjob 处理程序

转载 作者:行者123 更新时间:2023-12-02 08:04:53 26 4
gpt4 key购买 nike

我正在实现一个 Azure WebJob,当由服务总线消息触发时,它会对外部 Web 执行异步 API 调用服务。执行网络请求后,我的网络作业发生了一些奇怪的事情,并且在方法结束之前 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.

当我用非异步替代方案(例如使用 RestSharp)替换 Web 请求时,问题不会发生。您知道这个问题的根本原因是什么吗?

最佳答案

你几乎不应该使用async void除非你正在编写一个事件处理程序,你应该使用async Task来代替,这让系统知道你的webjob不是尚未完成,因此它不会在完成之前尝试强制关闭资源。

关于.net - HttpClient GetAsync 处置 Webjob 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46647237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com