gpt4 book ai didi

c# - ASHX 处理程序中的异步发布

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:31 27 4
gpt4 key购买 nike

我在使用 ASHX 处理程序时遇到问题。下面这段代码在本地机器上工作正常,但在服务器上不工作。

public void ProcessRequest(HttpContext context)
{
...... More code
// convert class to namevaluecollection
NameValueCollection formFields = payloadData.ToNameValueCollection();

using (var client = new WebClient())
{
//client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.UploadValuesAsync(new Uri("http://server/accept"), "POST", formFields);
}
}

我收到以下错误:

Exception type: InvalidOperationException 
Exception message: An asynchronous operation cannot be started at this time.
Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle.
If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.
This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing.
Instead, the asynchronous method should return a Task, and the caller should await it.

编辑:

我发现的一件事可能是版本对代码库的影响。有人可以确认 UploadValuesAsync 在框架 4.5 和 4.6 上的行为不同

最佳答案

这将异步发布:

Task.Run(() => PostAsync());

关于c# - ASHX 处理程序中的异步发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584730/

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