配置文件 'config.json' 未找到并且不是可选的
The configuration file 'config.json' was not found and is not optional
我使用 Azure Functions v2
我尝试创建启动文件:
[assembly: FunctionsStartup(typeof(AzureAppDomainRegistration.Startup))]
namespace AzureAppDomainRegistration
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.AddJsonFile("config.json")
.Build();
var connString = config["ConnectionStrings:DataContext"];
builder.Services.AddDbContext<DataContext>(options => options
.UseLazyLoadingProxies()
.UseSqlServer(connString));
builder.Services.AddTransient<IActionsRegistrationInfo, EfActionsRegistrationInfo>();
}
}
}
文件 "config.json" 存在并且有选项始终复制:
<None Update="config.json">
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
但是当我启动 Azure Function(本地或发布到 Azure)时出现错误:
Microsoft.Extensions.Configuration.FileExtensions: The configuration
file 'config.json' was not found and is not optional.
怎么了?
在以后的 .net 核心版本中,使用 *.csproj 文件代替 project.json 文件。
您可以修改文件以获得所需的结果,方法是添加:
<ItemGroup>
<Content Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
在.AddJsonFile()
方法之前,添加这行代码:.SetBasePath(Environment.CurrentDirectory)
。然后在你的项目中,右击 "config.json" -> select 属性 -> 然后将 "copy to output directory" 设置为 "copy always".
请注意,我使用的是 IWebJobsStartup
而不是 FunctionsStartup
。
这是适合我的代码:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
//using WebJobsStartup instead of FunctionsStartup
[assembly: WebJobsStartup(typeof(FunctionApp20.Startup))]
namespace FunctionApp20
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
}
//using IWebJobsStartup instead of FunctionsStartup
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("config.json",true,true)
.Build();
}
}
}
我使用 Azure Functions v2 我尝试创建启动文件:
[assembly: FunctionsStartup(typeof(AzureAppDomainRegistration.Startup))]
namespace AzureAppDomainRegistration
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.AddJsonFile("config.json")
.Build();
var connString = config["ConnectionStrings:DataContext"];
builder.Services.AddDbContext<DataContext>(options => options
.UseLazyLoadingProxies()
.UseSqlServer(connString));
builder.Services.AddTransient<IActionsRegistrationInfo, EfActionsRegistrationInfo>();
}
}
}
文件 "config.json" 存在并且有选项始终复制:
<None Update="config.json">
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
但是当我启动 Azure Function(本地或发布到 Azure)时出现错误:
Microsoft.Extensions.Configuration.FileExtensions: The configuration file 'config.json' was not found and is not optional.
怎么了?
在以后的 .net 核心版本中,使用 *.csproj 文件代替 project.json 文件。 您可以修改文件以获得所需的结果,方法是添加:
<ItemGroup>
<Content Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
在.AddJsonFile()
方法之前,添加这行代码:.SetBasePath(Environment.CurrentDirectory)
。然后在你的项目中,右击 "config.json" -> select 属性 -> 然后将 "copy to output directory" 设置为 "copy always".
请注意,我使用的是 IWebJobsStartup
而不是 FunctionsStartup
。
这是适合我的代码:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
//using WebJobsStartup instead of FunctionsStartup
[assembly: WebJobsStartup(typeof(FunctionApp20.Startup))]
namespace FunctionApp20
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}
}
//using IWebJobsStartup instead of FunctionsStartup
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("config.json",true,true)
.Build();
}
}
}