Vb.net 异步函数MVC错误500
我试图理解异步方法的概念,因此我制作了一个返回视图的简单方法:Vb.net 异步函数MVC错误500,vb.net,asp.net-mvc-4,Vb.net,Asp.net Mvc 4,我试图理解异步方法的概念,因此我制作了一个返回视图的简单方法: Public Async Function getPerfil() As Threading.Tasks.Task(Of ActionResult) Dim objOrganizacion As comOrganizacion.comOrganizacion = New comOrganizacion.comOrganizacion Dim ds As DataSet = Await Task.Run
Public Async Function getPerfil() As Threading.Tasks.Task(Of ActionResult)
Dim objOrganizacion As comOrganizacion.comOrganizacion = New comOrganizacion.comOrganizacion
Dim ds As DataSet = Await Task.Run(objOrganizacion.GetUserPostulacion())
Return View()
End Function
如果我从函数
Dim ds As DataSet=wait Task.Run(objOrganizacion.getuserposition())
中删除这一行,那么该方法工作正常,我可以看到视图。如果不删除该行,我会得到:500内部服务器错误
以下是MSDN的异步编程指南:
这是一个大量的阅读,我可以说,根据经验,这是很难掌握的,因为我仍然试图完全掌握自己。但是,如果您使用MVC的脚手架,其中包含使用wait的控制器中的函数,这可能有助于理解
在调用wait[function]时,该函数返回一个任务。例如:控制器中的异步HttpPost函数包含行“wait db.saveChangesSync()”。如果转到saveChangesSync()的定义,它会这样说:
Public Overridable Function SaveChangesAsync() As System.Threading.Tasks.Task(Of Integer)
Member of System.Data.Entity.DbContext
Summary: Asynchronously saves all changes made in this context to the underlying database.
Return Values: A task that represents the asynchronous save operation. The task result contains the number of objects written to the underlying database.
它使用的保存操作是SaveChanges,这是SaveChanges的定义:
Public Overridable Function SaveChanges() As Integer
Member of System.Data.Entity.DbContext
Summary: Saves all changes made in this context to the underlying database.
Return Values: The number of objects written to the underlying database.
基本上,这意味着savechangessync向控制器函数承诺,一旦它拥有它,就返回它所需要的。这就像使用一个中间人。假设你需要一个数字……而不是告诉数字目标“这是我需要的”,然后自己等待它,你告诉中间人“这是我需要的”,然后他告诉数学家你需要什么,然后自己等待,这样你就可以在这期间自由地做其他事情
现在,在您的上下文中,您似乎想打这个电话:
Dim ds As DataSet = Await objOrganizacion.GetUserPostulacionAsync()
其中objOrganizacion.getUserPositionAsync()是一个异步函数,返回(数据集的)任务。它调用实际函数(getUserPositionAsync),该函数将返回所需的数据集
无论如何,我希望这能有所帮助