[英]Could not load the appsettings.json file when function deployed to azure
I am working on azure function app which references another project from core folder and that project contains the below class. 我正在使用azure函数应用程序,该应用程序从核心文件夹引用了另一个项目,并且该项目包含以下类。
In this I am trying to read appsettings.json file and it works fine locally but when it's deployed to azure portal it could not find this file and builder.Build() method throws FileNotFoundException. 在这种情况下,我尝试读取appsettings.json文件,并且在本地运行良好,但是当将其部署到Azure门户时,找不到该文件,并且builder.Build()方法抛出FileNotFoundException。
public static class ConfigurationSettings
{
public static IConfigurationRoot GetConfigurationRoot()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
return builder.Build();
}
}
Can anyone suggest me what wrong i am doing here and is there any other to include files in azure function apps? 谁能告诉我我在这里做错了什么,还有其他在Azure功能应用程序中包含文件的问题吗?
When Functions execute in Azure, GetCurrentDirectory
is set to D:\\Windows\\system32
. 当函数在Azure中执行时, GetCurrentDirectory
设置为D:\\Windows\\system32
。 There's no appsettings.json
in that folder, thus the exception. 该文件夹中没有appsettings.json
,因此是例外。
I hope you are able to set the directory from which the library is loading settings. 我希望您能够设置从中加载库的目录。 If so, you should add an extra parameter to your function of type ExecutionContext
and use its FunctionDirectory
property. 如果是这样,则应在ExecutionContext
类型的函数中添加一个额外的参数,并使用其FunctionDirectory
属性。 See docs . 参见docs 。
Of course, be sure to publish appsettings.json
to Azure as part of your deployment. 当然,作为部署的一部分,请确保将appsettings.json
发布到Azure。
I am now able to run the function app and read the appsettings.json file. 现在,我可以运行功能应用程序并读取appsettings.json文件。 here is how i achieved this: 这是我如何做到这一点:
Created Custom Configuration provider CustomConfigurationProvider, assigned type to static property SettingsType and also embedded appsettings.json file in the class library project: 创建了定制配置提供程序CustomConfigurationProvider,为静态属性SettingsType分配了类型,并且还在类库项目中嵌入了appsettings.json文件:
public class CustomConfigurationProvider : ConfigurationProvider, IConfigurationSource
{
public static Type SettingsType;
public override void Load()
{
var JsonString = GetResponse("appsettings.json");
var jsonObject = JObject.Parse(JsonString);
foreach (var settingsGroup in jsonObject)
{
var settingsGroupName = settingsGroup.Key;
foreach (var setting in settingsGroup.Value)
{
var jProperty = setting as JProperty;
Data.Add($"{settingsGroupName}:{jProperty.Name.ToString()}", jProperty.Value.ToString());
}
}
}
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return this;
}
protected string GetResponse(string resourceName)
{
var assembly = SettingsType.Assembly;
var resourceStream = assembly.GetManifestResourceStream(SettingsType.Namespace+'.'+ "appsettings.json");
using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
it is working for me but still looking for some better ways to do it. 它对我有用,但仍在寻找更好的方法来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.