Could not find a part of the path '/tmp/functions\standby\wwwroot/Templates/template1.html'. ReadAllText on Linux Azure functions app seems not to append correct root path.

Abhishek Chandel 25 Reputation points
2025-01-21T12:43:59.6333333+00:00

Following line of code is breaking for Azure function app in Flex Consumption plan with .net 8 Isolated worker, while its working perfectly on local windows machine.

Even though I am using Path.Join for platform specific separator.

Here templatePath is formed correctly and its value is "Templates/template1.html" where as the final path that ReadAllText is internally using contains invalid separator.

I was also reading file in a separate code which requires the execution root directory, but I am unable to find way to get the root path.

Templates and other files are part of the build process and are copied to the build location.

string 
templatePath
 = Path.Join(_configuration.GetSection(Constants.ConfigSections.EmailTemplate.Key)
                .GetSection(Constants.ConfigSections.EmailTemplate.DirectoryName).Value, templateName);
var html = File.ReadAllText(templatePath);
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,376 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,083 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,264 questions
0 comments No comments
{count} votes

Accepted answer
  1. Khadeer Ali 2,855 Reputation points Microsoft Vendor
    2025-01-21T13:41:04.7633333+00:00

    @Abhishek Chandel ,

    Welcome to Microsoft Q&A Platform!

    Thank you for bringing this to our attention. It seems that the issue arises due to differences in how file paths are resolved between local Windows environments and Azure Functions running on Linux in the Flex Consumption plan. While your code is forming the templatePath correctly, the root path may not be resolved properly in the Azure environment.

    1. Use Path.Combine: This could help the path separators are handled correctly across different platforms.
    2. Get the Root Path: You could use AppContext.BaseDirectory to get the root path of your Azure Function app. This will give you the directory where your application is running.

    Please refer the below documentation on the example:

    https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.basedirectory?view=net-9.0

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.