Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationBuilder not working in azure function

I'm using azure functions template project in VS2017 and choosing version 2 (beta). I published it unchanged and it worked.

I added nuget package Microsoft.Extensions.Configuration and wrote a single statement to initialize an instance of ConfigurationBuilder

public static class Function1
{
    [FunctionName("Function1")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {
        var cb = new ConfigurationBuilder();// <<<<< added line
        log.Info("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = new StreamReader(req.Body).ReadToEnd();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

With this code the function crashes with 500 - internal server error and I cannot find any reason.

Am I missing something? How do I access configuration information in Azure functions (v2.0)

Edit: Executing in Compute emulator throws System.Private.CoreLib: Exception while executing function: Function1. Aweton.Labs.AzureFunc1: Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

like image 939
Mx.Wolf Avatar asked Dec 17 '22 22:12

Mx.Wolf


2 Answers

I was having the same issue. You need following nuget packages to be installed for these to work elegantly You need to install Microsoft.Extensions.Configuration for this to init at first place

Additionally

  • SetBasePath() requires:
    • Microsoft.Extensions.Configuration.Abstractions
  • AddJsonFile() requires:
    • Microsoft.Extensions.Configuration.FileExtensions
    • Microsoft.Extensions.Configuration.Json
  • AddEnvironmentVariables() requires:
    • Extensions.Configuration.EnvironmentVariables
    • and possibly Microsoft.Extensions.Configuration.UserSecrets

Please refer below for more info https://www.koskila.net/how-to-access-azure-function-apps-settings-from-c/

like image 60
Yohan Avatar answered Dec 20 '22 10:12

Yohan


Disclaimer: I believe my Visual Studio 2017 environment is kept up to date by notification services as of 15.7.3.

Setup: Created fresh Azure Function project (Add New Project, Cloud, Azure Functions [ENTER], Azure Functions v2 Preview (.NET Standard)) Once VS completes restoring dependencies set new project as the startup point in the solution and press F5 to run local debug.

VS2017 starts dotnet and you can find a line like

http://localhost:7071/api/Function1

Navigate your browser to the URL to make sure the project works OK. Now stop the debugger.

To reproduce behavior:

Open nuget package manager and find Microsoft.Extensions.Configuration. At the time of writing it has stable latest version 2.1.0. Add this package to the project. Also, just for fun, add System.Data.SqlClient, (latest version 4.5.0) Now make sure your project really depends on the DLLs. For example write the following as the first statements in method Run;

        var cb = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
        var sc = new System.Data.SqlClient.SqlConnection();

Now Start debug again. (F5) The Functions host is still loading OK, but as you try refresh the browser the console window will report

[10.06.2018 15:37:28] Executing 'Function1' (Reason='This function was     programmatically called via the host APIs.',     Id=6804e02c-441a-4e62-b6a4-6b02154ec7fb) 
[10.06.2018 15:37:29] Executed 'Function1' (Failed, Id=6804e02c-441a-4e62-b6a4-6b02154ec7fb)
[10.06.2018 15:37:29] System.Private.CoreLib: Exception while executing function: Function1. FunctionApp-repro: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

Work around: Open nuget package manager and "Update" System.Data.SqlClient to version 4.1.0 And Microsoft.Extensions.Configuration to version 2.0.0

like image 45
Mx.Wolf Avatar answered Dec 20 '22 11:12

Mx.Wolf