I have a command line app that uses .NET's dependency injection.
It works fine when called in its directory, like C:\GitHub\MyProject\bin\Debug\net9.0>MyApp
However, when I add it to the Path environment variable and call it from another location it fails:
C:\Users\me>MyApp
Unable to resolve service for type 'MyNamespace.IDependedOnService' while attempting to activate 'MyNamespace.Service'
Both MyNamespace.IDependedOnService and MyNamespace.Service are in my code, added in builder.ConfigureServices(..., but none of that code is in the stack trace.
In fact, the only non Microsoft code in the entire error stack is:
at Program.<Main>$(String[] args) in C:\GitHub\MyProject\Program.cs:line 83
Which is:
using var host = builder.Build();
After much digging it looks like it's picking up config settings in the secrets file, but missing any in C:\GitHub\MyProject\bin\Debug\net9.0\appsettings.config
Why does it only find the settings when run from the same directory?
How should .NET console apps be configured when run from the Path?
Pathand run from another location. Happens for all local config files types (json, xml or ini). Minimal repo is hard because thePathreg key and running it from another location are required.