aspnet / MicrosoftConfigurationBuilders

Microsoft.Configuration.Builders
MIT License
118 stars 61 forks source link

Visual studio throws error when starting up project #214

Closed daviddejaeger closed 1 year ago

daviddejaeger commented 1 year ago

Hello,

I'm trying to implement a custom key/value configuration builder but I keep getting an error when starting up the project: afbeelding

The connectionStrings section in my web.config looks like this: `

` I also added the section for the configBuilders in my web.config:

<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>

My configBuilders section itself looks like this: `

`

For simplicity I just used the methods for the sample code (In reality it will go an external http source):

`public class ConfigserviceConfigBuilder : KeyValueConfigBuilder { public string ServiceUrlEnvironmentVariableName { get; set; } public string UserEnvironmentVariableName { get; set; } public string PasswordEnvironmentVariableName { get; set; } public string YamlFile { get; set; }

    private Dictionary<string, string> _allValues = new Dictionary<string, string>();

    public override ICollection<KeyValuePair<string, string>> GetAllValues(string prefix)
    {
        return new Dictionary<string, string>() { { "one", "1" }, { "two", "2" } };
    }

    public override string GetValue(string key)
    {
        return "Value for given key, or null.";
    }

    protected override void LazyInitialize(string name, NameValueCollection config)
    {
        base.LazyInitialize(name, config);

        ServiceUrlEnvironmentVariableName = UpdateConfigSettingWithAppSettings("serviceUrlEnvironmentVariableName");
        UserEnvironmentVariableName = UpdateConfigSettingWithAppSettings("userEnvironmentVariableName");
        PasswordEnvironmentVariableName = UpdateConfigSettingWithAppSettings("passwordEnvironmentVariableName");
        YamlFile = UpdateConfigSettingWithAppSettings("yamlFile");
    }
}`

I had a previous error when implemented the class inside the web-project. Then I had the error similar in #129 So I implemented this inside a separate project but now I get this error. When I continue and debug the class itself everything seems to work and I have no more errors. Also the config loads correctly.

I tried playing with the modes (Strict, Greedy, Expand) but its still the same problem (when Greedy the error in in 'GetValues'). Any ideas if something is 'wrong' in the setup or how to remove this message from popping up?

Best regards, David

StephenMolloy commented 1 year ago

Probably nothing "wrong" with your setup. The last Q in our FAQ talks about this issue with Visual Studio.

Based on the error text, I would guess that you're using version 2 of these builders - and I would also guess that VS is able to resolve at least your builder assembly. (It could still be a dependent assembly failing to resolve/bind, but I obviously don't see any dependent assemblies in your stripped-down sample code. So it's probably not that.)

You might try working off of the 3.0-preview2 packages, as they handle this 'OpenConfig()' scenario better. (Not perfect, but better.) Hopefully they get you unstuck from this point.

It's also likely a harmless error dialog. Safe to dismiss and continue - albeit rather annoying to even see it.

daviddejaeger commented 1 year ago

Hi Stephen,

Yes its version 2. We went by just dismiss and continue as everything is working fine when the app is running.

Best regards, David