使用 IAzureClientFactory 触发带有主机存储锁的计时器

问题描述 投票:0回答:1

我正在运行一个独立的应用程序并使用

Microsoft.Azure.WebJobs
SDK。我目前正在使用
Microsoft.Azure.WebJobs.Host.Storage
(v 5.0.0) 和
TimeTrigger
。为了注入 Azure SDK 客户端,我通常使用
Microsoft.Extensions.Azure
集成,如此处所述,它允许我注入命名客户端并控制用于每个客户端的
TokenCredential
。我想使用注入的客户端连接到主机存储,但似乎该包实际上并未使用它,而是使用
DefaultAzureCredential
(尝试 VS 凭据、托管身份凭据等)。

有没有办法让

Microsoft.Azure.WebJobs.Host.Storage
通过注入的
IAzureClientFactory
来解析主机存储的blob服务提供者?

程序.cs:

hostBuilder.ConfigureWebJobs(serviceBuilder =>
{
    serviceBuilder.Services.AddAzureClients(builder =>
    {
        builder.AddClient<BlobServiceClient, BlobClientOptions>((options, credentials, serviceProvider) =>
        {
            TokenCredential tokenCredential = new MyTokenCredential();

            return new BlobServiceClient(new Uri("https://<my-storage>.blob.core.windows.net"), credential: tokenCredential, options);
        });
    });

    serviceBuilder.AddAzureStorageCoreServices();
    serviceBuilder.AddTimers();
});

MyTokenCredential.cs:

public class MyTokenCredential : TokenCredential
{
    public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        Console.WriteLine("Test1");

        return default;
    }

    public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        Console.WriteLine("Test2");

        return default;
    }
}

函数.cs:

public async Task Invoke5MinFlowJobsAsync([TimerTrigger("00:05:00", RunOnStartup = false)] TimerInfo timerInfo)
{
    await _service.DoSomethingAsync("5 min");
}

appsettings.json:

"AzureWebJobsStorage": {
  "blobServiceUri": "https://<my-storage>.blob.core.windows.net"
},
.net azure azure-webjobssdk
1个回答
0
投票

您似乎正在尝试合并自定义令牌凭据,以便使用

IAzureClientFactory
在 Azure Functions 应用程序中访问 Azure 存储。然而,
Microsoft.Azure.WebJobs.Host.Storage
包并不直接促进这种身份验证方法。相反,它取决于 appsettings.json
local.settings.json
中的 AzureWebJobsStorage 设置来配置存储连接。

您可以在 Azure Web 应用程序环境变量中添加此设置 -

AzureWebJobsStorage:"connectionstring"
*。***

功能定时器触发代码:-

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace FunctionApp2
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            string containerName = "data";
            string blobName = "Test.txt";

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(blobName);

            BlobDownloadInfo download = await blobClient.DownloadAsync();

            using (MemoryStream ms = new MemoryStream())
            {
                await download.Content.CopyToAsync(ms);
                string blobContent = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                log.LogInformation($"Blob content: {blobContent}");
            }
        }
    }
}

enter image description here

我的

local.settings.json
:-

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=siliconstrg54;AccountKey=xxxxxxxxx3/TWrxxxxfaw==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.