Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

System.Configuration.ConfigurationErrorsException Help!...

$
0
0

Development: Prism 4.1
Technology: MEF

In trying to read and write to the appName.vhost.exe.config I'm having trouble reading the section after in adds the section.

I have an assembly called appName.Infrastructure that does all the common task for main app and all other assemblies.  The .Infrastructure has a (public interface IOdsDocumentSectionService) for all assemblies to use for read and write to the app configuration file.  The actual .cs in located in the main app in which provides this as a service.

All assemblies are only added to main app reference and all the assemblies have a reference to the .Infrastructure.  These assemblies are loadedOnDemand and will be reloaded during start-up only if they were loaded during the app session.

Position Assembly:
Description:responsible for adding what assembles have been loaded to the app config.

The subScribed event:

            eventAggregator.GetEvent<OdsConfigElementEvent>().Subscribe(OdsConfigElementEvent, ThreadOption.PublisherThread);


The subScribe event handler:

        private void OdsConfigElementEvent(OdsConfigElementArgs e)
        {
            OdsDocumentSection odsDocumentSection = this.GetOdsDocumentSection(e.SectionName);

            if (odsDocumentSection == null) return;
            odsDocumentSection.OdsElement.ModuleName = e.ModuleName;
            odsDocumentSection.OdsElement.ViewName = e.ViewName;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(e.SectionName);
        }


Logic for reading and creating the section:

namespace StockTraderRI.Services
{

.....
...
..

private OdsDocumentSection GetOdsDocumentSection(string sectionName) { OdsDocumentSection odsDocumentSection = new OdsDocumentSection(); //Configuration config; try { // Get the current configuration file. if(this.config == null) this.config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration; if (this.config.Sections[sectionName] == null) { // Create a custom section if it does // not exist yet. // Store console settings. odsDocumentSection.OdsElement.ModuleName = "none"; odsDocumentSection.OdsElement.ViewName = "none"; // Add configuration information to the // configuration file. config.Sections.Add(sectionName, odsDocumentSection); config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. // This makes the new values available for reading. ConfigurationManager.RefreshSection(sectionName); } odsDocumentSection = this.config.GetSection(sectionName) as OdsDocumentSection; // Create the custom section entry // in <configSections> group and the // related target section in <configuration>. if (odsDocumentSection == null) { this.config.Sections.Add(sectionName, odsDocumentSection); } } catch (ConfigurationErrorsException err) { Console.WriteLine("Using GetSection(string): {0}", err.ToString()); return null; } return odsDocumentSection; }

Notice the name space for this source which is located in a folder on the main app.

Here the error I'm getting from the ConfigurationErrorsException:

Using GetSection(string): System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for metaSection: Could not load type 'StockTraderRI.OdsDocumentSection' from assembly 'StockTraderRI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. (F:\Downloads\WPF\NatarODS\StockTrader RI\Desktop\StockTraderRI\bin\Debug\StockTraderRI.vshost.exe.Config line 4) ---> System.TypeLoadException: Could not load type 'StockTraderRI.OdsDocumentSection' from assembly 'StockTraderRI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
   at System.Configuration.MgmtConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)
   at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.ConfigurationSectionCollection.Get(String name)
   at StockTraderRI.Services.OdsDocumentSectionService.GetOdsDocumentSection(String sectionName) in f:\Downloads\WPF\NatarODS\StockTrader RI\Desktop\StockTraderRI\Services\OdsDocumentSectionService.cs:line 125


If you notice the "StockTraderRI.OdsDocumentSection" in the error message.  Well, this object file in located in the.Infrastructure and the nameSpace is "StockTraderRI.Infrastructure.Models".  Its being written to the app config in the "<configurationa>" as:

<configSections><section name="metaSection" type="StockTraderRI.OdsDocumentSection, StockTraderRI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /></configSections><metaSection><odsElement modulename="MetaModule" viewname="MetaListView" /></metaSection>


Here the object class .cs:

using System.Configuration;

namespace StockTraderRI.Infrastructure.Models
{
    // Define a custom section that is used by the application
    // to create custom configuration sections at the specified 
    // level in the configuration hierarchy that is in the 
    // proper configuration file.
    // This enables the user that has the proper access 
    // rights, to make changes to the configuration files.
    public class OdsDocumentSection : ConfigurationSection
    {
        // Create a configuration section.
        public OdsDocumentSection()
        { }

        // Set or get the OdsElement. 
        [ConfigurationProperty("odsElement")]
        public OdsConfigElement OdsElement
        {
            get
            {
                return ((OdsConfigElement)this["odsElement"]);
            }
            set
            {
                this["odsElement"] = value;
            }
        }
    }
}


How do I get this written properly so that is reads the correct nameSpace?  Its suppose to look like this:

<section name="metaSection" type="StockTraderRI.Infrastructure.Models.OdsDocumentSection, StockTraderRI.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />



Code is like a box of chocolates!...





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>