Custom Configuration Sections

2019-04-06 00:25发布

问题:

I am currently trying to implement a Custom Configuration Section in a project I am busy with and no matter what I try I keep getting the error below:

{"An error occurred creating the configuration section handler for pageAppearanceGroup/pageAppearance: Could not load type 'Samples.AspNet.PageAppearanceSection' from assembly 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (E:\Three Nine Developments\lastfm\msdn\msdn\bin\Debug\Samples.Aspnet.vshost.exe.config line 6)"}

I have copied the code from this MSDN Artricle:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

I still get the same error.

I have tried the all the advice/guide in the following articles but to no avail.
http://www.evanclosson.com/devlog/bettercustomerrorsinaspnetcustomconfigurationsection

http://geekswithblogs.net/akraus1/articles/64871.aspx

This must be something stupid that I am missing. I am running Vista, could that be a problem? some obscure security setting?

    <configuration>
  <!-- Configuration section-handler declaration area. -->
  <configSections>
    <sectionGroup name="pageAppearanceGroup">
      <section
        name="pageAppearance"
        type="Samples.AspNet.PageAppearanceSection"
        allowLocation="true"
        allowDefinition="Everywhere"
      />
    </sectionGroup>
    <!-- Other <section> and <sectionGroup> elements. -->
  </configSections>

  <!-- Configuration section settings area. -->
  <pageAppearanceGroup>
    <pageAppearance remoteOnly="true">
      <font name="TimesNewRoman" size="18"/>
      <color background="000000" foreground="FFFFFF"/>
    </pageAppearance>
  </pageAppearanceGroup>



</configuration>

回答1:

You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful!

Marc



回答2:

My guess is that you've copied the code, but you have different assembly names. Posting the config will help.

I would also fully quality your type in the config (something that sample doesn't show). Something like...

<section name="MySection" type="My.Assembly.Type, My.Assembly" />


回答3:

Please try with the following code:

<configSections>
    <sectionGroup name="pageAppearanceGroup">
         <section name="pageAppearance" type="Samples.AspNet.PageAppearanceSection,Samples.AspNet" allowLocation="true"         allowDefinition="Everywhere"       />
    </sectionGroup>     <!-- Other <section> and <sectionGroup> elements. -->
</configSections>  


回答4:

Please try with this

<configSections>
 <sectionGroup name="pageAppearanceGroup">
  <section name="pageAppearance"
           type="Samples.AspNet.PageAppearanceSection,Samples.AspNet"
           allowLocation="true"
           allowDefinition="Everywhere" />
 </sectionGroup>
 <!-- Other <section> and <sectionGroup> elements. -->
</configSections>  

Thanks, Vedi



回答5:

So it turns out that when you create a project in Visual Studio, it automatically defines a root namespace (the name of the project by default) for the project. Thus you must include that root namespace in the section type as well as any custom namespaces you defined within your settings class.

For example, in the case of the original poster a working configuration for them may have looked something like this:

<section name="MySection" type="ROOT_NAMESPACE.Samples.AspNet.PageAppearanceSection, NAME_OF_ASSEMBLY" />

Where ROOT_NAMESPACE and NAME_OF_ASSEMBLY are defined in the project properties as shown in this snapshot of my project.

In my particular case, I did not explicitly define namespaces in my project. Thus my section configuration setting just had the root namespace, the name of the settings class, and the name of the assembly, as such;

<section name="programSettings" type="ShipmentImport.ProgramSettings, ShipmentImport" />    

I know its a couple years late, but I hope it keeps somebody else from spending hours on this like I did.