当通过 COM 调用时,我可以从 .net 代码使用/访问 app.config [英] Can I use / access the app.config from .net code, when called via COM

查看:24
本文介绍了当通过 COM 调用时,我可以从 .net 代码使用/访问 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组现有的 .net 库,我希望从 Excel VBA 调用它们(该部分工作正常).这些库依赖于 app.config 中的设置.我知道我可以在 excel.exe.config 文件中输入这些设置(与 excel.exe 放在同一目录中),但这对我来说似乎并不是一个非常易于管理的解决方案,因为我可以看到如果不止一个应用程序想要这样做.

I have an existing set of .net libraries that I wish to call from Excel VBA (that part is working fine). These libraries rely on settings in the app.config. I know I can enter these settings in a excel.exe.config file (placed in the same directory as the excel.exe), but this doesn't really seem like a very manageable solution to me, as I can see causing conflicts if more than one application wants to do this.

我的问题很简单:有没有什么方法可以让 COM 暴露的 dll 引用它们各自的配置文件?

My question is simple: is there any way of COM exposed dlls referring to their respective config files?

示例 app.config:

Sample app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test" value="Hello world!" />
  </appSettings>
</configuration>

示例 C#:

namespace ExcelVbaFacingCode
{
    public class SimpleAppSettingReader
    {
        public string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
    }
}

示例 VBA:

Public Sub Test()
    Dim Test As New SimpleAppSettingReader
    Dim sampleValue As String
    sampleValue = Test.GetValue("test")
    MsgBox "Value: '" + sampleValue + "'", vbOKOnly
End Sub

推荐答案

您确实可以指定 .NET 配置 API 将使用的配置文件,但您必须在创建 AppDomain 之前执行此操作(这不是听起来很简单.)例如,这就是 ASP.NET 的工作方式.它将 AppDomain 设置为使用位于虚拟目录中的web.config"文件,而不是需要 w3p.exe.config 或其他内容.

You can indeed specify the configuration file that will be used by the .NET configuration API's but you have to do it before the AppDomain is created (which is not as easy as it sounds.) This is how ASP.NET works for example. It sets up the AppDomain to use a "web.config" file located in the virtual directory as opposed to requiring an w3p.exe.config or what not.

您也可以这样做,但您需要创建一个辅助 AppDomain.这本身就是一个好主意,原因有很多,这些原因与多个加载项和程序集引用有关.但是,您可以在 AppDomain 的设置中添加一项内容a> 是app.config"文件的名称/路径.

You can do this too but you need to create a secondary AppDomain. This in itself is a good idea for a lot of reasons that have to do with multiple add-ins and assembly references. But one of the things you can put in the AppDomain's setup is the name/path of the "app.config" file.

要创建新的 AppDomain,您可以使用shim",它是一些样板 C++ 代码,用于配置和启动 AppDomain,从那时起的其余代码可以是托管代码.

To create the new AppDomain you can use a "shim" which is a bit of boilerplate C++ code that configures and starts the AppDomain and the rest of the code from that point on can be managed code.

MSDN 上有一篇文章由 Andrew Whitechapel 撰写,名为 使用 COM Shim 向导隔离 Office 扩展.我不会撒谎,这不是一个微不足道的概念,但也不错.有一个向导可以为您创建 C++ 项目,该项目将 .NET 插件加载到新的 AppDomain 中.AppDomain 的配置必须在加载之前完成,因此您需要将以下内容放入向导创建的代码中.

There's an article on MSDN written by Andrew Whitechapel and called Isolating Office Extensions with the COM Shim Wizard. I won't lie, it's not a trivial concept, but it's also not too bad. There's a wizard that creates the C++ project for you which loads the .NET addin into a new AppDomain. The configuration of the AppDomain must be done before it is loaded so you'll want to put the following in the wizard-created code.

// CLRLoader.cpp
HRESULT CCLRLoader::CreateLocalAppDomain()
{

    ... snip ...

    // Configure the AppDomain to use a specific configuration file
    pDomainSetup->put_ConfigurationFile(CComBSTR(szConfigFile));

    ... snip ...

}

还值得注意的是,如果/当您转换为 VSTO 项目时,VSTO 运行时会为您完成所有这些 AppDomain 配置.每个 VSTO 插件都在自己的 AppDomain 中运行,并带有自己的私有 app.config 文件.

It's also worth noting that if/when you convert to a VSTO project, the VSTO runtime does all this AppDomain configuration for you. Each VSTO addin runs in its own AppDomain with its own private app.config file.

这篇关于当通过 COM 调用时,我可以从 .net 代码使用/访问 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆