Microsoft.Office.Interop dll 的单元测试在生成服务器上失败

发布于 2024-11-25 12:00:55 字数 599 浏览 0 评论 0原文

我写了一个单元测试。测试中的代码引用 Microsoft.Office.Interop.Excel.dll。

测试在我的机器上运行良好,但在构建服务器上失败。它在实例化 Excel 应用程序的行上失败:

var application = new Application { Visible = Visible };

错误是:

System.Runtime.InteropServices.COMException: 
Retrieving the COM class factory for component with CLSID 
{00024500-0000-0000-C000-000000000046} failed due to the following error:
80040154 Class not registered

这是否意味着我必须在构建服务器上安装可再发行主互操作程序集?如果是这样,我还需要在构建服务器上安装 Excel 吗?

编辑: Microsoft.Office.Interop.Excel.dll 包含在我的解决方案中,并从那里引用它。

I've written a unit test. The code under test references Microsoft.Office.Interop.Excel.dll.

The test runs fine on my machine but fails on the build server. It fails on the line where the Excel application is instantiated:

var application = new Application { Visible = Visible };

The error is:

System.Runtime.InteropServices.COMException: 
Retrieving the COM class factory for component with CLSID 
{00024500-0000-0000-C000-000000000046} failed due to the following error:
80040154 Class not registered

Does this mean I have to install the Redistributable Primary Interop Assemblies on my build server? And if so, do I also need to install Excel on the build server?

Edit:
Microsoft.Office.Interop.Excel.dll is included in my solution and it is referenced from there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-12-02 12:00:55

如果将 Microsoft.Office.Interop.Excel.dll 添加到库文件夹中的解决方案并引用其中的程序集,则在升级时构建计算机应该获取所需的程序集。否则,您将引用 Excel 安装时附带的程序集。此外,如果您尝试分发应用程序而不将其放入解决方案中,则尝试运行未安装 Excel 的应用程序的任何人都会遇到运行时错误。

当然,如果您不想重新分发程序集,您也可以选择在调用 Excel 程序集的代码周围放置 try/catch,然后向用户显示一条消息,说明需要安装 Excel。

最后,每个版本的 Excel(2010、2007 等)都有不同的版本号。这意味着,如果您使用 2010 程序集,并且有人安装了 2007 Excel,他们将收到运行时错误。我最终使用的答案是反射来选择在其环境中使用哪个程序集。

If you add Microsoft.Office.Interop.Excel.dll to the solution in a library folder and reference the assembly there the build machine should get the required assembly when you promote. Otherwise, you're referencing an assembly that comes with the installation of Excel. Also, if you try to distribute your application without putting it in the solution anyone who tries to run your application that doesn't have Excel installed will get run time errors.

Or course, you're other option if you do not want to redistribute the assembly is to put a try/catch around the code that calls the Excel assembly and then display a message to the user saying an installation of Excel is required.

Finally, each version of Excel (2010, 2007, etc) has different version numbers. Meaning, if you're using the 2010 assembly and someone has 2007 Excel installed they will get run time errors. The answer I ended up using for that was reflection to pick which assembly is used in their environment.

我纯我任性 2024-12-02 12:00:55

是的,您似乎需要安装 PIA:

Microsoft Office 产品的主要互操作程序集也可在可再发行的 Microsoft Windows Installer 程序包中找到:

可以从 Microsoft 下载中心 (http:// /go.microsoft.com/fwlink/?LinkId=50479)。

2007 Microsoft Office system 的软件包可从 Microsoft 下载中心 (http ://go.microsoft.com/fwlink/?LinkId=72637)。

并且,为了完整起见,Office 2010 软件包位于此处:(http:// www.microsoft.com/download/en/details.aspx?id=3508

Yes, it looks like you need to install the PIA:

The primary interop assemblies for Microsoft Office products are also available in a redistributable Microsoft Windows Installer package:

The package for Microsoft Office 2003 is available for download from the Microsoft Download Center (http://go.microsoft.com/fwlink/?LinkId=50479).

The package for the 2007 Microsoft Office system is available for download from the Microsoft Download Center (http://go.microsoft.com/fwlink/?LinkId=72637).

And, for completeness, the Office 2010 package is here: (http://www.microsoft.com/download/en/details.aspx?id=3508)

哥,最终变帅啦 2024-12-02 12:00:55

您需要安装 Office 和该版本的主互操作程序集,或 Visual Studio 才能使其正常工作。在 CI 环境中运行时您可能会遇到 COM 问题,如果您可以重组测试以避免使用 Office Interop,我强烈推荐它。

You'll need to install Office and the Primary Interop Assemblies for that version, or Visual Studio to get this to work. You might run into COM issues with running in a CI environment, and if you can restructure your tests to avoid using Office Interop, I'd highly recommend it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文