使用 Rotativa 会导致 'Could not load file or assembly' 错误
Using Rotativa results in a 'Could not load file or assembly' error
我正在尝试在 MVC 应用程序中使用 Rotativa(使用 Visual Studio 下载 NuGet 包)将视图打印为 PDF,但每当调用该方法时,我都会收到 'Could not load file or assembly'错误。我试过在线寻找解决方案,但找不到合适的解决方案。以下是错误的完整堆栈跟踪:
Server Error in '/' Application.
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040)
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException: Could not load file or
assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
Source Error:
Line 48: "ViewOrder", Line 49: new {
id = id }) { FileName = "Order.pdf" }; Line 50: } Line 51:
Line 52: [HttpGet]
Source File:
c:\Users\André\Documents\WAD\Assignment1-10-2015\Controllers\OrdersController.cs
Line: 50
Assembly Load Trace: The following information can be helpful to
determine why the assembly 'System.Web.Mvc, Version=3.0.0.1,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.
=== Pre-bind state information === LOG: DisplayName = System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
(Fully-specified) LOG: Appbase =
file:///C:/Users/André/Documents/WAD/Assignment1/7-10-2015/ LOG:
Initial PrivatePath =
C:\Users\André\Documents\WAD\Assignment1-10-2015\bin Calling
assembly : Rotativa, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null.
=== LOG: This bind starts in default load context. LOG: Using application configuration file:
C:\Users\André\Documents\WAD\Assignment1-10-2015\web.config LOG:
Using host configuration file:
C:\Users\André\Documents\IISExpress\config\aspnet.config LOG: Using
machine configuration file from
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.Mvc, Version=3.0.0.1,
Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting
download of new URL
file:///C:/Users/André/AppData/Local/Temp/Temporary ASP.NET
Files/root/66e12f45/a5c0a521/System.Web.Mvc.DLL. LOG: Attempting
download of new URL
file:///C:/Users/André/AppData/Local/Temp/Temporary ASP.NET
Files/root/66e12f45/a5c0a521/System.Web.Mvc/System.Web.Mvc.DLL. LOG:
Attempting download of new URL
file:///C:/Users/André/Documents/WAD/Assignment1/7-10-2015/bin/System.Web.Mvc.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major
Version ERR: Failed to complete setup of assembly (hr = 0x80131040).
Probing terminated.
Stack Trace:
[FileLoadException: Could not load file or assembly 'System.Web.Mvc,
Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or
one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT:
0x80131040)]
_7_10_2015.Controllers.OrdersController.PrintOrder(Int32 id) in c:\Users\André\Documents\WAD\Assignment1-10-2015\Controllers\OrdersController.cs:50
lambda_method(Closure , ControllerBase , Object[] ) +161
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary2 parameters) +435<br>
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary
2
parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod()
+76 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult
asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult
asyncResult) +73<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c()
+117 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e()
+323 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +47<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult
asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass28.b__19()
+72 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult
asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult
asyncResult) +42<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
asyncResult) +40
System.Web.Mvc.Controller.b__1d(IAsyncResult
asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +70<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.b__15(IAsyncResult
asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +62<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +37 System.Web.Mvc.Controller.EndExecute(IAsyncResult
asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
asyncResult) +39
System.Web.Mvc.MvcHandler.b__4(IAsyncResult
asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +70<br>
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,
Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+9721605 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
以及我使用 Rotativa 的 ActionAsPDF 的代码示例:
[HttpGet]
public ActionResult ViewOrder(int id)
{
Order GetOrder = new CartBL().GetOrderById(id);
return View(GetOrder);
}
[HttpGet]
public ActionResult PrintOrder(int id)
{
return new ActionAsPdf(
"ViewOrder",
new { id = id }) { FileName = "Order.pdf" };
}
如果有人能帮我解决这个问题,我将不胜感激。
System.Web.Mvc, Version=3.0.0.1 未安装在您的系统上,或安装在工作进程无法访问的位置。
可能您需要做的是在您的解决方案中找到引用并将其标记为复制本地
还有其他方法可以解决此问题,为了完整起见,我将在此处概述。
您已登录融合绑定,这很好,但您没有使用它。它告诉您活页夹去哪里寻找程序集。去查看活页夹寻找程序集的每个位置。
它实际上在这些位置之一吗?那么使用应用程序池的帐户 运行 将无法访问 file/directory。这是一个权限问题——修复它。
它不在其中一个位置吗?然后它没有安装在该机器上。如果程序集是使用目标系统上的 msi/setup.exe、运行 安装的。如果不是,请参考 copy-local,正如我在此问题顶部所述。或者自己手动复制。
我猜,Rotativa 可能是使用 ASP.NET MVC 构建的,并引用特定版本(即 System.Web.Mvc 版本 - 3.0.0.1),并且您的 MVC 应用程序可能使用不同的System.Web.Mvc 的版本。尝试在项目的 web.config 中的 assemblyBinding
部分中添加类似于以下内容的程序集绑定。
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
这里,您可能需要将项目中的System.Web.Mvc版本替换为5.0.0.0。
我花了大约 1 个半小时后终于卸载了版本 1.7.3 并安装了 1.6.4 一切都按预期工作。
使用 NuGet 包管理器控制台命令提示符安装以前的版本:
安装包 Rotativa -版本 1.6.4
我正在尝试在 MVC 应用程序中使用 Rotativa(使用 Visual Studio 下载 NuGet 包)将视图打印为 PDF,但每当调用该方法时,我都会收到 'Could not load file or assembly'错误。我试过在线寻找解决方案,但找不到合适的解决方案。以下是错误的完整堆栈跟踪:
Server Error in '/' Application.
Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException: Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Source Error:
Line 48: "ViewOrder", Line 49: new { id = id }) { FileName = "Order.pdf" }; Line 50: } Line 51: Line 52: [HttpGet]
Source File: c:\Users\André\Documents\WAD\Assignment1-10-2015\Controllers\OrdersController.cs Line: 50
Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.
=== Pre-bind state information === LOG: DisplayName = System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///C:/Users/André/Documents/WAD/Assignment1/7-10-2015/ LOG: Initial PrivatePath = C:\Users\André\Documents\WAD\Assignment1-10-2015\bin Calling assembly : Rotativa, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\André\Documents\WAD\Assignment1-10-2015\web.config LOG: Using host configuration file: C:\Users\André\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///C:/Users/André/AppData/Local/Temp/Temporary ASP.NET Files/root/66e12f45/a5c0a521/System.Web.Mvc.DLL. LOG: Attempting download of new URL file:///C:/Users/André/AppData/Local/Temp/Temporary ASP.NET Files/root/66e12f45/a5c0a521/System.Web.Mvc/System.Web.Mvc.DLL. LOG: Attempting download of new URL file:///C:/Users/André/Documents/WAD/Assignment1/7-10-2015/bin/System.Web.Mvc.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Stack Trace:
[FileLoadException: Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
_7_10_2015.Controllers.OrdersController.PrintOrder(Int32 id) in c:\Users\André\Documents\WAD\Assignment1-10-2015\Controllers\OrdersController.cs:50 lambda_method(Closure , ControllerBase , Object[] ) +161
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +435<br> System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +73<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c() +117 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +323 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +47<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +72 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70<br> System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9721605 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
以及我使用 Rotativa 的 ActionAsPDF 的代码示例:
[HttpGet]
public ActionResult ViewOrder(int id)
{
Order GetOrder = new CartBL().GetOrderById(id);
return View(GetOrder);
}
[HttpGet]
public ActionResult PrintOrder(int id)
{
return new ActionAsPdf(
"ViewOrder",
new { id = id }) { FileName = "Order.pdf" };
}
如果有人能帮我解决这个问题,我将不胜感激。
System.Web.Mvc, Version=3.0.0.1 未安装在您的系统上,或安装在工作进程无法访问的位置。
可能您需要做的是在您的解决方案中找到引用并将其标记为复制本地
还有其他方法可以解决此问题,为了完整起见,我将在此处概述。
您已登录融合绑定,这很好,但您没有使用它。它告诉您活页夹去哪里寻找程序集。去查看活页夹寻找程序集的每个位置。
它实际上在这些位置之一吗?那么使用应用程序池的帐户 运行 将无法访问 file/directory。这是一个权限问题——修复它。
它不在其中一个位置吗?然后它没有安装在该机器上。如果程序集是使用目标系统上的 msi/setup.exe、运行 安装的。如果不是,请参考 copy-local,正如我在此问题顶部所述。或者自己手动复制。
我猜,Rotativa 可能是使用 ASP.NET MVC 构建的,并引用特定版本(即 System.Web.Mvc 版本 - 3.0.0.1),并且您的 MVC 应用程序可能使用不同的System.Web.Mvc 的版本。尝试在项目的 web.config 中的 assemblyBinding
部分中添加类似于以下内容的程序集绑定。
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
这里,您可能需要将项目中的System.Web.Mvc版本替换为5.0.0.0。
我花了大约 1 个半小时后终于卸载了版本 1.7.3 并安装了 1.6.4 一切都按预期工作。
使用 NuGet 包管理器控制台命令提示符安装以前的版本: 安装包 Rotativa -版本 1.6.4