Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep a watch on the key which doesn't have children? #112

Open
TechGeeky opened this issue Aug 12, 2020 · 2 comments
Open

Keep a watch on the key which doesn't have children? #112

TechGeeky opened this issue Aug 12, 2020 · 2 comments

Comments

@TechGeeky
Copy link

TechGeeky commented Aug 12, 2020

I have a node (with data) in Consul like below:

Node: data/stage/process
Data: {"key1":value1,"key2":"value2","key3":"value3"}

I don't have any other children of "process" node in Consul. I just have json data for that node and that's all.

Now I keep a watch on that same node from below code. My idea is to keep a watch on the same node and get the value of "process" key in the code and whenever it is updated again from outside I want my watch to be triggered as well and then get the value of "process" key again on every reload whenever there is a change.

    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddConsul(
            $"data/stage/process",
            options =>
            {
                options.ConsulConfigurationOptions =
                    cco => { cco.Address = new Uri("some-url"); };
                options.Optional = true;
                options.PollWaitTime = TimeSpan.FromSeconds(5);
                options.ReloadOnChange = true;
                options.Parser = new SimpleConfigurationParser();
                options.OnWatchException = ctx => TimeSpan.FromSeconds(ctx.ConsecutiveFailureCount);
            })
        .AddEnvironmentVariables();
    Configuration = builder.Build();

With the above code when I run it, I get an exception as below:

'The key must not be null or empty. Ensure that there is at least one key under the root of the process or that the data there contains more than just a single value.'

But if I just keep a watch on "data/stage" node then it works fine without any error and I can get the value of "process" key in the code so my question is - can I not just keep a watch on the same node for which I want to get the value also?

@Choc13
Copy link
Collaborator

Choc13 commented Aug 12, 2020

Hi @TechGeeky,

From a quick glance at your code it looks like you've made a mistake with the Parser option you've chosen when calling AddConsul.

You've set the parser as a SimpleConfigurationParser when actually you're trying to parse a JSON node. If you remove this option it will default to JsonConfigurationParser and it should work fine.

Hope that helps.

@TechGeeky
Copy link
Author

TechGeeky commented Aug 12, 2020

Hi @Choc13

Thanks for your quick response. I removed that parser line and after that I don't see any error but when I try to get the value of "process" key I don't get any data now. This is the way I am getting value -

string val = configuration["process"]
// and then parse this json "val" to get data I need

But if I keep a watch on this node "data/stage" and then get value of "process" node using above code with SimpleConfigurationParser then it worked fine but if I remove SimpleConfigurationParser then it doesn't work as well.

Below is what I have noticed -

  • With node data/stage/process and removing parser line I don't see any value of "process" key being printed out.
  • With node data/stage/ and removing parser line I don't see any value of "process" key being printed out again.
  • But with node data/stage/ and keeping SimpleConfigurationParser parser line I see the value being printed out.

And I want to keep a watch on this node directly data/stage/process and then get the value of process key internally in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants