【问题标题】:How to load a App.Config file into a VCL application?如何将 App.Config 文件加载到 VCL 应用程序中?
【发布时间】:2017-10-22 23:59:04
【问题描述】:

我正在尝试 RemObject Hydra 在 VCL 应用程序中嵌入 WPF 模块。

在这个 WPF 模块中,我有网格控件和 EntityFramework DataContext。它是connectionString、providers,所有内容都存储在app.config 中。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ModelConnection" connectionString="character set=UTF8;data source=localhost;initial catalog=PATHTODATABASE.FDB;user id=SYSDBA;password=MASTERKEY" providerName="FirebirdSql.Data.FirebirdClient" />
  </connectionStrings>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="FirebirdSql.Data.EntityFramework6.FbConnectionFactory, EntityFramework.Firebird" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="FirebirdSql.Data.FirebirdClient" type="FirebirdSql.Data.EntityFramework6.FbProviderServices, EntityFramework.Firebird" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FirebirdSql.Data.FirebirdClient" publicKeyToken="3750abcc3150b00c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
<system.data>
        <DbProviderFactories>
            <remove invariant="FirebirdSql.Data.FirebirdClient" />
            <add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
        </DbProviderFactories>
    </system.data></configuration>

下一步是使用delphi 制作VCL 主机,然后使用HYModuleManager 组件加载WPF 模块的.dll。这很简单:

procedure TMainForm.FormCreate(Sender: TObject);
begin
    HYModuleManager1.LoadModule('%path%\to\wpf\module\GridsModule.dll');
    HYModuleManager1.CreateVisualPlugin('ClientGridView', fInstance, Panel1);
end;

我在 Delphi 中运行应用程序。但是会抛出这个错误:

(翻译:调用目标抛出异常)

因此,通过更多研究,我使用 VisualStudio 运行此应用程序 (HydraHost.exe),并意识到这是模块引发的异常:

System.InvalidOperationException : 'No connection string named 'ModelConnection' could be found in the application config file.'

然后我决定编写一个 WPF 主机。我将 WPF Host App.config 设置为包含连接字符串和提供程序信息。毫不奇怪,一切正常。

因此,对于 VCL 主机,找不到 connectionString。对于 WPF 主机,它是。我的结论是 app.config 不是由 VCL 主机加载的,而是由 WPF 主机加载的。

所以我接下来要做的是按照答案之一的建议手动将 WPFHost.dll.config 复制到 VCLHost.exe.config,但同样的错误,未找到 connectionString。

所以我的问题是:如何将 .exe.config “链接”到使用 Delphi 构建的 VCL 应用程序?

【问题讨论】:

标签: wpf delphi dll plugins app-config


【解决方案1】:

您应该将 app.config 重命名为 YourExeName.exe.config,因为它现在可能是 YourDllName.dll.config

【讨论】:

  • 是的,我已经尝试过了,但似乎没有加载。我仍然收到 MyConnectionString not found 错误...您确定 delphi VCL exe 会尝试查找 .exe.config 吗?
  • Vcl exe 对配置文件一无所知,但 .net ConfigurationManager 加载可执行配置文件。我们还使用 hydra 将 .net 程序集加载到我们的 vcl exe 中。虽然我们没有使用配置文件来自动配置选项,但我们从那里读取了一些 appSettings。
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2013-07-14
  • 2016-12-20
  • 1970-01-01
  • 2010-09-17
  • 2010-10-22
相关资源
最近更新 更多