简体   繁体   English

将功能部署到Azure时无法加载appsettings.json文件

[英]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.

相关问题 appsettings.json 和 azure function 通过定时器 - appsettings.json and azure function by timer 在 azure 部署的应用程序中更新 appsettings.json 值 - Updating appsettings.json values in azure deployed application 如何将 appsettings.json 文件添加到我的 Azure Function 3.0 配置? - How to add an appsettings.json file to my Azure Function 3.0 configuration? 使用 appsettings.json 时,Azure 函数无法初始化 Serilog 接收器 - Azure Function failing to initialise Serilog Sink when using appsettings.json 从 Azure 中的 KeyVault 更新 appsettings.json 值 Function - Updating appsettings.json values from KeyVault in Azure Function Azure应用程序设置不会覆盖我的appsettings.json文件值 - Azure Application Settings not overriding my appsettings.json file values Azure 函数在部署时未绑定到 appsettings - Azure Function not binding to appsettings when deployed 如何在已部署的Azure Web App中通过PowerShell访问appsettings.json? - How can I access appsettings.json via PowerShell in a deployed Azure Web App? Azure 批处理服务 - 找不到 appsettings.json - Azure Batch Service - appsettings.json not found 在azure webapp上应用appsettings.json - applying appsettings.json on azure webapp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM