尝试在web配置中创建自定义配置部分时,无法加载文件或程序集

本文关键字:加载 文件 程序集 分时 配置部 web 配置 自定义 创建 | 更新日期: 2023-09-27 18:01:38

我正在尝试创建我自己的自定义配置部分:

web中的代码。配置文件:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="customConfigurationSection" type="CustomConfigurationSectionRepro.ConfigHandlers.CustomConfigurationSection" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>
<customConfigurationSection fileName="'Configs'customconfig.config" />
....
....
</configuration>
自定义配置处理程序文件中的代码:
namespace CustomConfigurationSectionRepro.ConfigHandlers
{
    public class CustomConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("fileName", IsRequired = false)]
        public string FileName
        {
            get
            {
                return (string)this["fileName"];
            }
            set
            {
                this["fileName"] = value;
            }
        }
    }
}

我的web应用程序只是一个标准的mvc web应用程序从Visual Studio MCV模板。当我在使用IIS Express的开发环境下运行它时,我一直得到错误提示。

Could not load file or assembly 'CustomConfigurationSectionRepro' or one of its dependencies. An attempt was made to load a program with an incorrect format.
=== Pre-bind state information ===
LOG: DisplayName = CustomConfigurationSectionRepro
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: CustomConfigurationSectionRepro | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more     information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/vtran/Documents/Visual Studio 2015/Projects/Playground/CustomConfigurationSectionRepro/
LOG: Initial PrivatePath = C:'Users'vtran'Documents'Visual Studio 2015'Projects'Playground'CustomConfigurationSectionRepro'bin
Calling assembly : (Unknown).

我一直在四处寻找,但找不到解决办法。名称空间和程序集名称似乎是正确的。customconfigurationsectionrep .dll也存在于bin文件夹中。

任何帮助都将非常感激。

更新:如果我将主机服务器从IIS Express更改为本地IIS,则应用程序运行正常。我还是不明白为什么。

尝试在web配置中创建自定义配置部分时,无法加载文件或程序集

我觉得应该是

<section name="customConfigurationSection" type="CustomConfigurationSectionRepro.ConfigHandlers.CustomConfigurationSection, CustomConfigurationSectionRepro" allowLocation="true" allowDefinition="Everywhere" />

类名后加逗号和DLL名

它看起来像您的web中的configSection条目。配置需要是装配合格的。

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="customConfigurationSection" type="CustomConfigurationSectionRepro.ConfigHandlers.CustomConfigurationSection" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>

type的行需要在类型名后面用逗号和程序集名称指定程序集,如下所示:

CustomConfigurationSectionRepro.ConfigHandlers.CustomConfigurationSection, CustomConfigurationSectionRepro

让你的配置元素看起来更像这样:

<section name="customConfigurationSection" type="CustomConfigurationSectionRepro.ConfigHandlers.CustomConfigurationSection, CustomConfigurationSectionRepro" allowLocation="true" allowDefinition="Everywhere" />