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

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

问题描述

我有一个现有的.net库,我希望从Excel VBA(该部分工作正常)。这些库依赖于app.config中的设置。我知道我可以在excel.exe.config文件(放置在与excel.exe相同的目录)中输入这些设置,但这并不真的像一个非常易于管理的解决方案,我可以看到导致冲突if多个应用程序想要这样做。

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的设置是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 ++

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 addin在其自己的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天全站免登陆