如何覆盖azurewebjobsstorage的ConnectionsTring值为.NET Core函数应用程序中的新值

发布于 2025-02-10 03:34:09 字数 196 浏览 2 评论 0原文

我必须根据某种条件指定存储帐户连接属性,

例如:

if(condition=true)
[StorageAccount("ConnectionString1")]
else
[StorageAccount("ConnectionString2")]

我们是否有一些条件属性可以在.NET中实现此目标?

I have to specify the storage account connections attribute based on some condition

eg:

if(condition=true)
[StorageAccount("ConnectionString1")]
else
[StorageAccount("ConnectionString2")]

Do we have some conditional attributes to achieve this in .net?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2025-02-17 03:34:09

我为覆盖Azure函数应用程序存储帐户所做的解决方法之一是:

  1. 创建一个.NET Core 6 Azure函数应用程序和一个额外的存储帐户,其中两个存储帐户连接字符串都在我的函数应用程序中注明local.settings.json文件如下:
{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Get_Conn_String_from_AzureStorageAcc_AccessKeys",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureStorageAccount02": "<Get_Conn_String_from_AzureStorageAcc_AccessKeys>"
  }
}
  1. 要根据Azure函数的条件覆盖不同的存储帐户,我写了以下代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace KrishNet6CoreFunctionApp
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            if (name == "HariKrishna")
            {
                var constring1 = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                log.LogInformation("Function Execution logs will be stored in 1st storage account.");
            }
            else
            {
                var constring1 = Environment.GetEnvironmentVariable("AzureStorageAccount02");
                log.LogInformation("Function Execution logs will be stored in 2nd storage account.");
            }


                string responseMessage = string.IsNullOrEmpty(name)
                    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                    : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

”“在此处输入图像说明”

结果

“

如果参数名称值有所操作/活动:

“

One of the workarounds I did to override the Azure Function App Storage Accounts is:

  1. Created a .NET Core 6 Azure Function App and one extra storage account in which both storage account connection strings are noted in My Function App local.settings.json file like below:
{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Get_Conn_String_from_AzureStorageAcc_AccessKeys",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureStorageAccount02": "<Get_Conn_String_from_AzureStorageAcc_AccessKeys>"
  }
}
  1. To override the different storage account based on condition from the Azure Function, I have written the following code:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace KrishNet6CoreFunctionApp
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            if (name == "HariKrishna")
            {
                var constring1 = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                log.LogInformation("Function Execution logs will be stored in 1st storage account.");
            }
            else
            {
                var constring1 = Environment.GetEnvironmentVariable("AzureStorageAccount02");
                log.LogInformation("Function Execution logs will be stored in 2nd storage account.");
            }


                string responseMessage = string.IsNullOrEmpty(name)
                    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                    : 
quot;Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

enter image description here

Result:

IfCondTrueUse1stStorageAcc

If the parameter name value is different, then utilize the 2nd Storage account to store logs, perform any other operations/activities:

IfCondfalseUse2ndStorageAcc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文