Where to place the Settings for External *.dll to run VS code Locally

Viewed 61

I am using an Azure Function I created with VS Code and added an external dependency (it's a Class Library created in VS 2022 *.dll). The External dependency requires me to have a connection string to an Azure SQL Server to work accordingly. I want it, to run locally to determine the outcome of the process, but I am unable to do it so.

I have added the dependency in the *.csproj as:

<ItemGroup>
    <Reference Include="HostServiceClassLib">
      <HintPath>..\..\..\..\..\..\HostServiceClassLib.dll</HintPath>
    </Reference>
  </ItemGroup>

What I did so far,

In many places from VS Code, I added the settings namely JksbMsSqlConnection. Thus, so far included the following locations.

  1. Folder > local.Settings.json
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "ConnectionStrings": {
    "JksbMsSqlConnection": "xxxxxxxxxxxx"
  }
}
  1. Folder > vscode > settings.json
    {
      "azureFunctions.deploySubpath": "bin/Release/net6.0/publish",
      "azureFunctions.projectLanguage": "C#",
      "azureFunctions.projectRuntime": "~4",
      "debug.internalConsoleOptions": "neverOpen",
      "azureFunctions.preDeployTask": "publish (functions)",
      "JksbMsSqlConnection": "xxxxxx"
    }
  1. Folder > Properties > launchSettings.json
    {
      "profiles": {
        "CDSFileComposerFunc": {
          "commandName": "Project",
          "commandLineArgs": "--port xxx",
          "launchBrowser": false,
          "environmentVariables": {
            "JksbMsSqlConnection": "xxxxxx"
          }
        }
      }
    }

I even added the prefix as SQLAZURECONNSTR_ but still no luck in all places above.

However, if I deploy to Azure and have the Settings under Azure Function > Configuration > Connection String it works!

enter image description here

My question is, for me to run it locally where do I need to place this connection string set? and what extra settings do I need to do?

Thank You,

1 Answers

My question is, for me to run it locally where do I need to place this connection string set? and what extra settings do I need to do?

You have to place the Connection strings in local.settings.json of the Azure Functions projects and that should be called in the C# Function Code.

I have taken sample code from the MS Doc and used the similar syntax (connection strings) you have given in the Question:

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "ConnectionStrings": {
    "JksbMsSqlConnection": "Server=tcp:<server_name>.database.windows.net,1433;Initial Catalog=<db_Name>;Persist Security Info=False;User ID=<sqlserver_username>;Password=<server-password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  }
}

Call the SQL Connection String from the C# function code:

var str = Environment.GetEnvironmentVariable("ConnectionStrings:JksbMsSqlConnection");

enter image description here

Related