0

I am trying to create my own custom configuration section with following:

Code in the web.config file:

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

Code in the custom config handler file:

namespace CustomConfigurationSectionRepro.ConfigHandlers
{
    public class CustomConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("fileName", IsRequired = false)]
        public string FileName
        {
            get
            {
                return (string)this["fileName"];
            }
            set
            {
                this["fileName"] = value;
            }
        }
    }
}

My web app is just a standard mvc web app from the Visual Studio MCV template. When I run it under development environment with IIS Express, I keep getting the error saying.

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).

I've been searching around but could not find the solution. The namespace and assembly name seem to be correct. The CustomConfigurationSectionRepro.dll also does exist in the bin folder.

Any help would be really appreciated.

UPDATE: If I change the host server from IIS Express to Local IIS, the application ran just fine. I still don't understand why.

4

2 回答 2

0

我认为它需要是形式

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

在类名之后使用逗号和 DLL 名称。

于 2016-08-09T19:21:38.950 回答
0

看起来configSection您的 web.config 中的条目需要符合汇编条件。

<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" />
于 2016-08-09T19:24:57.860 回答