我遇到了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上的行为是否不同吗?