Providing a custom IConfigurationRoot to WebApplicationBuilder #64994
Replies: 4 comments
-
|
You don’t pass a custom IConfigurationRoot into WebApplicationBuilder anymore in .NET 10. Instead, you extend the configuration after creating the builder. WebApplicationBuilder owns configuration now. You modify its sources; you don’t replace it with your own IConfigurationRoot. The supported pattern is: Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
|
@vst121 : Only solves the easy mode version of the problem so no. It would only work if the IConfigurationBuilder from WebApplication.CreateBuilder were empty. |
Beta Was this translation helpful? Give feedback.
-
|
You’re right, that only addresses the “easy mode” where extending the existing pipeline is sufficient. The real issue is that WebApplicationBuilder does not start with an empty IConfigurationBuilder, and there’s no supported way to inject a pre-built IConfigurationRoot anymore. The intent in .NET 8+ / 10 is that configuration is owned by the host and only mutated via sources. If you need full control over provider ordering (e.g. filesystem → custom DB provider with filtering → env vars), the supported pattern is to clear and rebuild the sources explicitly: That gives equivalent control to CreateEmptyBuilder() without bypassing the host. There still isn’t a way to replace the configuration root, but this is the intended way to rebuild the pipeline while preserving hosting behavior. |
Beta Was this translation helpful? Give feedback.
-
|
@vst121 : Well that would be a research project to determine if Sources.Clear() is actually reasonable or a disaster waiting to happen. We're currently using CreateEmptyBuilder() to start with the empty configuration. Making dotnet run work took ages but was possible in the end. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
On upgrade to .NET 10; the old builder is obsolete and needs to be changed out.
On changing it out I stopped at:
The code wants to pass in a custom IConfigurationRoot to WebApplicationBuilder and that doesn't seem to be possible.
This is the easy mode version of the problem. It looks like there's a one-line solution to add the extra file if it exists; but there actually isn't. In the next step I will encounter a customer IConfigurationProvider and some filter code that ensures that
#64993 is tangentially related (discovered on researching how to fix this), but fixing that is completely orthogonal to the issue before me.
This can also be approached from the opposite direction: starting with CreateEmptyBuilder(); make the template in
dotnet new mvcrun.#48811
Beta Was this translation helpful? Give feedback.
All reactions