Search code examples
azureazure-functionsazure-deploymentkudu

Azure Function searches File in a Wrong Place


I have an Azure Function (C# v3) that uses a local text file located at .\Assets\somefile.txt. The file has property Copy always and is properly seen when running on my local machine. However, when deployed to Azure Function app, the file is expectedly located in C:\home\site\wwwroot\Assets\somefile.txt but the running function does not see it and writes an error log message Could not find a part of the path 'C:\Program Files (x86)\SiteExtensions\Functions\3.1.3\32bit\Assets\somefile.txt'.

How can I make the function to see the file without hard coding the complete path to the file (it is different on my local machine anyway and I want to understand the actual root cause of the issue).


Solution

  • There are a couple ways to get the function execution root path. One is to add an ExecutionContext parameter in your function method and read the FunctionAppDirectory value.

    public static string Run(TimerInfo timer, ExecutionContext context)
    {
    
        var executionRoot = context.FunctionAppDirectory;
        var filePath = Path.Combine(executionRoot, "Assets", "somefile.txt");
        
        ...
    }
    

    Alternatively construct the base path from the HOME environment variable:

    public static string Run(TimerInfo timer)
    {
    
        var executionRoot = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
        var filePath = Path.Combine(executionRoot, "Assets", "somefile.txt");
        
        ...
    }