2014-10-02 63 views
2

我正在玩ASP.NET MVC5和RavenDB。我想使用Raven的异步API,但在我的生活中无法使其工作。异步控制器方法显示任务的类型名称<ActionResult>

我控制器(实际的查询结果是伪造的,因为在数据库中没有数据)

public class BooksController : Controller 
{ 
    public async Task<ActionResult> IndexAsync() 
    { 
    var books = this.documentSession.Query<Book>(); 
    Book book = await books.FirstOrDefaultAsync() ?? 
     new Book { Title = "Programming WCF service", ASIN = "B0043D2DUK" }; 
    return this.View(book); 
    } 
} 

和视图IndexAsync.cshtml

@using Hydra.FubuConventions 
@model Hydra.Models.Book 
@{ 
    ViewBag.Title = "Books Asynchronous"; 
} 
<h2>Books Asynchronous</h2> 
@Html.FormBlock(x => x.Title) 
@Html.FormBlock(x => x.ASIN) 

在Web.config

<configuration> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <customErrors mode="On" defaultRedirect="Error.html"/> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
</configuration> 

当我使用同步控制器方法为模型提供数据时,它工作得很好。但与异步方法它只显示

System.Threading.Tasks.Task 1 System.Web.Mvc.ActionResult]`

我找到了很多在这里计算器职位,表明MVC的旧版本可能是问题的。但我检查了我的输出文件夹中的MVC dll(System.Web.Helpers v3.0.11001.0,System.Web.Mvc v5.0.11001.0和System.Web.WebPages v3.0.11001.0)的版本,似乎是正确的。

任何想法我失踪?


UPDATE

我比较加载的程序集的列表。除了仅在其中一个项目中加载的程序集明显不同之外,它们是匹配的。 web.config是相同的。我用与新项目中使用的相同的测试存根替换了对RavenDB的调用。仍然获取类型名称而不是视图。

+0

您是否从早期的MVC版本迁移过项目?创建一个新项目并添加一个脚手架异步控制器,以确保异步控制器正常工作。如果他们不这样做,你会从开发环境中丢失软件包,但我怀疑它。您可能在'web中配置了错误的版本。config' – 2014-10-02 14:52:03

+0

@PanagiotisKanavos空项目按预期工作...... – 2014-10-02 15:18:20

+0

@SebastianWeber:既然你有一个解决方案可以工作,一个没有,那么它只能找到两者之间的区别。 – 2014-10-03 00:39:26

回答

3

如果您使用自定义ControllerActionInvoker,则必须从AsyncControllerActionInvoker继承它。

1

必须以您的web.config为目标.NET 4.5 集合httpRuntime.targetFramework4.5

+0

项目的目标框架设置为.NET Framework 4.5(在项目属性中)。并且如上面web.config中所示,httpRuntime.targetFramework也设置为4.5 – 2014-10-02 21:58:30

0

您的web.config缺少在新的MVC项目中发现的程序集重定向指令。我怀疑旧的MVC程序集已安装在GAC中,并由您的应用程序提取。

在任何情况下,行为都是典型的版本不匹配。您需要通过比较项目和新的干净的MVC 5项目之间的差异来找出为什么旧的程序集会加载。

在一个新的MVC 5.2项目,在web配置的<runtime>部分包含这些组件重定向:

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
    </dependentAssembly> 
    </assemblyBinding> 
</runtime> 

您应该重定向从清洁项目复制到您的旧项目,看看这是否解决了问题

+0

我非常感谢你的帮助。但重定向没有解决问题。我仍然在MVC 5.0与VS2012顺便说一句。 – 2014-10-03 10:03:06

+0

干净的项目证明你正在加载错误的MVC版本。我发布的具体版本号为5.2。你使用了干净项目中的重定向吗?你是否也删除了项目文件夹中的任何MVC dll?您可能在某处复制了旧版本并忘记了它。清洁'bin'文件夹。你不需要任何DLL,当你建立你的项目时它会被重新创建。 – 2014-10-03 10:11:36

+0

干净的项目没有任何重定向。我更新了我的“问题项目”(添加了大量重定向)中的所有nuget软件包,但这也不起作用。 – 2014-10-03 10:15:08