none
Config error refers to Version 4.0.0.0 after re-targeting to 2.0.0.0 even after changing all references in config files RRS feed

  • Question

  • I had a project targeting .NET Framework 4 which for a variety of reasons I had to downgrade to target .NET 3.5.  This produced a rash of System.Configuration.ConfigurationErrorsExceptions whenever the code tried to access My.Settings.  The error message was "An error occurred creating the configuration section handler for userSettings/ACal4.My.MySettings: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. I searched the solution for every instance of  'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and changed it to 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' but the error persisted.  I looked in the project file but found nothing relevant there.

    I tried recreating the project from scratch in NET 2 and upgraded to NET 3.5 and I still got the same error even though the new project had never referenced 4.0!  Could there be some kind of cache that I am missing?

    I am using VS2013.


    Mike VE

    Thursday, May 29, 2014 6:09 AM

Answers

  • Given your requirement for different versions of Outlook I'd recommend just switching your COM addin over to an out of process COM server.  You can then continue to use v2 (for compatibility reasons) and you will have your own config file.  This will solve all your problems.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, May 30, 2014 3:21 PM
    Moderator

All replies

  • Config sections have been added over the years so one problem with downgrading is that you often have unknown config sections.  For your particular error though the issue is that it is failing on the settings you defined in the Settings tab of the project properties.  Note that there are 2 assemblies referenced (one for the group and one for the section) so you need to change both of them to 2.0.0.0.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, May 29, 2014 2:38 PM
    Moderator
  • Thanks for the response.

    As far as I can see the app.config is correct.  Here is the config section.

    <configSections>        
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <section name="ACal4.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <section name="ACal4.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
       </configSections>

    I have just noticed that there is a squiggly underscore under the first <configSections> and the warning message for it is "The global element 'configuration' has already been declared."

    Here is the start of the settings section

    <userSettings>
            <ACal4.My.MySettings>
                <setting name="MyEmailAddress" serializeAs="String">
                    <value />
                </setting>
                <setting name="IsExchangeUser" serializeAs="String">
                    <value>False</value>
                </setting>
                <setting name="IsNewVersion" serializeAs="String">
                    <value>False</value>
                </setting>

    This is the error message

    Detailed technical information follows: 
    ---
    (Inner Exception)
    (Inner Exception)
    Date and Time:         30/05/2014 7:45:40 AM
    Machine Name:          CHILLBLAST
    IP Address:            fe80::901:bce6:292b:297c%10
    Current User:          GREENHILLSOFTWA\Mike
    
    Application Domain:    F:\VS Projects\Outlook Add-ins\ACal4\ACal4\bin\Debug\
    Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    Assembly Full Name:    System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    Assembly Version:      2.0.0.0
    
    Exception Source:      System.Configuration
    Exception Type:        System.IO.FileNotFoundException
    Exception Message:     Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
    Exception Target Site: GetTypeWithReflectionPermission
    
    ---- Stack Trace ----
       System.Configuration.TypeUtil.GetTypeWithReflectionPermission(host As IInternalConfigHost, typeString As String, throwOnError As Boolean)
           System.Configuration.dll: N 0036 (0x24) IL 
       System.Configuration.RuntimeConfigurationFactory.Init(configRecord As RuntimeConfigurationRecord, factoryRecord As FactoryRecord)
           System.Configuration.dll: N 0018 (0x12) IL 
       System.Configuration.RuntimeConfigurationFactory.InitWithRestrictedPermissions(configRecord As RuntimeConfigurationRecord, factoryRecord As FactoryRecord)
           System.Configuration.dll: N 0028 (0x1C) IL 
       System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(factoryRecord As FactoryRecord)
           System.Configuration.dll: N 0007 (0x7) IL 
       System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(configKey As String, isRootDeclaredHere As Boolean&)
           System.Configuration.dll: N 0094 (0x5E) IL 
    
    
    
    (Outer Exception)
    Date and Time:         30/05/2014 7:45:40 AM
    Machine Name:          CHILLBLAST
    IP Address:            fe80::901:bce6:292b:297c%10
    Current User:          GREENHILLSOFTWA\Mike
    
    Application Domain:    F:\VS Projects\Outlook Add-ins\ACal4\ACal4\bin\Debug\
    Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    Assembly Full Name:    System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    Assembly Version:      2.0.0.0
    

    The other thing that I had wondered about is that it could be something to do with the registry.  The project is a COM add-in for Outlook built with Add-in Express.  The whole thing has to be registered and that creates loads of Registry entries.  I wonder if there is a stray entry somewhere that is causing it to point at version 4.0.0.0 not 2.0.0.0

    Any comments will be gratefully appreciated.


    Mike VE

    Friday, May 30, 2014 7:21 AM
  • The COM addin part is starting to bring things into focus then.  Chances are your addin is a DLL right?  DLLs don't have config files (even though VS shows one, it isn't used at runtime).  Instead your addin would be using Outlook's config (which is most likely v4).  Hence you get an error about your v2 app not being able to load a v4 assembly. 

    There are several solutions.

    1) Make your COM object an out of process server instead of a DLL.  You can then target v2 and have your own config file.

    2) Move your project back to v4 (because you really don't want to target v2 anyway) and move any config entries you need into Outlook's config file.

    3) Leave your project as v2 and don't use ConfigurationManager to read the default configuration.  Instead use one of the Open methods on CM to open your custom configuration file.  Since you're loading your own config file and not Outlook it should work with your v2 code.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, May 30, 2014 1:54 PM
    Moderator
  • Thanks your reply, CoolDad. Yes the project is a COM add-in for Outlook so the config situation is not simple. The project was originally built in VS2008.  I have now migrated to VS2013 and targeted NET 4. The new VS meant meant updating Crystal Reports.  The current version of CR will only run against NET 4 if you add some lines to Outlook.exe.config.  This would would make deployment very complex as the add-ins support all version of Outlook from 2003 onward.  These can live in a number of different places and there may or may not be an outlook.exe.config there already.  That was the reason I went back to 3.5 so I could do a simpler deployment.

    I think your suggestions are beyond my current skill level and I have started rebuilding the solution from scratch targeting 3.5 from the beginning to avoid the problems.

    Thanks anyway.


    Mike VE

    Friday, May 30, 2014 2:51 PM
  • Given your requirement for different versions of Outlook I'd recommend just switching your COM addin over to an out of process COM server.  You can then continue to use v2 (for compatibility reasons) and you will have your own config file.  This will solve all your problems.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, May 30, 2014 3:21 PM
    Moderator