How to load app.config in hydra WPF plugin for VCL

I am trying the RemObject Hydra to embed a WPF module inside a VCL app.

In this WPF module, I have Grid Controls, and EntityFramework DataContext. It’s connectionString, providers and everything stored in a 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>

The next step was to make the VCL host with delphi then load the WPF module’s .dll using the HYModuleManager component. This is pretty straigtforward :

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

I run the application in Delphi. But this error is thrown :

An exception was thrown by the target of an invocation

So with a little more research, I run this application (HydraHost.exe) using VisualStudio, and realize that this si the exception thrown by the module :

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

I then decide to write a WPF host. I set the WPF Host App.config to include the connection string and provider infos. To no surprise, everything works fine.

So, with the VCL host, the connectionString is not found. With the WPF host, it is. My conclusion is that the app.config is not loaded by the VCL host but it is loaded by the WPF host.

So what I did next is to manually copy the WPFHost.dll.config to VCLHost.exe.config as suggested by one of the answers, but same error, connectionString is not found.

So my question is : How can I “link” a .exe.config to a VCL app built with Delphi?

Please copy your question’s details here from StackOverflow

Done!

Hello

So, from what I see in the .NET FX sources .NET relies on Assembly.GetEntryAssembly() to determine the configuration file name. Still for plugins loaded into unmanaged host EnryAssembly is null. So there are 2 possible solutions:

A. Create your own configuration file, load values from it and configure stuff directly in code

B. Tell .NET where it can find the app.config file.

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"...path to the config file here...\App.config");

typeof(ConfigurationManager)
			.GetField("s_initState", BindingFlags.NonPublic |	 BindingFlags.Static)
			.SetValue(null, 0);

typeof(ConfigurationManager)
			.GetField("s_configSystem", BindingFlags.NonPublic |
										BindingFlags.Static)
			.SetValue(null, null);

typeof(ConfigurationManager)
			.Assembly.GetTypes()
			.Where(x => x.FullName ==
						"System.Configuration.ClientConfigPaths")
			.First()
			.GetField("s_current", BindingFlags.NonPublic |
								   BindingFlags.Static)
			.SetValue(null, null);

The cryptic reflection-related part is required to reset the internal caches of the Configuration Manager. It can be avoided if the APP_CONFIG_FILE option is set BEFORE the ConfigurationManager’s static constructor is called.

Hope that helps