【问题标题】:Azure Function publishAzure 函数发布
【发布时间】:2020-02-04 05:32:48
【问题描述】:

我尝试发布 Azure Function v.2,但出现错误:

函数运行时无法启动。 Microsoft.Extensions.Configuration.FileExtensions:配置 找不到文件“local.settings.json”,不是可选的。这 物理路径是 'D:\Program Files (x86)\SiteExtensions\Functions\2.0.12961\32bit\local.settings.json'。

我有以下配置:

<None Update="local.settings.json">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>

我在 Kudu 中看到了这个文件。

如何解决?

添加:

我尝试创建另一个文件,名为config.json

{
  "FtpSettings": {
    "FtpServer": "ftp://address/out",
    "FtpLogin": "login",
    "FtpPassword": "pass"
  }
}

然后尝试阅读它:

        var config = new ConfigurationBuilder()
            .AddJsonFile("config.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        builder.Services.AddTransient<FtpService.IFtpService>(s => new FtpService.Core.FtpService(
            address: config.GetSection("FtpSettings:FtpServer").Value,
            username: config.GetSection("FtpSettings:FtpLogin").Value,
            password: config.GetSection("FtpSettings:FtpPassword").Value
            ));

所以,我的Startup 班级是:

[assembly: FunctionsStartup(typeof(FunctionAppEfsGetFilesFromFtp.Startup))]
namespace FunctionAppEfsGetFilesFromFtp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("config.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            builder.Services.AddTransient<FtpService.IFtpService>(s => new FtpService.Core.FtpService(
                address: config.GetSection("FtpSettings:FtpServer").Value,
                username: config.GetSection("FtpSettings:FtpLogin").Value,
                password: config.GetSection("FtpSettings:FtpPassword").Value
                ));
        }
    }
}

但也读不出来

【问题讨论】:

  • 你的函数中有启动类吗?
  • 可以显示你的函数启动类的代码吗?
  • @IvanYang 添加了

标签: azure azure-functions azure-function-app


【解决方案1】:

根据docs

默认情况下,当项目发布到 Azure 时,这些设置不会自动迁移。

所以 local.settings.json 不包含在部署过程中。建议的方法是使用环境变量。一种解决方法是将文件命名为其他名称并从路径中读取文件并将其解析为 json 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多