-2
  • Created a new "WPF Application" .NET 6.0 project There creating classical Application Settings was easy in project->properties->Settings->"Create or open application settings"
  • Observed: the project gets a new folder "Properties" which has a yellow Folder icon with an additional black wrench symbol, okay
  • It contains a new item Settings.settings that can get edited via classical Settings Designer looking like it used to look in .Net 4.8, and a new App.config XML file is getting created automatically in the project's root folder which also looks like it used to in .Net 4.8, okay

Now the same procedure can apparently only be done manually in

  • a new "Class Library" project being added in the same solution where I would want to use that Properties.Settings / app.config feature pack for storing a DB connection string configurably:
  • the new sub-project does not seem to have a "Settings" option in the project Properties dialog (as opposed to a .Net4.x would have had)
  • the new Properties folder and new Settings file can be created successfully there too manually as described in Equivalent to UserSettings / ApplicationSettings in WPF .NET 5, .NET 6 or .Net Core
  • but doing a "Rebuild solution" gives an

Error CS1069 The type name 'ApplicationSettingsBase' could not be found in the namespace 'System.Configuration'. This type has been forwarded to assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. ClassLibrary1 C:\Users\Stefan\source\repos\WpfCorePropertiesSettings\ClassLibrary1\Properties\Settings.Designer.cs 16 Active

  • as a next step adding NuGet package "System.Configuration.Abstractions" to the Class Library project cures the symptom, "rebuild solution" makes the error disappear.

TLDNR, actual question: is that sequence an acceptable solution or a kludge to avoid?

To me the NuGet package description does not sound as if the package was made for that purpose, and I have not heard the maintainers' names before (which might or might not matter?) https://github.com/davidwhitney/System.Configuration.Abstractions

TIA

PS: "Class Library" Project Template

"Class Library" Project properties

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28

2 Answers2

0

Maybe I don't understand something... Why create "Equivalent to UserSettings"?

My configuration is Win10+VS2022. I am creating a WPF .Net6 project. I go to the "Project Properties" menu. In the menu of the project properties tab (column on the left) there is an item Options. When selected, if the settings have not yet been created, there will be a small comment and a link to "Open or create application settings".

Unfortunately, I have Russian localization, so the screenshots are with Russian names.

enter image description here

enter image description here

enter image description here

Addition

But an additional "Class Library" sub-project does not seem to have that Project Properties option in my En/US localization. Does it in yours?

These are the APP settings. Therefore, they do not make much sense in the library. But if you need to, you can just copy the class to the library and then set up the links you need.

To do this, type in the application code the line Properties.Settings.Default.Save();. Move the cursor to Settings and press the F12 key. enter image description here

You will be taken to the source code for the Settings class declaration. This code is generated by a code generator. enter image description here

After moving to, copy all the source code into a class in another project. After the migration, you may need to add references in the project, fix the namespace and add usings.

As for the parameters in the «Class Library» project, it probably depends on what type this library is. I have such settings in the «Class Library for WPF». enter image description here

But in Libraries for Standard - no. enter image description here

EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • That's indeed easier / more straight forward for the WPF "main" project part, thanks. But an additional **"Class Library"** sub-project does not seem to have that Project Properties option in my En/US localization. Does it in yours? – Stefan Wuebbe Nov 04 '22 at 07:38
  • Thank you, the Error CS1069 issue seems to persist though, at least the way I tried. And yes, I do want to have an Application scoped setting in a kind of legacy sub-project in an older solution. Same effect in a minimal-repro attempt with a newly created solution containing one WPF main project and one ClassLibrary sub-project – Stefan Wuebbe Nov 04 '22 at 09:04
  • @StefanWuebbe , **ClassLibrary* - What type, for what platform? – EldHasp Nov 04 '22 at 09:38
  • Project Template screenshot added at the bottom of the question, спасибо – Stefan Wuebbe Nov 04 '22 at 09:52
  • @StefanWuebbe , In order for the Parameters to appear in the project, it is necessary that the following lines be in the project file: ` true ` – EldHasp Nov 04 '22 at 18:19
  • Great research, that works (together with a `net6.0-windows`) – Stefan Wuebbe Nov 05 '22 at 10:57
  • The sad part for me currently seems to be that that project is an `EF Core` "...Data" project being used by different UI projects, i.e. currently one `wpf` and one `asp.net core` project, hence itself not really meant to "UseWPF" at all – Stefan Wuebbe Nov 05 '22 at 11:02
  • And even more sad that I had not mentioned that point in the Question before, thanks a lot anyway :) – Stefan Wuebbe Nov 05 '22 at 11:03
0

In the meantime I'm happy with a custom "AppSettings.json" approach.

After removing the previously described "classical app.config" approach, and after adding two NuGet packages:

<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />

... I created a custom Json file on "Class Library" (sub)project level in Visual Studio manually, and set its CopyToOutputDirectory property

<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

And added an 'IConfigurationBuilder` part:

using Microsoft.Extensions.Configuration;

namespace Xyz.Data
{
    internal class AppSettingsConfig
    {
        public AppSettingsConfig()
        {
            IConfigurationBuilder builder = new ConfigurationBuilder();
            _ = builder.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "AppSettings.Movies3Data.json"));

            var root = builder.Build();
            AttachedDb = root.GetConnectionString("AttachedDb")!;
        }

        public string AttachedDb { get; init; }
    }
}

And then made it a "Jon Skeet singleton"

    /// <summary>
    /// Singleton as described by Jon Skeet
    /// </summary>
    /// https://csharpindepth.com/Articles/Singleton
    internal sealed class AppSettingsConfigSingleton
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        private AppSettingsConfigSingleton()
        {
            log.Trace($"{nameof(AppSettingsConfigSingleton)} ctor is running");

            IConfigurationBuilder builder = new ConfigurationBuilder();
            _ = builder.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "AppSettings.Movies3Data.json"));

            var root = builder.Build();
            AttachedDb = root.GetConnectionString("AttachedDb")!;
        }
        static AppSettingsConfigSingleton() { }

        public string? AttachedDb { get; init; }

        public static AppSettingsConfigSingleton Instance { get { return Nested.instance; } }

        private class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly AppSettingsConfigSingleton instance = new();
        }
    }

And it "works well" by also reading JSON content just having been modified by admins at run-time. (Which would be the Entity Framework Core "localdb" location for the unit-of-work pattern in a multi-UI solution). Thanks again to you too, @EldHasp

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28