Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to import a .NET DLL having App.config with a configuration section handler?
    primarykey
    data
    text
    <p>I am trying to use a DLL from our in-house .net application.</p> <p>The DLL has a App.config file, and has a config section that specifies a configuration handler.</p> <p>I am unable to get my PowerShell script to load this dll.</p> <p>I have boiled the problem down into the simplest form I can.</p> <p>Here is the PowerShel script I am trying:</p> <pre><code>[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "D:\CustomConfig\CustomConfig\CustomConfigTestHarness\bin\Debug\CustomConfigTestHarness.exe.config") Add-Type -Path 'D:\CustomConfig\CustomConfig\CustomConfigTestHarness\bin\Debug\CustomConfig.dll' $mainClass = New-Object CustomConfig.Main $mainClass.TestConfig() </code></pre> <p>This is the config file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;section name="PrimarySqlServers" type="CustomConfig.ServerConfiguration, CustomConfig" /&gt; &lt;/configSections&gt; &lt;PrimarySqlServers&gt; &lt;Server name="data source=SQL\SQL2005; Initial Catalog=master; Trusted_Connection=yes;"/&gt; &lt;/PrimarySqlServers&gt; &lt;/configuration&gt; </code></pre> <p>Here is the DLL:</p> <pre><code>namespace CustomConfig { public class Main { public string TestEcho(string message) { return message; } public string TestConfig() { Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string sectionName = "PrimarySqlServers"; ServerConfiguration serverConfiguration = configuration.GetSection(sectionName) as ServerConfiguration; if (serverConfiguration == null || serverConfiguration.ServerList.Count == 0) { throw new ConfigurationErrorsException(string.Format(CultureInfo.InvariantCulture, "ERR_MISSING_OR_EMPTY_SECTION", sectionName)); } ServerConfigurationEntry entry = serverConfiguration.ServerList[0]; { return entry.Name; } } } } </code></pre> <p>The configClass.cs file reads:</p> <pre><code>using System; using System.Configuration; using System.Diagnostics; namespace CustomConfig { /// &lt;summary&gt; /// Contains individual configuration information about a site to be deployed /// &lt;/summary&gt; public class ServerConfiguration : ConfigurationSection { /// &lt;summary&gt; /// Get the collection of assembly items /// &lt;/summary&gt; [ConfigurationProperty("", IsDefaultCollection = true)] public ServerConfigurationCollection ServerList { get { return (ServerConfigurationCollection) base[""]; } } } /// &lt;summary&gt; /// ContextCollection - represents the collection of context nodes /// &lt;/summary&gt; public class ServerConfigurationCollection : ConfigurationElementCollection { /// &lt;summary&gt; /// Get the assembly item element with the given name /// &lt;/summary&gt; /// &lt;param name="name"&gt;The name of the item you want&lt;/param&gt; /// &lt;returns&gt;The item specified&lt;/returns&gt; public new ServerConfigurationEntry this[string name] { get { if (IndexOf(name) &lt; 0) return null; return (ServerConfigurationEntry) BaseGet(name); } } /// &lt;summary&gt; /// Get a assembly item element by index /// &lt;/summary&gt; /// &lt;param name="index"&gt;The index of the item to get&lt;/param&gt; /// &lt;returns&gt;The item specified&lt;/returns&gt; public ServerConfigurationEntry this[int index] { get { return (ServerConfigurationEntry) BaseGet(index); } } /// &lt;summary&gt; /// Clear the collection of items /// &lt;/summary&gt; public void Clear() { BaseClear(); } /// &lt;summary&gt; /// Add a new item to the collection /// &lt;/summary&gt; /// &lt;param name="name"&gt;The name of the site to add&lt;/param&gt; public void AddItem(string name) { ServerConfigurationEntry newEntry = new ServerConfigurationEntry(); newEntry.Name = name; this.BaseAdd(newEntry, true); } /// &lt;summary&gt; /// Get the index of a given assembly item /// &lt;/summary&gt; /// &lt;param name="name"&gt;The name of the item to get the index of&lt;/param&gt; /// &lt;returns&gt;The index of the given item, or -1 if not found&lt;/returns&gt; public int IndexOf(string name) { for (int index = 0; index &lt; base.Count; index++) { if (string.Compare(this[index].Name, name, StringComparison.OrdinalIgnoreCase) == 0) return index; } return -1; } /// &lt;summary&gt; /// Get the type of collection. BasicMap in this case. /// &lt;/summary&gt; public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } /// &lt;summary&gt; /// Factory up a new element /// &lt;/summary&gt; /// &lt;returns&gt;A new element&lt;/returns&gt; protected override ConfigurationElement CreateNewElement() { return new ServerConfigurationEntry(); } /// &lt;summary&gt; /// Get the unique key for a assembly item element /// &lt;/summary&gt; /// &lt;param name="element"&gt;The element to get the key for&lt;/param&gt; /// &lt;returns&gt;A unique identifier for the element&lt;/returns&gt; protected override object GetElementKey(ConfigurationElement element) { return ((ServerConfigurationEntry) element).Name; } /// &lt;summary&gt; /// Get the XML element name for elements of this type /// &lt;/summary&gt; protected override string ElementName { get { return "Server"; } } } /// &lt;summary&gt; /// ContextElement - represents a single context element in the config /// &lt;/summary&gt; [DebuggerDisplay("{Name}")] public class ServerConfigurationEntry : ConfigurationElement { private const string NAME = "name"; /// &lt;summary&gt; /// Get or set the server name or connection string /// &lt;/summary&gt; [ConfigurationProperty(NAME, DefaultValue = "", IsRequired = true, IsKey = true)] public string Name { [DebuggerStepThrough] get { return (string) this[NAME]; } [DebuggerStepThrough] set { this[NAME] = value; } } } } </code></pre> <p>The error message I get when I try to run it is:</p> <pre><code> Exception calling `"TestConfig" with "0" argument(s)`: "An error occurred creating the configuration section handler for PrimarySqlServers: Could not load file or assembly 'CustomConfig' or one of its dependencies. The system cannot find the file specified. (D:\CustomConfig\CustomConfig\CustomConfigTestHarness\bin\Debug\CustomConfigTestHarness.exe.config line 4)" At C:\dll.ps1:15 char:1 + $mainClass.TestConfig() + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ConfigurationErrorsException </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload