1

I have some variables which are going to be used by the business logic part of a function. Therefore, instead of adding them inside the appsetting.json file, I have added a separated file as variable.json Testing on my machine works but after deploy, it seems function can not find it. and I got an error: enter image description here

The properties for this file is like the below image. (The build action was None before, but nothing has been changed even by content)

enter image description here

and the below image shows how it looks like in root enter image description here

And because of that reason, any call the response will be "Function host is not running."

The code for reading this file (path = "Variables.json")

 private static List<Variable> GetVariables(string path)
    {
        string json = File.ReadAllText(path);
        var variables = JsonConvert.DeserializeObject<List<Variable>>(json);

        return variables;
    }

Does anyone have any clue why this is happening?

2
  • Can you show the code of how to use the variables.json? Commented Sep 21, 2020 at 9:32
  • @BowmanZhu I have updated the post with the code Commented Sep 21, 2020 at 9:46

1 Answer 1

2

Problem was because when we start Azure Function locally the file varibale.json is available by Directory.GetCurrentDirectory(), but published on azure portal it's Directory.GetCurrentDirectory() + @"\site\wwwroot"

To get the correct folder path you can use following code:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var path = System.IO.Path.Combine(context.FunctionDirectory, "varibale.json");
    // ...
}

For startup.cs, you can use the following code:

var executioncontextoptions = builder.Services.BuildServiceProvider()
    .GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executioncontextoptions.AppDirectory;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer! is it any way to get this on startup? this context is available through func runtime and not startup time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.