如何将.NET app.config应用到进程外COM服务器?
我们有一个带有基于 COM 的插件系统的遗留应用程序。要注册本机插件,将调用 DllRegistryServer
,它会注册 COM 类并添加一些用于簿记的注册表信息。对于 .NET 组件,我们有一个用 C# 编写的 COM 服务器,它调用 RegistrationServices.RegisterAssembly
。为了获得最大兼容性,此 C# dll 以 .NET v2.0 为目标。 (本机)插件注册器 CoCreateInstance()
是 C# 服务器。
由于 .NET 插件可能以 .NET v4.0 为目标,因此我们在应用程序配置中具有以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Load 4.0 if available, otherwise 2.0 -->
<!-- http://msdn.microsoft.com/en-us/library/w4atty68.aspx -->
<!-- http://msdn.microsoft.com/en-us/library/w671swch.aspx -->
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
问题是 COM 注册需要在 Vista 或 7 中提升运行,因此插件注册器是使用 COM Elevation Moniker 创建的,这在系统代理 (dllhost.exe
) 中在进程外运行它。这会产生一个问题,因为应用程序配置未应用于 dllhost.exe
。
我们希望解决这个问题,而无需编写自定义代理、部署两个版本的 C# COM 服务器、需要 v4.0 运行时或手动托管 CLR。
如何将应用程序配置应用到由 COM Elevation Moniker 创建的对象?
We have a legacy application with a COM-based plugin system. To register a native plugin, DllRegistryServer
is called, which registers the COM classes and adds some registry information for bookkeeping. For .NET components, we have a COM server written in C# that calls RegistrationServices.RegisterAssembly
. For maximum compatibility, this C# dll targets .NET v2.0. The (native) plugin registrar CoCreateInstance()
s the C# server.
Since .NET plugins may target .NET v4.0, we have the following in the application config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Load 4.0 if available, otherwise 2.0 -->
<!-- http://msdn.microsoft.com/en-us/library/w4atty68.aspx -->
<!-- http://msdn.microsoft.com/en-us/library/w671swch.aspx -->
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
The issue is that COM registration needs to be run elevated in Vista or 7, so the plugin registrar is created with the COM Elevation Moniker, which runs it out-of-proc in the system surrogate (dllhost.exe
). This creates a problem, because the application config is not applied to dllhost.exe
.
We want to solve this problem without writing a custom surrogate, deploying two versions of the C# COM server, requiring the v4.0 runtime, or manually hosting the CLR.
How do I apply the application config to an object created by the COM Elevation Moniker?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参考这篇文章,您可能也读过:
http://blogs.msdn.com/b/clrteam/archive/2010/06/23/in-proc-sxs-and-migration-quick-start.aspx
查看“托管 COM 组件”部分。除了相对简单的更新注册表的方法(我个人会避免这样做,因为通过反射筛选来查找所有 CLSID 可能并不酷),我还看到了这段文字:
您可以尝试添加 Plugin.dll 吗?配置到您的插件之一,看看这是否可以解决问题?所包含的文章在我提到的部分下面有一些具体的示例,但就所有范围和目的而言,您似乎至少可以复制/粘贴您所拥有的用于测试的内容(为了更优雅一点,有些部分是您不需要的,具体取决于您的配置)。
鉴于这是来自 CLR 团队,我的假设是您的选择是 reg hack 或配置文件。
Refering this, which you have probably also read:
http://blogs.msdn.com/b/clrteam/archive/2010/06/23/in-proc-sxs-and-migration-quick-start.aspx
Look in the section "Managed COM Component". Aside from the relatively straightforward hack to update the registry (which I would personally avoid because sifting through reflection to find all your CLSIDs is probably not cool), I saw this bit of text:
Can you try adding Plugin.dll.config to one of your plugins and see if this resolves the issue? The included article has some specific examples below the section I mentioned, but for all extent and purposes it looks like you can copy/paste what you have for testing at least (to be a bit more elegant there are pieces you do not need depending on your configuration).
Given that this is from the CLR team, my assumption here is going to be that your choices are the reg hack or the config file.