Chromatic Gecko Chromatic Gecko - 3 months ago 35
ASP.NET (C#) Question

Using Rotativa results in a 'Could not load file or assembly' error

I'm trying to use Rotativa in an MVC application (downloaded the NuGet package using Visual Studio) to print a view to PDF yet whenever the method is called I'm getting a 'Could not load file or assembly' error. I've tried looking for solutions online but I can't find an adequate one. Below is the full Stack Trace of the error:


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\7-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\7-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\7-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\7-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, IDictionary
2 parameters) +435

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.WrappedAsyncResult
2.CallEndDelegate(IAsyncResult
asyncResult) +73

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.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult
asyncResult) +47

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.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult
asyncResult) +42

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.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
asyncResult) +70

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.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
asyncResult) +62

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.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
asyncResult) +70

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


And the code sample where I used Rotativa's 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" };
}


I'd really appreciate it anyone could help me fix this.

Answer

I guess, Rotativa may be built using ASP.NET MVC, and referencing to a particular version (i.e., System.Web.Mvc version - 3.0.0.1), and your MVC application may be using a different version of System.Web.Mvc. Try adding assembly binding similar to following inside assemblyBinding section in web.config of your project.

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>

Here, you may need to replace 5.0.0.0 by the version of System.Web.Mvc in your project.