Subsonic Access To App.Config Connection Strings From Referenced DLL in Powershell Script
Asked Answered
A

1

3

I've got a DLL that contains Subsonic-generated and augmented code to access a data model. Actually, it is a merged DLL of that original assembly, Subsonic itself and a few other referenced DLL's into a single assembly, called "PowershellDataAccess.dll. However, it should be noted that I've also tried this referencing each assembly individually in the script as well and that doesn't work either.

I am then attempting to use the objects and methods in that assembly. In this case, I'm accessing a class that uses Subsonic to load a bunch of records and creates a Lucene index from those records.

The problem I'm running into is that the call into the Subsonic method to retrieve data from the database says it can't find the connection string. I'm pointing the AppDomain at the appropriate config file which does contain that connection string, by name.

Here's the script.

$ScriptDir = Get-Location
[System.IO.Directory]::SetCurrentDirectory($ScriptDir)
[Reflection.Assembly]::LoadFrom("PowershellDataAccess.dll")
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$ScriptDir\App.config")
$indexer = New-Object LuceneIndexingEngine.LuceneIndexGenerator
$indexer.GeneratePageTemplateIndex("PageTemplateIndex");

I went digging into Subsonic itself and the following line in Subsonic is what's looking for the connection string and throwing the exception:

ConfigurationManager.ConnectionStrings[connectionStringName]

So, out of curiosity, I created an assembly with a single class that has a single property that just runs that one line to retrieve the connection string name.

I created a ps1 that called that assembly and hit that property. That prototype can find the connection string just fine.

Anyone have any idea why Subsonic's portion can't seem to see the connection strings?

Asgard answered 7/5, 2010 at 16:0 Comment(0)
A
9

Did you add the System.Configuration assembly to your PowerShell session? The following works for me:

PS> gc .\app.config

<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings>
      <clear />
      <add name="Name"
           providerName="System.Data.ProviderName"
           connectionString="Valid Connection String;" />
    </connectionStrings>
</configuration>

PS> [appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$home\app.config")
PS> Add-Type -AssemblyName System.Configuration
PS> [Configuration.ConfigurationManager]::ConnectionStrings['Name']

Name                    : Name
ConnectionString        : Valid Connection String;
...
Ahwaz answered 8/5, 2010 at 20:20 Comment(2)
I did try other prototypes where my code goes after connection strings through ConfigurationManager. Those all work. It's only when I hand off control to Subsonic code and Subsonic makes it's call to ConfigurationManager that the config seems to disappear.Asgard
Are you loading the System.Configuration.dll assembly into PowerShell before invoking the Subsonic generated code? If so, then could you list the error info e.g. $error[0] | fl * -forceAhwaz

© 2022 - 2024 — McMap. All rights reserved.