Azure 函数发布
Azure Function publish
我尝试发布 Azure Function v.2,但出现错误:
The function runtime is unable to start.
Microsoft.Extensions.Configuration.FileExtensions: The configuration
file 'local.settings.json' was not found and is not optional. The
physical path is 'D:\Program Files
(x86)\SiteExtensions\Functions.0.12961bit\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
class 是:
[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
));
}
}
}
但是也看不懂
根据 docs:
By default, these settings are not migrated automatically when the project is published to Azure.
因此 local.settings.json 未包含在部署过程中。建议的方法是使用环境变量。解决方法是将文件命名为其他名称并从路径中读取文件并将其解析为 json 对象。
我尝试发布 Azure Function v.2,但出现错误:
The function runtime is unable to start. Microsoft.Extensions.Configuration.FileExtensions: The configuration file 'local.settings.json' was not found and is not optional. The physical path is 'D:\Program Files (x86)\SiteExtensions\Functions.0.12961bit\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
class 是:
[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
));
}
}
}
但是也看不懂
根据 docs:
By default, these settings are not migrated automatically when the project is published to Azure.
因此 local.settings.json 未包含在部署过程中。建议的方法是使用环境变量。解决方法是将文件命名为其他名称并从路径中读取文件并将其解析为 json 对象。