c# - 在哪里放置外部 *.dll 的设置以本地运行 VS 代码

标签 c# azure visual-studio-code azure-functions

我正在使用通过 VS Code 创建的 Azure 函数,并添加了外部依赖项(它是在 VS 2022 *.dll 中创建的类库)。外部依赖项要求我有一个到 Azure SQL Server 的连接字符串才能相应地工作。我希望它在本地运行以确定该过程的结果,但我无法这样做。

我在 *.csproj 中添加了依赖项:

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

到目前为止我做了什么,

在 VS Code 的许多地方,我添加了设置,即 JksbMsSqlConnection。因此,到目前为止包括以下位置。

  1. 文件夹 > local.Settings.json
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "ConnectionStrings": {
    "JksbMsSqlConnection": "xxxxxxxxxxxx"
  }
}
  • 文件夹 > 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"
        }
    
  • 文件夹 > 属性 > launchSettings.json
  •     {
          "profiles": {
            "CDSFileComposerFunc": {
              "commandName": "Project",
              "commandLineArgs": "--port xxx",
              "launchBrowser": false,
              "environmentVariables": {
                "JksbMsSqlConnection": "xxxxxx"
              }
            }
          }
        }
    

    我什至将前缀添加为 SQLAZURECONNSTR_,但在上述所有地方仍然没有运气。

    但是,如果我部署到 Azure 并在 Azure 功能 > 配置 > 连接字符串下进行设置,它就可以工作了!

    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?

    您必须将连接字符串放置在 Azure Functions 项目的 local.settings.json 中,并且应在 C# 函数代码中调用该字符串。

    我从 MS Doc 中获取了示例代码并使用您在问题中给出的类似语法(连接字符串):

    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;"
      }
    }
    

    从 C# 函数代码调用 SQL 连接字符串:

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

    enter image description here

    关于c# - 在哪里放置外部 *.dll 的设置以本地运行 VS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73662927/

    相关文章:

    visual-studio-code - 如何在 visual studio 代码中集成 git bash

    c# - 通常假设 toString() 具有低成本是否安全?

    c# - 在 Umbraco 6.x 中使用内容服务 api 更新属性

    visual-studio-code - 为什么 jsconfig.json 文件报告 'not found' 问题?

    java - 任何人都可以做到这一点吗?如何将 VS-Code 多个扩展从一种语言特定扩展切换到另一种语言特定扩展?

    azure - 如何在成功部署 Azure 模板时从事件网格获取事件

    c# - 使用最小起订量测试存储库查询

    javascript - 为什么ajax调用不调用静态web方法?

    azure - 通过服务总线消息传递使用 Azure 服务结构的用例有哪些?

    Azure 管道循环