如何导入具有App.config和配置节处理程序的.NET DLL? [英] How to import a .NET DLL having App.config with a configuration section handler?

查看:75
本文介绍了如何导入具有App.config和配置节处理程序的.NET DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用内部.net应用程序中的DLL。

I am trying to use a DLL from our in-house .net application.

该DLL具有一个App.config文件,并具有一个config部分,用于指定一个配置处理程序。

The DLL has a App.config file, and has a config section that specifies a configuration handler.

我无法获取PowerShell脚本来加载该dll。

I am unable to get my PowerShell script to load this dll.

我已经煮沸了

这是我正在尝试的PowerShel脚本:

Here is the PowerShel script I am trying:

[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文件使用系统读取:

The configClass.cs file reads:

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

我收到的错误消息当我尝试运行它时:

The error message I get when I try to run it is:

 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


推荐答案

您遇到的问题存在的问题是该程序集不在.net的程序集搜索路径中。

The problem you're having is that the assembly isn't in .net's assembly search path.

您可以通过许多不同的方式来解决此问题(在

You can fix that in many different ways (including putting assemblies in the GAC, etc).

将有关该程序集的其余信息添加到您的部分键(版本,文化,公共)可能就足够了。密钥令牌...类似这样:

It might be enough to add the rest of the information about the assembly to your section key (the version, culture, public key token ... something like this:

<section name="PrimarySqlServers" type="CustomConfig.ServerConfiguration, CustomConfig, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 

另一种方法是编写事件处理程序:

Another way is to write an event handler:

$configdll = 'D:\CustomConfig\CustomConfig\CustomConfigTestHarness\bin\Debug\CustomConfig.dll'

[System.AppDomain]::CurrentDomain.add_AssemblyResolve({
    param($source, $assembly)
    # Detect that they're looking for YOUR assembly specifically
    if($assembly.Name.Split(",")[0] -eq "CustomConfig") {
        # And load it for them: your path will be difference
        return [System.Reflection.Assembly]::LoadFile( $configdll  )
    }
})

另一种方法是将dll放在应用程序的主目录中目录。
但是在这种情况下,这就是您的Windows\System32\WindowsPowerShell\v1.0\ ...所以这可能不是一个好计划。

Another way would be to put the dll in the app's home directory. But in this case, that's your Windows\System32\WindowsPowerShell\v1.0\ ... so that's probably not a great plan.

这篇关于如何导入具有App.config和配置节处理程序的.NET DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆