我正在将 .NET Core 控制台应用程序部署到 Azure Function,因此我使用 appsettings.json 文件,因为我们现在没有时间将其更改为 local.settings.json 文件。
通过 VSCode 在 Azure Function 中发布应用后,
/azure-functions-host/
目录中存在 appsettings.json 和 appsettings.dev.json,但不存在 appsettings.prod.json。
我在依赖项目中有这些复制属性:
<ItemGroup>
<None Include="..\..\appsettings.prod.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="..\..\appsettings.dev.json" Condition=" '$(Configuration)' == 'Debug' ">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="..\..\appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
例外:
The configuration file 'appsettings.prod.json' was not found and is not optional. The physical path is '/azure-functions-host/appsettings.prod.json'.\n at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at DataImoveis.Setup.SetupConfig.LoadConfig(IServiceCollection service) in /mnt/d/Cloud/dev/src/DataImoveis.Setup/SetupConfig.cs:line 26
我的LoadConfig功能:
public static void LoadConfig(IServiceCollection service)
{
string env = "dev";
if (!checkFunctionEnvironment(ref env))
{
checkIfRelease(ref env);
}
var currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var config = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(
"appsettings.json",
optional: false,
reloadOnChange: true
)
.AddJsonFile(
$"appsettings.{env}.json",
optional: false,
reloadOnChange: true
);
service
.AddDefaultAWSOptions(config.Build().GetAWSOptions())
.Configure<Settings>(config.Build());
}
这是怎么回事?为什么只有这个文件没有发布? 我尝试了很多东西,比如
CopyToPublishDirectory = Always
选项和其他东西。
我在图像/主机内搜索了 appsettings.prod.json 文件,但没有找到它。
ResolvedFileToPublish
中的ItemGroup
可以解决您的问题。以下是我的测试步骤。
步骤1.
我在本地创建了
appsettings.dev.json
和 appsettings.prod.json
,如下所示。
第 2 步。
在您的
<projectname>.csproj
文件中添加以下设置。
示例代码。
<ItemGroup>
<ResolvedFileToPublish Include="azure-functions-host/appsettings.prod.json">
<RelativePath>azure-functions-host/appsettings.prod.json</RelativePath>
</ResolvedFileToPublish>
<ResolvedFileToPublish Include="azure-functions-host/appsettings.dev.json">
<RelativePath>azure-functions-host/appsettings.dev.json</RelativePath>
</ResolvedFileToPublish>
</ItemGroup>
步骤 3.
检查scm站点中的文件。
第 4 步。
检查我的测试函数网址。
我的测试代码。
[FunctionName("HttpTriggerCSharp1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
///////////////////////////////////////////////////////////////////////
bool isLocal = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
var jsonString=string.Empty;
if(isLocal){
jsonString = File.ReadAllText(Path.Combine(@"C:\home\site\wwwroot\azure-functions-host","appsettings.dev.json"));
}else{
try
{
jsonString = File.ReadAllText(Path.Combine(@"C:\home\site\wwwroot\azure-functions-host","appsettings.prod.json"));
}
catch (System.Exception e)
{
jsonString=e.ToString();
throw;
}
}
string response = isLocal ? "Function is running on local environment." : "Function is running on Azure.";
///////////////////////////////////////////////////////////////////////
return new OkObjectResult(jsonString);
}
您找到解决方案了吗?我面临同样的问题,并且标记的解决方案无法解决问题