为什么在 Crystal Report Export 中 System.InvalidCastException?

Why System.InvalidCastException in CrystalReport Export?

我正在创建一个 WCF 服务,它将使用 Crystal 报告创建 .pdf 并将文件保存在服务器的特定路径中。为什么我在这一行出现 System.InvalidCastException 异常?此外,我在每个导出函数中都遇到了这个异常。

MemoryStream memoryStream = (MemoryStream)cryRpt.ExportToStream(ExportFormatType.PortableDocFormat);

异常详情:

System.InvalidCastException: 'Unable to cast COM object of type 'CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass' to interface type 'CrystalDecisions.ReportAppServer.Controllers.ISCRReportSource'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{31E7715A-8AD0-4D1C-958E-C1BE0A6F2D0C}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

Exception Screenshot

注意:Crystal报告没有问题。当我在控制台应用程序中使用相同的代码时,它起作用了。

因为它没有return MemoryStream。

查看 this link 以获取更多信息。他们说该方法 return 是 FileStreamDeleteOnClose 的实例,因为 Crystal 的某些版本是开发人员的决定。

看看 this post 以了解如何以另一种方式处理它。

编辑:

this post 中,解决方法是降级 Crystal 报告。不好看...

SAP forums中有讨论。据说,为了避免这种情况,Microsoft Windows 更新 KB2999226 是 SP21 的先决条件(在一些细节中)。检查您的 Crystal 报告版本并检查它是否适用于您。

好的。只需将 Crystal 报告更新到版本 18.0.23 即可解决我的问题。可能是旧版本不适用于 WCF。