ConfigurationManager.GetSection给出错误"无法从程序集加载类型..."

ConfigurationManager.GetSection Gives Error “Could not load type…from assembly…”
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我的app.config文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />
    </configSections>
    <ProcessConfiguration>
        <processes>
            <process name="Process1" />
        </processes>
    </ProcessConfiguration>
</configuration>

我有以下(单独的)类来获取配置:

namespace Configuration
{
    using System.Configuration;

    public class ProcessesConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("processes", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ProcessCollection))]
        public ProcessCollection Processes
        {
            get
            {
                return (ProcessCollection)base["processes"];
            }
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessCollection : ConfigurationElementCollection
    {
        public ProcessConfig this[int index]
        {
            get
            {
                return (ProcessConfig)BaseGet(index);
            }

            set
            {
                BaseAdd(index, value);
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProcessConfig)element).Name;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ProcessConfig();
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessConfig : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name 
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }
    }
}

但是,当我点击以下代码行时:

var processConfigurationSection = ConfigurationManager.GetSection("ProcessConfiguration") as ProcessesConfigurationSection;

我收到指出以下内容的错误:

为ProcessConfiguration创建配置节处理程序时发生错误:无法从程序集"Configuration"中加载类型"Configuration.ProcessConfigurationSection".

我很沮丧,如果有人可以帮助我,我将非常感激.

速聊1:
您需要使用Fusion日志查看器进行调试. 只需确保以管理员身份运行它,打开日志,然后重新启动,然后再尝试调试.您将看到CLR在哪里寻找程序集,什么版本,并从那里确定为什么找不到该程序集(如果这是问题所在).
速聊2:
您正在构建的DLL的名称是什么?
速聊3:
您的类型ProcessesConfigurationSection在输入程序集中定义了吗?
速聊4:
我现在刚在名为"配置"的控制台应用程序中拥有它.Andrii,我应该在哪里查看类型是否在入口程序集中定义?谢谢.
解决过程1

在该行中:

<section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />

名称"配置"应指代您正在构建的DLL,请尝试对此进行检查并在需要时更正.

我也认为您可能有错字,在您的代码中类型名称为:

ProcessesConfigurationSection

(请注意流程与流程)

速聊1:
谢谢,编译后的二进制文件的名称是Configuration.exe,所以我认为这意味着那行正确吗?
速聊2:
看到加法,我认为这可能是错别字
速聊3:
在我的情况下,"配置"是我将配置处理程序放入的名称空间,它不在单独的DLL / Assembly中.
速聊4:
奇怪的是,正是"您可能有错字"解决了我的问题.我遇到此错误是因为我的大写错误.
解决过程2

由于调用程序集无法在指定程序集中加载类类型,因此引发了此错误.此错误可能是由类型名称后的空格引起的(这发生在我身上),例如下面的config部分

type="Configuration.ProcessConfigurationSection , Configuration"

也会产生此错误.

My app.config file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />
    </configSections>
    <ProcessConfiguration>
        <processes>
            <process name="Process1" />
        </processes>
    </ProcessConfiguration>
</configuration>

I have the following (separate) classes to get the configuration:

namespace Configuration
{
    using System.Configuration;

    public class ProcessesConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("processes", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ProcessCollection))]
        public ProcessCollection Processes
        {
            get
            {
                return (ProcessCollection)base["processes"];
            }
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessCollection : ConfigurationElementCollection
    {
        public ProcessConfig this[int index]
        {
            get
            {
                return (ProcessConfig)BaseGet(index);
            }

            set
            {
                BaseAdd(index, value);
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProcessConfig)element).Name;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ProcessConfig();
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessConfig : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name 
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }
    }
}

However when I hit this line of code:

var processConfigurationSection = ConfigurationManager.GetSection("ProcessConfiguration") as ProcessesConfigurationSection;

I get the error which states:

An error occurred creating the configuration section handler for ProcessConfiguration: Could not load type 'Configuration.ProcessConfigurationSection' from assembly 'Configuration'.

I'm completely stumped, if anyone can help me out I'd really appreciate it.

Talk1:
You'll want to debug this using the Fusion log viewer. Just make sure to run it as admin, turn on the log, and reboot before attempting to debug. You'll see where the CLR is looking for the assembly, and what version, and from there determine why it isn't being found, if that's the issue.
Talk2:
what is the name of the DLL you are building?
Talk3:
Is your type ProcessesConfigurationSection defined in the entry assembly?
Talk4:
I just have it in a console application just now, named 'Configuration'. Andrii, where would I look to see if the type is defined in the entry assembly? Thanks.
Solutions1

In the line:

<section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />

The name 'Configuration' should refer to the DLL that you re building, please try checking this and correct if needed.

Also I think you may have a typo, in your code the type name is:

ProcessesConfigurationSection

(Note the Processes vs Process)

Talk1:
Thanks, the name of the compiled binary is Configuration.exe so I think that means I have that line correct?
Talk2:
See addition, I think this may be a typo
Talk3:
In my case 'Confirguration' is the namespace that I put the configuration handler in , which is not in a separate DLL/Assembly .
Talk4:
Oddly enough, it was the "you may have a typo" that solved my problem. I got this error because I had the capitalization wrong.
Solutions2

This error has been raised because the invoking assembly couldn't load the class type in the specified assembly. This error can be caused by a space after the type name (which has happened to me), for example the following config section

type="Configuration.ProcessConfigurationSection , Configuration"

will generate this error too.

转载于:https://stackoverflow.com/questions/19095215/configurationmanager-getsection-gives-error-could-not-load-type-from-assembl

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入