2

I've been working with functions with Azure, I've built a very simple Http Function locally by following the example linked here, the only difference is I've defined a User table instead of a Todo table

Everything works as expected locally, I'm able to post and get.

However, when deploying the function and trying to make a POST request I see the following within the logs:

Executed 'User' (Failed, Id=5df9dffe-eedf-4b11-aa10-54fda00992b0, Duration=1ms)System.ArgumentNullException : Value cannot be null. (Parameter 'connectionString')

I've checked the SQL Server to ensure it's accessible by other Azure Services just encase that was causing a problem, but I can confirm it's set to allow.

I have found this question, I've gone through the steps and checked against mine and I can confirm my Function App configuration does have the AzureWebJobsStorage connection string.

I'm not 100% sure why this would be happening due to my lack of knowledge of functions at the moment, have anyone else experience this? if so how did you resolve it?

Update

After further testing, it seems the error is coming from my Startup class,

class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        string connectionString = Environment.GetEnvironmentVariable("SqlConnectionString");
        builder.Services.AddDbContext<ApplicationDbContext>(
            options => SqlServerDbContextOptionsExtensions.UseSqlServer(options, connectionString));
    }
}

Upon deployment, connectionString variable is null.... not sure why though.

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141

3 Answers3

1

If you want to use the Connection Strings section.

Add the "ConnectionStrings":{} section to your local.settings.json file then add your connection string

{
...
  "ConnectionStrings": {
    "MyConnectionString": ""
  }
}

Then you need to set the connection string in the Settings section of the Function App in the Azure Portal.

enter image description here

The scroll down to the Connection Section

enter image description here

And add a new connection string. Make sure it has the same name as you connection in the local.settings.json file.

DFord
  • 2,352
  • 4
  • 33
  • 51
  • It's weird, I've added the connection string into the place mentioned above. The connection string is called SqlConnectionString however I still get the error message mentioned in my question, saying connectionString can't be null – Code Ratchet Jan 10 '21 at 02:01
  • 1
    Did you try using `ConfigurationManager .ConnectionStrings["MyConnectionString"].ConnectionString;` If you put your connection string in `Values"` section of `local.settings.json` then your code will be looking for it in the setting section, not the connection string section – DFord Jan 10 '21 at 02:33
  • yeah I added the connection string inside the `values` section of the `local.settings.json` file – Code Ratchet Jan 10 '21 at 06:47
1

Yes, you can not get it because you didn't set it in the configuration settings.

enter image description here

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
0

Your question isn't 100% clear if this is happening locally (as you refer to local.settings.json) or when deploying. If this occurs when deploying, changing your local.settings.json file will not help, unfortunately.

You will need to add the Application Setting within the Azure Portal (located under Settings -> Configuration -> Application Settings -> New application setting).

You will need to save the application setting, and then restart the Azure Function instance for the changes to reflect.

Check out https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal

0if
  • 1