Microsoft.Office.Interop dll 的单元测试在生成服务器上失败
我写了一个单元测试。测试中的代码引用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将 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.
是的,您似乎需要安装 PIA:
并且,为了完整起见,Office 2010 软件包位于此处:(http:// www.microsoft.com/download/en/details.aspx?id=3508)
Yes, it looks like you need to install the PIA:
And, for completeness, the Office 2010 package is here: (http://www.microsoft.com/download/en/details.aspx?id=3508)
您需要安装 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.