Tim*_*Tim 4 c# windows-installer exception visual-studio-2010
我使用Visual Studio 2010在64位系统上开发了一项服务.该服务只是一个访问辅助库中的Start和Stop方法的框架.该库访问64位COM对象,并且必须构建为x64.COM对象是一个单独构建为x64并在64位环境中测试的DLL.我有一个安装程序,用于设置项目并通过自定义操作安装服务.
我使用绕过服务的测试应用程序调试代码,因此我可以确认服务正在访问的库是否正常工作.
我遇到的问题是安装时出现BadImageFormatException.我正在使用目标平台为x64的安装程序.如果我将所有内容构建为x64,我会收到以下消息:
Error 1001. Exception occured while initializing the installation: System.BadImageFormatException. Could not load file or assembly... or one of its dependencies. An attempt was made to load a program with an incorrect format.
Run Code Online (Sandbox Code Playgroud)
如果我将服务设置为构建为任何CPU,则安装正常,并且服务可以访问库,但无法找到COM对象.如果我删除安装该服务的自定义操作,则安装也将起作用.如果我然后尝试使用installutil手动安装服务,我会收到与上面相同的错误消息.
下面是我在服务中使用的库的列表.
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using MAPIMail;
using MAPIMail.Logging;
Run Code Online (Sandbox Code Playgroud)
请注意,MAPIMail项目正在构建为x64.
有没有人知道为什么我不能专门安装x64作为x64系统上的任何CPU安装服务.我感谢任何和所有的建议.
我尝试了Alex和Jacob Seleznev的解决方案,他们都工作了.我发现第二个答案,由Greg Sansom,雅各布的链接提供了最好的解决方案.为了帮助别人,我在下面复制了他的答案:
如果您的安装程序正在安装64位dll,则会发生这种情况.从MSDN复制以下内容:
如果将64位托管自定义操作添加到安装项目,则Visual Studio生成过程会将32位版本的InstallUtilLib.dll作为InstallUtil嵌入到MSI中.反过来,加载32位.NET Framework以运行64位托管自定义操作并导致BadImageFormatException异常.
要解决此问题,请将32位InstallUtilLib.dll替换为64位版本.
Run Code Online (Sandbox Code Playgroud)1. Open the resulting .msi in Orca from the Windows Installer SDK. 2. Select the Binary table. 3. Double click the cell [Binary Data] for the record InstallUtil. 4. Make sure "Read binary from filename" is selected and click the Browse button. 5. Browse to %WINDIR%\Microsoft.NET\Framework64\v2.0.50727. 6. The Framework64 directory is only installed on 64-bit platforms and corresponds to the 64-bit processor type. 7. Select InstallUtilLib.dll. 8. Click the Open button. 9. Click the OK button.分享|编辑|标志回答2011年7月23日3:09 Greg Sansom 6,5781335