Azure Functions 不发布 appsettings.prod.json 文件

Azure Functions don#39;t publish appsettings.prod.json file(Azure Functions 不发布 appsettings.prod.json 文件)
本文介绍了Azure Functions 不发布 appsettings.prod.json 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的 .NET Core 控制台应用程序部署到 Azure Function,因此我正在使用 appsettings.json 文件,因为我们现在没有时间将其更改为 local.settings.json 文件.

I'm deploying my .NET Core Console app to Azure Function, and for this reason I'm using appsettings.json files because we don't have time to change it to local.settings.json file right now.

通过 VSCode 在 Azure Function 中发布应用后,/azure-functions-host/ 目录中存在 appsettings.json 和 appsettings.dev.json,但 appsettings.prod.json 中不存在.

After publishing the app in Azure Function through VSCode, appsettings.json and appsettings.dev.json exists in the /azure-functions-host/ directory, but not the appsettings.prod.json.

我在依赖项目中有这些复制属性:

I have these copy properties in a dependent project:

<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'.
   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 函数:

My LoadConfig function:

        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 选项和其他东西.

What this happens? Why just this file isn't published? I've tried a lot of things like CopyToPublishDirectory = Always option and other stuff.

我已经在图像/主机中搜索了 appsettings.prod.json 文件,但没有找到.

I've searched for the appsettings.prod.json file inside of the image/host and I haven't found it.

推荐答案

ItemGroup中使用ResolvedFileToPublish可以解决你的问题.

以下是我的测试步骤.

Use ResolvedFileToPublish in ItemGroup can solve your issues.

Below is my test steps.

第 1 步.

我在本地创建 appsettings.dev.jsonappsettings.prod.json,如下所示.

I create appsettings.dev.json and appsettings.prod.json in my local like below.

第 2 步.

在您的 <projectname>.csproj 文件中添加以下设置.

Add below settings in your <projectname>.csproj file.

示例代码.

<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 站点中的文件.

Check files in scm site.

第 4 步.

检查我的测试函数网址.

Check my test function url.

我的测试代码.

    [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:homesitewwwrootazure-functions-host","appsettings.dev.json"));
        }else{
            try
            {
                jsonString = File.ReadAllText(Path.Combine(@"C:homesitewwwrootazure-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);
    }

这篇关于Azure Functions 不发布 appsettings.prod.json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)