MVC4-异步操作不能在这个时候开始--- code正常工作的MVC3 [英] MVC4- An asynchronous operation cannot be started at this time --- code works fine on MVC3

查看:508
本文介绍了MVC4-异步操作不能在这个时候开始--- code正常工作的MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换一个项目从MVC3到MVC4,也从实体框架5至6.1 EF。在code有一个具有code部分使用Html.BeginForm ....使用终结一VBHTML页。在该页面中,用户可以选择一个文件,然后点击提交。这要求在控制器POST方法。在控制器的方法上传选择的文件使用的GData API的可恢复上传(aynchronous)谷歌驱动器。 ResumableUploader必须使用(上传PDF和大文件的GData API限制)。现在,这个code过去一直供职于MVC3。只要平台改为MVC4,开始得到此异常:

这是异步操作不能在这个时间开始。异步操作只能在异步处理程序或模块内或在在页面生命周期的某些事件开始。如果在执行一个页面出现了这种异常,保证页面被标记为LT;%@页面异步=真正的%>

 在System.Web.AspNetSynchronizationContext.OperationStarted()在Google.GData.Client.ResumableUpload.ResumableUploader.AsyncStarter
(AsyncResumableUploadData数据,WorkerResumableUploadHandler workerDelegate,
对象用户数据)

我没把@Page异步=真的VBHTML页面,但是这并没有帮助。我100%肯定这是关系到MVC4和/或.Net 4.5,因为没有其他变化在code(比移动到MVC4并移动到其他.Net4.5)已经完成。我现在有2 code分支,一个在MVC3,一个在MVC4。当我编译MVC3,并在应用程序复制输出DLL,上述问题不会浮出水面。当我取代MVC4版本的DLL,上述问题的出现。如何处理?

这是code导致此问题:

 昏暗的权威性作为身份验证=新AuthSubAuthenticator(即为MyToken,authFactory.Token)
昏暗上传作为新ResumableUploader(10485760)
AddHandler的uploader.AsyncOperationCompleted,AddressOf UploaderCompleted
uploader.InsertAsync(AUTH,文件,新的对象())

跟踪显示异常是从最后一行(uploader.InsertAsync)抛出


我知道有,我可以用一个await方法。没有尝试,而是改为 uploader.InsertAsync uploader.Insert 和code工程确定。但是,但用户必须等待,直到上载完成(对于更大的文件,相当长一段时间)。


解决方案

  

我100%肯定这是关系到MVC4和/或.Net 4.5


是的,这是ASP.NET 4.5的变化。然而,值得注意的是,code是以前技术上的错误是很重要的。 ASP.NET 4.5增加了一些保障措施赶上不当使用异步。因此,MVC3 code实际上是不正确的;它只是没有被抓到并报告本身。


  

改变uploader.InsertAsync到uploader.Insert和code工程确定。但是,但用户必须等待,直到上载完成(对于更大的文件,相当长一段时间)。


这是正确的方式做到这一点对ASP.NET(在一个侧面说明,使用等待将会比更高效的插入)。 ASP.NET的目的不是做工作,而用户的连接。

考虑,例如,会发生什么,如果上传错误的;有没有办法来通知,其实上传没有完成的用户。对于这个问题,有没有办法来通知用户上传的没有的完成。此外,ASP.NET可以回收您的应用程序,它可以杀死一个正在进行上传。由于这些原因,在做射后不理建议不要在ASP.NET的工作。

不过,如果你愿意忍受这些限制,我在我的博客描述了各种不同的方式做的射后不理的ASP.NET 。需要注意的是ASP.NET 4.5.2增加了一个内置的方式做到这一点: HostingEnvironment.QueueBackgroundWorkItem

I converted a project from MVC3 to MVC4 and also from Entity Framework 5 to EF 6.1. In the code there is a VBHTML page that has a code section "Using Html.BeginForm.... end using". in this page, a user can select a file and click submit. That calls a POST method in a controller. The method in the controller uploads the selected file to Google drive using resumable uploader (aynchronous) of GData API. ResumableUploader has to be used (GData API restrictions for uploading PDF and big files). Now, this code has always worked in the past in MVC3. As soon as the platform was changed to MVC4, started getting this exception:

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" %>

at System.Web.AspNetSynchronizationContext.OperationStarted() at Google.GData.Client.ResumableUpload.ResumableUploader.AsyncStarter
(AsyncResumableUploadData data, WorkerResumableUploadHandler workerDelegate, 
Object userData)

I did put @Page Async="true" in the VBHTML page, but that did not help. I am 100% sure this is related to MVC4 and/or .Net 4.5 because no other change has been done in the code (other than moving to MVC4 and moving to .Net4.5). I have two code branches now, one on MVC3 and one on MVC4. When I compile MVC3 and copy the output DLL in the app, the above issue does not surface. When I replace that DLL by MVC4 version, the above issue comes up. How to handle this?

This is the code that causes this issue:

Dim auth As Authenticator = New AuthSubAuthenticator("MyToken", authFactory.Token)
Dim uploader As New ResumableUploader(10485760)
AddHandler uploader.AsyncOperationCompleted, AddressOf UploaderCompleted
uploader.InsertAsync(auth, file, New Object())

The trace shows that the exception was thrown from the last line (uploader.InsertAsync)


I understand there is a await method that I could use. Did not try, instead, changed uploader.InsertAsync to uploader.Insert and the code works ok. But but the user has to wait until the upload is complete (for bigger files, for quite a while).

解决方案

I am 100% sure this is related to MVC4 and/or .Net 4.5

Yes, this is a change in ASP.NET 4.5. However, it's important to note that the code was technically wrong before. ASP.NET 4.5 adds several safeguards to catch improper asynchronous usage. So, the MVC3 code was actually improper; it just wasn't getting caught and reported as such.

changed uploader.InsertAsync to uploader.Insert and the code works ok. But but the user has to wait until the upload is complete (for bigger files, for quite a while).

This is the "proper" way to do it on ASP.NET (on a side note, using await would be more efficient than Insert). ASP.NET is not designed to do work without a user connection.

Consider, for example, what would happen if the upload errors out; there's no way to notify the user that the upload in fact did not complete. For that matter, there's no way to notify the user that the upload did complete. Also, ASP.NET may recycle your application, which can kill an in-progress upload. For these reasons, doing "fire and forget" work is not recommended on ASP.NET.

However, if you're willing to live with those limitations, I describe on my blog a variety of ways to do fire and forget on ASP.NET. Note that ASP.NET 4.5.2 added a built-in way to do this: HostingEnvironment.QueueBackgroundWorkItem.

这篇关于MVC4-异步操作不能在这个时候开始--- code正常工作的MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆