This is a migrated thread and some comments may be shown as answers.

MVC/HTML5 Report Viewer Instructions Unclear (R2 2016)

1 Answer 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Tim Mc
Top achievements
Rank 1
Tim Mc asked on 18 Aug 2016, 10:00 PM

After spending several days trying to get the Report Viewer working in an existing MVC5 application, I thought I would layout here the "real" steps you need to do to get this working in a project, including any caveats NOT detailed in Telerik's online resources.  These suggestions are based on all the errors you will get once you've added the "Telerik MVC Report Viewer View R2 2016" item template as suggested in the online forums and documentation.

1.) Startup / Global.asax order of operations IS IMPORTANT!

This comes into play especially for large existing projects which may have a lot of altered startup code in the Global.asax.  You may be tempted to place the Telerik Reporting configuration (Telerik.Reporting.Services.WebApi.ReportsControllerConfiguration.RegisterRoutes(System.Web.Http.GlobalConfiguration.Configuration)) at the end of your "Application_Startup" code.  However, it needs to be placed JUST right. 

In most existing apps there may already be setup code for the WebApi routes (specifically "GlobalConfiguration.Configure(YourRegisterWebApiCode)").  If the call to your "GlobalConfiguration.Configure(...)" method comes BEFORE the Telerik reporting registration, you will NEVER be able to browse your reports and will have all kinds of issues, nor will you be able to even browse to "/api/reports/formats".  Although not mentioned anywhere in the documentation, this is because the default routes setup for the WebApi already have a "Default" route which will get hit before even trying to resolve the Telerik ones (essentially overriding the Telerik ones).  Once you register the Telerik reporting BEFORE your calls to register your custom routes "GlobalConfiguration.Configure(...)", everything is just fine!

2.) You get exceptions about "Telerik.Reporting.OpenXmlRendering.dll".
The report may still render, however, you run across this exception:
An exception of type 'System.TypeInitializationException' occurred in Telerik.Reporting.OpenXmlRendering.dll and wasn't handled before a managed/native boundary.

Upon further inspection of the Inner Exceptions, you may see:
Could not load file or assembly 'DocumentFormat.OpenXml' or one of its dependencies.
The system cannot find the file specified.":"DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

Adding a nuget reference to Microsoft's "DocumentFormat.OpenXml" will most likely fix this part, but there are more exceptions to fix yet...

3.) "InvalidCastException" occured:

After fixing the issue from #2, you may run across this exception:
An exception of type 'System.InvalidCastException' occurred in Telerik.Reporting.dll and wasn't handled before a managed/native boundary
Additional information: Specified cast is not valid.

Additional information: The type initializer for 'Telerik.Reporting.OpenXmlRendering.Wordprocessing.WordprocessingReport' threw an exception.
Additional information: The type initializer for 'Telerik.Reporting.OpenXmlRendering.Presentation.PresentationReport' threw an exception.
Additional information: The type initializer for 'Telerik.Reporting.OpenXmlRendering.Spreadsheet.SpreadsheetReport' threw an exception.

Oh so helpful! But what you may need to do (especially since the Telerik dlls are not on the current "DocumentFormat.OpenXml" version) is simply add this xml section into your web.config under the "runtime/assemblyBinding" section (of course, once you've followed #2 above):

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<dependentAssembly>
    <assemblyIdentity name="DocumentFormat.OpenXml" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.5.65535.65535" newVersion="2.5.5631.0" />
</dependentAssembly>

 

And viola!  Now your reports should run without errors!

1 Answer, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 19 Aug 2016, 02:06 PM
Hello Tim,

Thank you for this feedback.

In addition:
  1. Depending on the VS project template, custom controllers and their routes, please consider modifying the default routes registered for the Reporting REST service - How to change the registered by default Telerik Reporting REST Web API routes. This will reduce the chance of having duplicating routes.
  2. By default OpenXML formats (DOCX, XLSX, PPTX) are not added in the viewer's export options. You need to add references to both Telerik.Reporting.OpenXmlRendering.dll and Open XML SDK 2.0 for Microsoft Office(DocumentFormat.OpenXml.dll v.2.0.5022.0 or or above with proper binding redirect).

I hope this helps.

Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Tim Mc
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or