我正在尝试创建一种天蓝色的持久功能,但是很难找到有关该主题的一些常规指南。我已经设置了DI,并尝试读取该功能的设置,但是它崩溃了
我在VS 2019中设置了一个Azure Function项目,并添加了一个持久的Orchestrator Function模板。我从该类中删除了所有“静态”引用,并且所有这些似乎都可以正常工作,直到在启动文件中添加configurationbuilder为止。
谁能向我解释这应该如何工作或提供一些指导,在哪里可以找到有关持久功能配置的解释?在host.json,local.settings.json中应该包含什么内容,以及在门户网站上发布时的变化如何?
我的情况是这样。启动文件看起来像这样
使用Microsoft.Azure.Functions.Extensions.DependencyInjection; 使用Microsoft.Extensions.Configuration;
[assembly: FunctionsStartup(typeof(DurableFunctions.Startup))]
namespace DurableFunctions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var settings = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
}
}
}
host.json就是这样
{
"version": "2.0"
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
这是输出:
[11/8/2019 10:29:04 AM] A host error has occurred during startup operation '8b80bc94-2b98-408b-895f-c5697430acfd'.
[11/8/2019 10:29:04 AM] Microsoft.Azure.WebJobs.Extensions.DurableTask: Value cannot be null.
[11/8/2019 10:29:04 AM] Parameter name: hostConfiguration.
Value cannot be null.
Parameter name: provider
答案 0 :(得分:0)
您想改写 FunctionStartup 类(https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources)中的 ConfigureAppConfiguration 方法。
以下示例通过添加用户机密使文档中提供的示例更进一步。
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
FunctionsHostBuilderContext context = builder.GetContext();
builder.ConfigurationBuilder
.SetBasePath(context.ApplicationRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{context.EnvironmentName}.json", optional: true, reloadOnChange: false)
.AddUserSecrets(Assembly.GetExecutingAssembly(), true, true)
.AddEnvironmentVariables();
}
默认情况下,诸如 appsettings.json 之类的配置文件不会自动复制到Azure Function输出文件夹中。请务必查看文档(https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources),以对您的 .csproj 文件进行修改。还要注意,由于该方法附加了现有提供程序的方式,因此必须始终以 .AddEnvironmentVariables()结尾。
上找到有关Azure函数中配置的更深入的讨论