我正在尝试编写作为多租户工作的Azure函数。当clientId作为参数来自客户端时,我需要使用标签从Azure应用配置中提取其配置。
让我用伪代码来解释
我使用的是.Net 6 Azure函数隔离。我在Program.cs中进行了配置,如下面的代码所示;
new HostBuilder()
.ConfigureAppConfiguration(builder =>
{
string cs = "connection-string"; //which is actually comes from environment veriable
builder.AddAzureAppConfiguration(options =>
{
options.Connect(cs);
//options.Select(KeyFilter.Any, Environment.GetEnvironmentVariable("ClientId")); //It works that way, but I don't want to do it. I need to read the clientId while I receive the request
});
})
假设我们创建了一个中间件,发出了一个请求
public RequestMiddleware(){
//Here I need to inject some service
}
//Invoke method of middleware
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next){
HttpRequestData requestData = await context.GetHttpRequestDataAsync();
requestData.Headers.TryGetValues("client-id", out var clientId);
var id = clientId.First();
//And here, I need to configure App Configuration to use the id as a label. As I did in the Program.cs
//like this ; options.Select(KeyFilter.Any, id);
}
是否可以在请求生命周期中按标签加载配置?请记住,标准层中的Azure应用配置对请求有限制,即每小时3000个请求。这就是为什么每次加载AppConfiguration不是一个好主意。
1条答案
按热度按时间ggazkfy81#
你可以做的是使用一个静态字典来“缓存”,每当一个带有新客户端id的请求到达时,你就获取配置并将其添加到字典中以备将来的请求。