1

When creating a project with any version .NET Core, debugging the application throws this error:

System.Configuration.ConfigurationErrorsException: 'Configuration system failed to initialize'

Inner Exception ConfigurationErrorsException: Unrecognized configuration section system.diagnostics. (C:\Users[UserName]\Documents\Visual Studio 2022\Projects\WinFormsApp1\WinFormsApp1\bin\Debug\net6.0-windows\WinFormsApp1.dll.config line 8)

However, when creating a project with any version of .NET Framework, the application debugs and runs perfectly fine, with no errors or exceptions. Both projects were created as fresh clean projects. Both projects were created exactly the same, the only difference being .NET Core or .NET Framework.

I'm using Visual Studio 2022, VB.NET, and creating a Windows Forms App (VB.) The versions of .NET Core I have installed: [3.1], [5.0], and [6.0]. the versions of .NET Framework I have installed: [2.0], 3.0], [3.5], [4.6], [4.6.1], [4.6.2], [4.7], [4.7.1], [4.7.2], and [4.8].

The steps for both projects are as follows: Open Visual Studio 2022 -> Create A New Project -> Select Windows Forms App (.NET Core - any version) or (.NET Framework - any version) -> Project Name: WindowsApp1, Location: [My Location], Solution Name: WindowsApp1, Place solution and project in same directory: (I've tried checked and unchecked), Framework: (I've tried any and all versions of .NET Core and .NET Framework) -> Create

Add Checkbox to empty form -> Checkbox name is default (CheckBox1 - I've tried changing the name) -> Project Properties -> Settings -> Add Setting - Name: cbState (I've tried different names), Type: Boolean, Scope: User, Value: True or False (I've tried both)

The code for both projects is exactly the same:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckBox1.Checked = My.Settings.cbState
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        My.Settings.cbState = CheckBox1.Checked
        My.Settings.Save()
    End Sub
End Class

I try to debug the application -> The .NET Framework version runs with no exceptions, and works as intended - saving the checkbox state of CheckBox1 and restoring it when re-launching the application.

The .NET Core version throws the exception error as listed above.

Again, all steps were executed in the exact same way, through the exact same process.

The Settings.Designer.vb file is highlighted ///here when the exception is thrown:

        <Global.System.Configuration.UserScopedSettingAttribute(),  _
         Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
         Global.System.Configuration.DefaultSettingValueAttribute("False")>  _
        Public Property cbState() As Boolean
            Get
                Return CType(Me("cbState"),Boolean) ///This is the highlighted line
            End Get
            Set
                Me("cbState") = value
            End Set
        End Property
    End Class
End Namespace

I've also tried to Synchronize the Settings.

I have a project that I've spent hours on, that was created using .NET Core 6.0. When I encountered it, I decided to try and reproduce this error with a new project, with the result being the same exception being thrown with .NET Core. I then tried using .NET Framework instead of .NET Core, and the issue was resolved. I encountered this error when trying to use My.Settings() in the project. I've searched online for an answer, but could not find anything. If anyone has any solution or if I'm missing something, please let me know. I would like to find a solution that does not require me to create a new project from scratch using .NET Framework instead of .NET Core, as I'd have to start from scratch (I'd assume.) All I'm trying to do is bind property settings to an object (such as a checkbox.) In VB.NET 6.0 Core it seems Microsoft removed the easily accessible (ApplicationSettings) binding from the properties window, so setting a binding to property has to be done manually, from what I've heard. Any help would be appreciated!

Barfunkle
  • 19
  • 6
  • 1
    You need to delete the specified section from the config file. – user18387401 Apr 05 '22 at 14:24
  • 2
    There's no such thing in .NET Core. `My.Settings` is just a shortcut for `Properties.Settings.Default` which in turn load settings from .NET Old's `app.config` file. There's no such thing in .NET Core. Instead of a single file with a fixed name .NET Core allows loading configurations from a multitude of sources including files in various formats, environment variable, databases and the command line. – Panagiotis Kanavos Apr 05 '22 at 14:26
  • @user18387401 .NET Core doesn't have a config file – Panagiotis Kanavos Apr 05 '22 at 14:26
  • .NET 6 is .NET *Core* 6, not .NET Framework 4+2. The config system changed completely from .NET old going to .NET Core 5 years ago. The old system was too cumbersome and anything but easily accessible - try managing different settings for dev and production. Or trying to unit test with different config settings. Never mind container deployments, where settings are typically provided through environment variables provided by the container engine/orchestrator – Panagiotis Kanavos Apr 05 '22 at 14:28
  • @PanagiotisKanavos, try creating a VB WinForms app project targeting .NET 6 and add a setting and then get back to me. There's a config file and it contains a section, which is why the error message provided refers to that section in that file. If that section is deleted then the error goes away. This is not my first rodeo. – user18387401 Apr 05 '22 at 14:34
  • In that case you know I'm right, that this is .NET Core 6, that the configuration system is completely different and ConfigurationManager is a manually installed legacy NuGet package now. Looks like Microsoft tried to make the transition easier for VB.NET programmers, but the change did happen and you'll have to migrate from the legacy ConfigurationManager to the Configuration middleware sooner or later. After all , all the NuGet packages you use work with `IConfiguration`, not ConfigurationManager. You probably already have to manage two settings files – Panagiotis Kanavos Apr 05 '22 at 14:42
  • And that means that it's not just `` but a *lot* of other sections that refer to types that no longer exist in .NET Core, or have no meaning – Panagiotis Kanavos Apr 05 '22 at 14:45

1 Answers1

1

Deleting the <system.diagnostics> section from the app.config file resolved the exception, and the application was then able to save and restore My.Settings() properly.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Barfunkle
  • 19
  • 6