-
Notifications
You must be signed in to change notification settings - Fork 442
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
When using a FunctionsStartup class, settings are not read from the correct directory #4517
Comments
See #4464 and Azure/azure-functions-core-tools#122 @ahmelsayed can probably give more background. You can use this to find the correct root
|
@espray is correct. The host sets up the configuration provider at the runtime location, to read its configuration, which differs from the function root. The approach mentioned above should work in the meantime, but we do have plans to support application level configuration files out of the box, so this setup wouldn't be required. |
Thanks :-) |
@fabiocav So what does that mean? |
@KalyanChanumolu-MSFT |
In creating a simple Azure Function using D.I. (a
FunctionsStartup
class) and ASP.NET Core 2 style configuration, I have run into issues with it not reading configuration from the correct directory.In my
Startup.Configure
method, I tried writing code like this:However, all of the configuration properties are always
null
. Drilling into it a bit with the debugger, it looks like the registeredIConfiguration
does in fact have aJsonConfigurationProvider
, but it is watching the wrong directory. It looks like theJsonConfigurationProvider
assumes thatAppContext.BaseDirectory
will be thebin
directory of the web app, but when the Functions app is starting up,AppContext.BaseDirectory
points at the directory containing the Functions framework bits (e.g.C:\Users\Jonathan Gilbert\AppData\Local\AzureFunctionsTools\Releases\2.22.0\cli\
).I have not found a non-ugly work-around for this yet.
Repro steps
AppSettings.cs
with a dummy setting in it:appsettings.json
with a value for this setting:appsettings.json
to be copied to the build output if newer.Startup.cs
with code to registerIOptions<AppSettings>
via the configuration subsystem:/api/test
endpoint.Expected behavior
The function should return the "foobar" value assigned to
DummySetting
inappsettings.json
.Actual behavior
When the function is called,
_settings.DummySetting
isnull
.Place a breakpoint on the configuration callback in the
Configure
method, and observe thatconfiguration
has no JSON configuration loaded, and that itsFileProvider
is looking at the wrong directory.Known workarounds
The closest thing I have found to a work-around is to obtain a reference to the
ScriptApplicationHostOptions
object used to initialize the parentScriptHost
. I haven't located a "proper" way to do this, so my "workaround" involves using reflection to extract the value of a private member (in this case, the_hostOptions
member of theHostJsonFileConfigurationProvider
configuration provider). This then provides aScriptPath
value that can be used to override theFileProvider
for aConfigurationBuilder
and thereby explicitly load the correctappsettings.json
file. I then use thisIConfiguration
object instead of the registered one when resolvingIOptions<AppSettings>
:This is an extremely poor "workaround", owing to the dependency on a private member.
The text was updated successfully, but these errors were encountered: