3

社内の .net アプリケーションから DLL を使用しようとしています。

DLL には App.config ファイルがあり、構成ハンドラーを指定する構成セクションがあります。

PowerShell スクリプトでこの dll をロードできません。

問題を可能な限り単純な形にまとめました。

これが私が試しているPowerShelスクリプトです:

[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()

これは設定ファイルです:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="PrimarySqlServers" type="CustomConfig.ServerConfiguration, CustomConfig" />
    </configSections>
    <PrimarySqlServers>
        <Server name="data source=SQL\SQL2005; Initial Catalog=master;  Trusted_Connection=yes;"/>
    </PrimarySqlServers>
</configuration>

DLL は次のとおりです。

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;
            }
        }
    }
}

configClass.cs ファイルの内容は次のとおりです。

using System;
using System.Configuration;
using System.Diagnostics;

namespace CustomConfig
{
    /// <summary>
    /// Contains individual configuration information about a site to be deployed
    /// </summary>
    public class ServerConfiguration : ConfigurationSection
    {
        /// <summary>
        /// Get the collection of assembly items
        /// </summary>
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public ServerConfigurationCollection ServerList
        {
            get { return (ServerConfigurationCollection) base[""]; }
        }
    }

    /// <summary>
    /// ContextCollection - represents the collection of context nodes
    /// </summary>
    public class ServerConfigurationCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Get the assembly item element with the given name
        /// </summary>
        /// <param name="name">The name of the item you want</param>
        /// <returns>The item specified</returns>
        public new ServerConfigurationEntry this[string name]
        {
            get
            {
                if (IndexOf(name) < 0) return null;
                return (ServerConfigurationEntry) BaseGet(name);
            }
        }

        /// <summary>
        /// Get a assembly item element by index
        /// </summary>
        /// <param name="index">The index of the item to get</param>
        /// <returns>The item specified</returns>
        public ServerConfigurationEntry this[int index]
        {
            get { return (ServerConfigurationEntry) BaseGet(index); }
        }

        /// <summary>
        /// Clear the collection of items
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }

        /// <summary>
        /// Add a new item to the collection
        /// </summary>
        /// <param name="name">The name of the site to add</param>
        public void AddItem(string name)
        {
            ServerConfigurationEntry newEntry = new ServerConfigurationEntry();
            newEntry.Name = name;
            this.BaseAdd(newEntry, true);
        }


        /// <summary>
        /// Get the index of a given assembly item
        /// </summary>
        /// <param name="name">The name of the item to get the index of</param>
        /// <returns>The index of the given item, or -1 if not found</returns>
        public int IndexOf(string name)
        {
            for (int index = 0; index < base.Count; index++)
            {
                if (string.Compare(this[index].Name, name, StringComparison.OrdinalIgnoreCase) == 0)
                    return index;
            }

            return -1;
        }

        /// <summary>
        /// Get the type of collection. BasicMap in this case.
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        /// <summary>
        /// Factory up a new element
        /// </summary>
        /// <returns>A new element</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServerConfigurationEntry();
        }

        /// <summary>
        /// Get the unique key for a assembly item element
        /// </summary>
        /// <param name="element">The element to get the key for</param>
        /// <returns>A unique identifier for the element</returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServerConfigurationEntry) element).Name;
        }

        /// <summary>
        /// Get the XML element name for elements of this type
        /// </summary>
        protected override string ElementName
        {
            get { return "Server"; }
        }
    }

    /// <summary>
    /// ContextElement - represents a single context element in the config
    /// </summary>
    [DebuggerDisplay("{Name}")]
    public class ServerConfigurationEntry : ConfigurationElement
    {
        private const string NAME = "name";

        /// <summary>
        /// Get or set the server name or connection string
        /// </summary>
        [ConfigurationProperty(NAME, DefaultValue = "", IsRequired = true, IsKey = true)]
        public string Name
        {
            [DebuggerStepThrough]
            get { return (string) this[NAME]; }
            [DebuggerStepThrough]
            set { this[NAME] = value; }
        }
    }
}

実行しようとすると表示されるエラーメッセージは次のとおりです。

 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
4

2 に答える 2

0

カスタム構成セクションで、アセンブリ名が間違っていると思います。

あなたの投稿によると、これは次のようになります。

<section name="PrimarySqlServers" type="CustomConfig.ServerConfiguration, CustomConfigTestHarness.exe" />

2 番目のパラメーターは、CustomConfig.ServerConfiguration 型の定義を含むアセンブリまたは実行可能ファイルの名前です。

于 2013-02-26T08:23:54.843 に答える