1
votes

I have created a Azure Function, which is having a dependency on ini file.

 public class DataProcessingFunction
 {
    FunctionName("DataProcessingFunction")]
    public async Task Run([EventGridTrigger]EventGridEvent eventGridEvent,ILogger log)
    {
        string iniFolderPath = $@"{Directory.GetCurrentDirectory()}\Ini\";
        string iniFileName = "Sample.ini";
        var iniConfig = FileManager.ReadFile(iniFolderPath, iniFileName);
    }
 }

I have selected Copy if never option in Visual Studio while publishing the code to Azure function Also, I have tried selecting Embedded Resource. But I am not able to find the file

enter image description here

I get an exception

File not found.

Add/Upload option in Azure portal is disabled because I am publishing the function from Visual studio

Question: Do I need to upload file to blob and then refer it in a code?

2
Function is deployed to azure, it is actually deployed to kudu, you modify Sample.ini to Copy if newer, then in theory it should be able to successfully transfer to kudu's d drive. Check if you have this file in kudu. I think it should have uploaded successfully. - Cindy Pau
I think it should upload successfully. Is there Sample.ini under D:\home\site\wwwroot> in your kudu? Please check. - Cindy Pau
yes, you are right, I can see it under wwwroot. So what path I should use it in code? - kudlatiger
What is the internal logic of FileManager.ReadFile, or which package is this method in ? Is this a method wrote by yourself? - Cindy Pau
Yes, internally it uses StreamReader to read the file by passing file path. - kudlatiger

2 Answers

2
votes

The final conclusion is that the file was successfully uploaded, the problem is that an error occurred while reading the path. It seems that using Directory.GetCurrentDirectory() in Azure is not reliable.

I just tried it and Directory.GetCurrentDirectory() got the wrong path in azure (I printed it out and it showed "D:\Program Files (x86)\SiteExtensions\Functions\2.0.12961\32bit", This is obviously not the current folder), and eventually it fails to find the Sample.ini file. Since the function is your own, you can set the path to something like "D:\\home\\site\\wwwroot>\\Ini\\Sample.ini". This should read the Sample.ini file.

1
votes

Here is the way to do it.

ExecutionContext context; // You can modify your function method to take an additional parameter of type ExecutionContext

public static async Task Run(<.... Other parameters>, ILogger log, ExecutionContext context)

Then build the path by combining function directory, "templates" is the directory I have in the function app project which is deployed along with other code, emails.html is file within templates (For each of these files you have to set, copy-always or copy-if-newer in the properties.). Instead of templates you can have your .ini file.

string templatePath = Path.Combine(context.FunctionAppDirectory, "Templates", "emails.html");