我正在开发一个插件解决方案。该解决方案是我们提供的众多插件之一,所有插件均通过同一安装程序安装。为此,我为所有插件提供了一个安装程序解决方案,并且每个插件都有一个 System.Configuration.Install.Installer 的实现,它是通过调用 ManagedInstallerClass.InstallHelper 来执行的。
在我的一个插件中,父软件要求我注册包含插件入口点的 DLL。因此,我在安装程序类中执行常规安装,在复制文件列表中找到相关文件,并尝试使用调用
RegistrationServices.RegisterAssembly(Assembly.LoadFrom(copiedFileLocation), AssemblyRegistrationFlags.SetCodeBase)
进行 com 注册
这就是我的代码现在的样子:
致电我的安装人员:
List<string> copiedFiles = CopyFiles(Session.InstallPath, installPath);
var interfaceDllPath = copiedFiles.FirstOrDefault(cf => cf.EndsWith(@"\MyInterface.dll"));
if (interfaceDllPath != null)
{
try
{
var comRegistrar = new ComRegistrar(interfaceDllPath);
bool registerSuccess = comRegistrar.Register();
if (registerSuccess)
{
Context.LogMessage($"Successfully COM-registered DLL at {interfaceDllPath}");
Context.LogMessage("Enabling AddIn Auto-Load");
SetAutoLoadEntry();
}
else
{
Context.LogMessage("Failed to com-register DLL (unknown reason).");
}
}
catch (Exception exception)
{
Context.LogMessage($"Error during com-registration: {exception}");
throw;
}
}
ComRegistrar 的实际实现:
public class ComRegistrar
{
private readonly string _assemblyPath;
public ComRegistrar(string assemblyPath)
{
if (string.IsNullOrWhiteSpace(assemblyPath))
throw new ArgumentException("Error in ComRegistrar: assemblyPath cannot be null or empty.");
_assemblyPath = assemblyPath;
}
public bool Register()
{
if (String.IsNullOrEmpty(_assemblyPath))
{
throw new ArgumentException("Error in ComRegistrar: assemblyPath cannot be null or empty.");
}
RegistrationServices registrationService = new RegistrationServices();
return registrationService.RegisterAssembly(Assembly.LoadFile(_assemblyPath), AssemblyRegistrationFlags.SetCodeBase);
}
public bool Unregister()
{
if (String.IsNullOrEmpty(_assemblyPath))
{
throw new ArgumentException("Error in ComRegistrar: assemblyPath cannot be null or empty.");
}
RegistrationServices registrationService = new RegistrationServices();
registrationService.UnregisterAssembly(Assembly.LoadFile(_assemblyPath));
//Force GarbageCollect to close file handle on Assembly before uninstall.
registrationService = null;
GC.Collect();
GC.WaitForPendingFinalizers();
return true;
}
}
请忽略取消注册期间的手动 GC(我必须这样做,因为 RegistrationServices 会在 DLL 上保留打开的文件句柄)。
在注册方法期间,我收到以下错误: 无法加载文件或程序集“AddInAssistant,Version=19.23.516.10987,Culture=neutral,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件。
这个外部依赖是一个取自父软件的DLL,我们从中提取一些类型。
我检查了我们要安装的目录:AddInAssistant.dll 存在于此。我检查了版本:AssemblyVersion 正是我要查找的数字。
检查异常中的FusLog:
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\1_Develop_\Repos\External.Interfaces\Interfaces.Installer\Interfaces.Installer\UI.TestShell\bin\Debug\TDM Interfaces Installer.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = AddInAssistant, Version=19.23.516.10987, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\1_Develop_\Repos\External.Interfaces\Interfaces.Installer\Interfaces.Installer\UI.TestShell\bin\Debug\TDM Interfaces Installer.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant.EXE.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant/AddInAssistant.EXE.
如您所见,我的安装程序项目仅在其自己的位置查找 DLL,而不是在 MyInterface.dll 旁边
有趣的是,当我检查写入磁盘的实际 FusLog 时,它实际上正在查看正确的位置,不知何故,它只是忽略了正确的 DLL is 的事实:
*** Assembly Binder Log Entry (21.09.2023 @ 14:23:18) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\1_Develop_\Repos\External.Interfaces\Interfaces.Installer\Interfaces.Installer\UI.TestShell\bin\Debug\TDM Interfaces Installer.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = AddInAssistant, Version=19.23.516.10987, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = TDM Interfaces Installer.exe
Calling assembly : TDMInterface, Version=2023.0.0.1, Culture=neutral, PublicKeyToken=null.
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\1_Develop_\Repos\External.Interfaces\Interfaces.Installer\Interfaces.Installer\UI.TestShell\bin\Debug\TDM Interfaces Installer.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant.EXE.
LOG: Attempting download of new URL file:///C:/1_Develop_/Repos/External.Interfaces/Interfaces.Installer/Interfaces.Installer/UI.TestShell/bin/Debug/AddInAssistant/AddInAssistant.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/ParentSoftware/AddIns/MyPluginFolder/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/ParentSoftware/AddIns/MyPluginFolder/AddInAssistant/AddInAssistant.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/ParentSoftware/AddIns/MyPluginFolder/AddInAssistant.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/ParentSoftware/AddIns/MyPluginFolder/AddInAssistant/AddInAssistant.EXE.
LOG: All probing URLs attempted and failed.
最好的部分:如果我手动进行安装,使用 regasm.exe /codebase 不会发生错误,我可以正确启动应用程序。
有什么建议吗=/?
不要尝试在安装时进行注册。相反,请在构建期间捕获注册并将注册表项包含在安装程序中,以避免与自定义操作和自定义操作服务器进行复杂的交互。
对于 WiX 工具集,我们 (FireGiant) 有一个扩展,可以在构建过程中捕获注册。