Keep a watch on the key which doesn't have children?
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?
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.
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/processand 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 keepingSimpleConfigurationParserparser 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.