gpt4 book ai didi

asp.net-mvc - 我可以在 Azure 函数连接字符串部分中使用测试连接字符串和生产连接字符串吗?

转载 作者:行者123 更新时间:2023-12-03 02:53:22 25 4
gpt4 key购买 nike

我在下面有这个 Azure 函数,它有一些从我的 Azure 函数设置中的连接字符串部分提取的静态变量。从下面的代码中您可以看到我只为每个 key 和/或数据库提取一个连接字符串。每个都与我的 Azure 函数设置区域中的一个值相关,如下图所示。

但现在我希望能够指定一个测试字符串和一个生产字符串!如何在不进入 Azure 设置并手动更改隐藏字符串值的情况下执行此操作?我应该使用像 #if DEBUG 这样的预处理器语句

namespace Yogabandy2017.StripeWebhook
{
public static class StripeWebhook
{
static string YbDatabaseConnectionString;
static string SendGridApiKey;
static string StripeSecret;
static string StripeApiKey;

static StripeWebhook()
{
SqlProviderServices.SqlServerTypesAssemblyName = typeof(SqlGeography).Assembly.FullName;
YbDatabaseConnectionString = ConfigurationManager.ConnectionStrings["YogaBandyDatabase"].ConnectionString;
SendGridApiKey = ConfigurationManager.ConnectionStrings["SendGridApiKey"].ConnectionString;
StripeSecret = ConfigurationManager.ConnectionStrings["StripeSecret"].ConnectionString;
StripeApiKey = ConfigurationManager.ConnectionStrings["StripeApiKey"].ConnectionString;
}

[FunctionName("StripeWebhook")]
public static async Task<object> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, TraceWriter log)
{
return req.CreateResponse(HttpStatusCode.OK, new
{
greeting = $"Everythings ok!"
});
}
}
}

enter image description here

最佳答案

首先注意提示

Connection strings should only be used with a function app if you are using entity framework. For other scenarios use App Settings.

即使您的代码也能正常工作,我的建议是在连接字符串上方的应用程序设置部分中添加键,并使用ConfigurationManager.AppSettings["settingName"] 阅读。

对于您的问题,请考虑使用 slots (功能的预览功能)。

  1. 创建一个测试槽,它默认从生产槽复制设置。根据需要修改设置。

    enter image description here

  2. 将设置修复为 slot settings (测试槽和生产槽)。 enter image description here

  3. 在测试槽中进行测试后,交换两个槽以使用生产设置。 enter image description here

如果您不想使用插槽,请将具有不同前缀的测试和生产设置(例如 test_SendGridApiKeyprod_SendGridApiKey)添加到应用程序设置中。然后添加额外的 prefix 设置来确定读取哪些键,当然我们需要手动修改 prefix

例如,添加三项设置、两个键:test_SendGridApiKeyprod_SendGridApiKey,以及一个值为 testprefix >.

然后在函数代码中,我们调用下面的代码来获取测试 key

var prefix = ConfigurationManager.AppSettings["prefix"];
var settingName = "SendGridApiKey";
var sendGridApiKey = ConfigurationManager.AppSettings[$"{prefix}_{settingName}"]`;

一旦我们在应用程序设置中将前缀更改为prod,我们就会获得生产 key 。

关于asp.net-mvc - 我可以在 Azure 函数连接字符串部分中使用测试连接字符串和生产连接字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54679791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com