如何将.NET app.config应用到进程外COM服务器?

发布于 2024-12-01 15:08:13 字数 1112 浏览 0 评论 0原文

我们有一个带有基于 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 技术交流群。

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

发布评论

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

评论(1

薔薇婲 2024-12-08 15:08:13

参考这篇文章,您可能也读过:
http://blogs.msdn.com/b/clrteam/archive/2010/06/23/in-proc-sxs-and-migration-quick-start.aspx

查看“托管 COM 组件”部分。除了相对简单的更新注册表的方法(我个人会避免这样做,因为通过反射筛选来查找所有 CLSID 可能并不酷),我还看到了这段文字:

如果通过免注册清单激活托管 COM 组件(无法扩展以包含 SupportedRuntimes 条目),则运行时激活将在包含 COM 可见类的程序集旁边查找配置文件

您可以尝试添加 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:

If a managed COM component is activated through a reg-free manifest (which cannot be extended to contain a SupportedRuntimes entry), runtime activation will look for a configuration file next to the assembly containing the COM visible class

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.

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