【问题标题】:.NET 1.1 and .NET 2 config files working together.NET 1.1 和 .NET 2 配置文件一起工作
【发布时间】:2010-10-02 05:30:23
【问题描述】:

我继承了一个相当大的项目,其中包含一个用 VB6 编写的应用程序和几个用 VB6、VB.NET 1.1 和 VB.NET 2 编写的 DLL 和 ActiveX 控件。我想更改其中一个 DLL 的设置用 VB.NET 2 编写,在其 application.dll.config 文件中,但似乎没有任何效果。

我的主要 VB6 应用程序(我将其称为 Alpha)有一个配置文件 (Alpha.exe.cnfig),其中包含我的 VB.NET 1.1 DLL(我将其称为 Bravo)使用的设置。调用 Bravo 后,Alpha 调用 Charlie(我的 VB.NET 2 DLL)。但是,即使我在 DLL 所在的子目录中更改了 Charlie.dll.config 中的应用程序设置,它也没有任何效果。我曾尝试将 Charlie 的设置放在 Alpha 的配置文件中,但这会导致 Bravo 因自动化错误而失败(我认为这是因为配置文件的格式从 .NET 1.1 和 .NET 2 更改)。

下面是一个简化的目录结构和文件位置示例:

\Application\Alpha\Alpha.exe(我的 VB6 应用程序)
\Application\Alpha\Alpha.exe.config(此配置文件由 Bravo.dll 使用)
\Application\Assembly\Bravo.dll(我的 VB.NET 1.1 DLL)
\Application\Controls\Charlie\Charlie.dll(我的 VB.NET 2 DLL)
\Application\Controls\Charlie\Charlie.dll.config(这个文件被 Charlie.dll 忽略)

我已经重新编译了我的 VB.NET 2 DLL,更改了默认设置,我这样做是为了检查设置本身没有代码错误,这工作正常。但是,我希望能够通知客户如何更改配置文件,以便他可以将其设置为他想要的任何内容,而无需在每次他想要不同的设置时重新编译 DLL。

我只想更改 app.config 而不是 machine.config 或 user.config。

这里是一个 Alpha.exe.config 的例子:

<configuration>
    <appSettings>
        <add key="MySetting" value="MyValue" />
    </appSettings>
</configuration>

这里是 Charlie.dll.config 的示例

<configuration>
    <applicationSettings>
        <Charlie.My.MySettings>
            <setting name="MySetting" serializeAs="String">
                <value>MyValue</value>
            </setting>
        </Charlie.My.MySettings>
    </applicationSettings>
</configuration>

如果我尝试将 applicationSettings 部分 under 直接放在 Alpha.exe.config 中的 appSettings 部分(即作为配置元素的另一个子元素)下方,那么 Bravo.dll 将失败。

非常感谢您提供的任何帮助。

【问题讨论】:

    标签: .net vb.net interop app-config config


    【解决方案1】:

    问题似乎是因为 Bravo.dll 以 1.1 为目标,所以会加载 1.1 版本的框架,而且正如您所说,applicationSettings 部分是 .NET 2.0 中的新内容。解决办法是强制VB exe加载2.0框架。在 Alpha.exe.config 文件中添加 startup/supportedRuntime 元素应该可以解决问题。您还需要一些配置部分声明,因此最后 Alpha.exe.config 应如下所示:

    <configuration>
        <startup>
            <supportedRuntime version="v2.0.50727" />
        </startup>
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Charlie.My.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </configSections>
        <appSettings>
            <add key="MySetting" value="MyValue" />
        </appSettings>
        <applicationSettings>
            <Charlie.My.MySettings>
                <setting name="MySetting" serializeAs="String">
                    <value>MyValue</value>
                </setting>
            </Charlie.My.MySettings>
        </applicationSettings>
    </configuration>
    

    【讨论】:

    • 这就是我所说的在下面的意思,就像在下面,而不是在里面,很抱歉造成混乱。我已经尝试过您的建议,但它会导致自动化错误,我猜是因为 .NET 1.1 无法理解配置文件的 .NET 2 部分。
    • 我已尝试按照您的建议添加 configSections 元素,但这仍然会导致 Bravo.dll (.NET 1.1) 因自动化错误而失败,或者至少这是 VB6 报告的内容。
    • 我又更新了,添加supportedRuntime,希望最终能解决问题。
    • 唉,我仍然收到自动化错误。我也尝试过使用 requiredRuntime ,但没​​有效果。我担心我唯一的解决方案是将 Bravo.dll 移植到 .NET 2。
    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2018-03-30
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多