繁体   English   中英

ASHX处理程序中的异步发布

[英]Async Post in ASHX Handler

我在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());

更新资料

经过更多调查后,我发现服务器上存在导致该错误的配置。 另外,“ UseTaskFriendlySynchronizationContext”是什么意思? 这个问题很有帮助。

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

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

让您的处理程序实现IHttpAsyncHandler。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM