0

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.

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>

Exception:

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

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());
        }

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

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

mayconfsbrito
  • 2,085
  • 4
  • 26
  • 45

1 Answers1

3

Use ResolvedFileToPublish in ItemGroup can solve your issues.

Below is my test steps.

Step 1.

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

enter image description here

Step 2.

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

enter image description here

Sample code.

<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>

Step 3.

Check files in scm site.

enter image description here

Step 4.

Check my test function url.

enter image description here

My test code.

    [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);
    }
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • How can I use SCM or File explorer using Azure Functions? I'm using the Pay-As-You-Go subscription. – mayconfsbrito Apr 24 '21 at 23:14
  • 1
    @mayconfsbrito Same as azure web app. – Jason Pan Apr 24 '21 at 23:18
  • 1
    @mayconfsbrito Before azurewebsites in url, add scm like my gif in answer. – Jason Pan Apr 24 '21 at 23:23
  • Thank you. It's woking putting the scm in the middle of the URL, but I don't have many options inside the Azure App Service, just two links to "App Settings" or "Deployments" and another Wiki Link. – mayconfsbrito Apr 25 '21 at 00:13
  • I'm starting to believe that this problem or maybe the limited kudu it's related with my account (it's an DEV profile account) or my function on linux. – mayconfsbrito Apr 25 '21 at 00:15
  • 1
    @mayconfsbrito Maybe, but you can get the running path of the current project through the code, and then create the corresponding folder locally to store the files you want, and then modify the csproj file according to my suggestions, which can achieve the same effect. – Jason Pan Apr 25 '21 at 09:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231569/discussion-between-jason-pan-and-mayconfsbrito). – Jason Pan Apr 25 '21 at 09:16
  • 1
    @mayconfsbrito If there is a problem with the product, raise a suuport ticket on portal. From a technical level, I can only provide a method to verify it, and the solution is definitely correct. If you want, I hope you can accept my answer, so that you can help more forum users. – Jason Pan Apr 25 '21 at 09:17