I was playing around with hosted applications. But whenever i use “UserName” in appsettings.json it gets replaced by a different value. I had set it to “servicebus” but it contained “Llama”, is it somehow pulling my username from somewhere in windows?

If you are trying to use some “popular” keys for config, just create your own section in json and access it via config["section:UserName"], so you won’t have any collisions.

Or even better create class to contain options and bind it to configuration smth like services.Configure<ServiceBusOptions>(configuration["ServiceBus"]);
Oh duh! That seems so obvious now, thanks!

As others have mentioned, sounds like you have evironement variables or user secrets getting loaded overriding your config file. You can call Clear (I think it’s just clear could be clear providers) on your config builder when setting up in program.cs. Then you can setup only your config sources you choose.

Also, slightly unrelated but protip, don’t access IConfiguration directly. Make a POCO and call Configure<MyPoco> in your service registration. You can inject IOptions<MyPoco> in your classes and now you have a strongly typed, object with intellisense and if you change a name of your config you’ll know at build time where all the references are.

IConfiguration could be from user secrets or environment variables
USERNAME is an environment variable, is IConfiguration[“key”] not case sensitive then?

Look at how the configuration object is constructed in program.cs (or wherever you have it)
Most times it includes AddEnvironment() at the end, so will consume config files in order and then override values with the environment too.
So yes it sounds like you have this set by something else on your system. If you disable the environment call I bet it works (not a long term solution, just for debugging)

I tried the same thing in different applications, neither has any USERNAME setting
Perhaps it’s a ‘reserved’ keyword?

source