C# ASHX处理程序中的异步Post

C# ASHX处理程序中的异步Post,c#,asp.net,asynchronous,webclient,httphandler,C#,Asp.net,Asynchronous,Webclient,Httphandler,我和ASHX handler有问题。以下代码在本地计算机上正常工作,但在服务器上不工作 public void ProcessRequest(HttpContext context) { ...... More code // convert class to namevaluecollection NameValueCollection formFields = payloadData.ToNameValueCollection(); using (var c

我和ASHX handler有问题。以下代码在本地计算机上正常工作,但在服务器上不工作

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.
异常类型:InvalidOperationException
异常消息:此时无法启动异步操作。
异步操作只能在异步处理程序或模块内启动,或在页面生命周期中的某些事件期间启动。
如果在执行页面时发生此异常,请确保已标记该页面。
此异常还可能表示有人试图调用“async void”方法,这在ASP.NET请求处理中通常不受支持。
相反,异步方法应该返回一个任务,调用方应该等待它。
编辑:


我发现可能是版本对代码库的影响。有人能确认UploadValuesAsync在framework 4.5和4.6上的行为不同吗?让您的处理程序实现IHTTPassynchandler。

让您的处理程序实现IHTTPassynchandler。

这将异步发布:

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

这将以异步方式发布:

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

更新

经过进一步调查,我发现服务器上存在导致错误的配置。这个问题也很有帮助

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />


提到的键在machine.config中的默认值为False,在web.config中设置为true

更新

经过进一步调查,我发现服务器上存在导致错误的配置。这个问题也很有帮助

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />


提到的键在machine.config中的默认值为False,在web.config中设置为true

我的Http处理程序不是异步处理程序,但我需要将值异步发布到另一台服务器,以防止任何线程锁定。我想发布数据并忘记它。我的Http处理程序不是异步处理程序,但我需要将值异步发布到另一台服务器,以防止任何线程锁定。我想发布数据,然后忘掉它。