ReportViewerCore.NETCore运行rdlc报表报BC30002错误问题咨询
.NET 6 环境下LocalReport.Render生成PDF报BC30002类型未定义问题
环境信息
- 开发工具:Visual Studio 2022(v17.2.4)
- 目标框架:.NET 6.0
- 报表组件:
Microsoft.Reporting.WinForms.LocalReport,曾替换为Microsoft.Reporting.NETCore.LocalReport验证,问题复现 - 部署方式:rdlc报表作为嵌入资源存放在类库项目中,类库以插件形式部署在主程序的子目录下,未放置在可执行文件根目录
现象说明
- VS调试模式下报表生成功能完全正常
- 直接运行编译产出的exe、或将debug目录拷贝到测试机运行时,执行
byte[] bytes = Report.Render("PDF")代码行抛出异常 - 已排查项:报表名称配置正确、已安装所有相关依赖包、排除rdlc文件格式错误(调试模式可正常运行可佐证)
异常堆栈
22/06/2022 10:42:10 - Error saving report JobInfo: Microsoft.Reporting.WinForms.LocalProcessingException: An error occurred during local report processing. ---> Microsoft.Reporting.DefinitionInvalidException: The definition of the report '' is invalid. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: An unexpected error occurred in Report Processing. ---> System.InvalidOperationException: (20) : error BC30002: Type 'System.Reflection.AssemblyVersion' is not defined. --- End of inner exception stack trace --- at Microsoft.Reporting.WinForms.LocalReport.EnsureExecutionSession() at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, CreateAndRegisterStream createStreamCallback, Warning[]& warnings) at Microsoft.Reporting.WinForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings) at Microsoft.Reporting.WinForms.LocalReport.Render(String format, String deviceInfo, PageCountMode pageCountMode, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings) at Microsoft.Reporting.WinForms.Report.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings) at Microsoft.Reporting.WinForms.Report.Render(String format, String deviceInfo) at Microsoft.Reporting.WinForms.Report.Render(String format) at Sales.Demo.ManageReport.SaveReport(DataTable ReportData, String reportName, String reportResourceFilePath, String reportFilePath)
根因
RDLC报表渲染时会动态使用VB编译器编译报表内的表达式,该过程需要加载引用的系统程序集。VS调试模式下会自动继承全局程序集解析路径,可以正常找到System.Reflection等核心类型;脱离VS独立运行时,尤其是插件部署在子目录的场景,报表引擎的默认程序集搜索路径不包含.NET 6运行时核心程序集位置,也不会主动扫描子目录下的依赖,导致编译时报System.Reflection.AssemblyVersion类型未定义的错误。
修复步骤
- 升级
Microsoft.ReportingServices.ReportViewerControl.WinformsNuGet包到150.1586.0及以上版本,旧版本对.NET 6的兼容性存在已知缺陷。 - 初始化LocalReport实例时,手动向报表引擎注册需要引用的核心运行时程序集,示例代码如下:
using System.Reflection; using Microsoft.Reporting.WinForms; var report = new LocalReport(); // 保留原有加载嵌入rdlc资源的逻辑 // report.ReportEmbeddedResource = "YourProject.YourReportFile.rdlc"; // 手动添加核心程序集信任引用,解决动态编译找不到类型的问题 report.AddTrustedCodeModule(typeof(object).Assembly.Location); report.AddTrustedCodeModule(Assembly.Load("System.Runtime").Location); report.AddTrustedCodeModule(Assembly.Load("System.Reflection").Location); report.AddTrustedCodeModule(Assembly.Load("System.Collections").Location);
- 针对子目录插件部署场景,在程序启动入口(Program.cs)注册程序集解析事件,确保子目录下的依赖可以被正常加载,示例代码:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var pluginPath = Path.Combine(AppContext.BaseDirectory, "你的插件子目录名称"); var targetDll = $"{new AssemblyName(args.Name).Name}.dll"; var fullPath = Path.Combine(pluginPath, targetDll); return File.Exists(fullPath) ? Assembly.LoadFrom(fullPath) : null; };
无需将rdlc文件拷贝到exe根目录,只要嵌入资源加载逻辑正确、程序集解析路径配置完成,子目录部署场景可正常运行。
内容的提问来源于stack exchange,提问作者Mark Whiting
相关产品推荐
相关产品推荐

