我正在使用通过 VS Code 创建的 Azure 函数,并添加了外部依赖项(它是在 VS 2022 *.dll 中创建的类库)。外部依赖项要求我有一个到 Azure SQL Server 的连接字符串才能相应地工作。我希望它在本地运行以确定该过程的结果,但我无法这样做。
我在 *.csproj 中添加了依赖项:
<ItemGroup>
<Reference Include="HostServiceClassLib">
<HintPath>..\..\..\..\..\..\HostServiceClassLib.dll</HintPath>
</Reference>
</ItemGroup>
到目前为止我做了什么,
在 VS Code 的许多地方,我添加了设置,即 JksbMsSqlConnection
。因此,到目前为止包括以下位置。
- 文件夹 >
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 功能 > 配置 > 连接字符串下进行设置,它就可以工作了!
我的问题是,对于我来说,要在本地运行它,我需要将该连接字符串集放在哪里?我需要做哪些额外设置?
谢谢,
最佳答案
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");
关于c# - 在哪里放置外部 *.dll 的设置以本地运行 VS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73662927/