gpt4 book ai didi

azure - 如何在Azure函数中的Configure(IWebJobsBuilder builder)中获取FunctionAppDirectory

转载 作者:行者123 更新时间:2023-12-03 00:48:45 27 4
gpt4 key购买 nike

在Azure函数中,我可以从上下文中获取FunctionAppDirectory,但是如何在Configure方法中获取FunctionAppDirectory。

我需要 FunctionAppDirectory 位于 <<FunctionAppDirectory>>在代码中:

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebJobsExtensionStartup.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Intercom.Helpers;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

[assembly: WebJobsStartup(typeof(ConvAi.BfChannel.BotManagementService.WebJobsExtensionStartup), "Web Jobs Extension")]
namespace ConvAi.BfChannel.BotManagementService
{
/// <summary>
/// WebJobsExtensionStartup
/// So this Azure function should be deployed with App service plan.
/// </summary>
public class WebJobsExtensionStartup : IWebJobsStartup
{
/// <summary>
/// AzureServiceTokenProvider which is used for requesting identity token.
/// </summary>
public static AzureServiceTokenProvider AzureServiceTokenProvider { get; set; }

/// <summary>
/// Configure services.
/// </summary>
/// <param name="builder">WebJob Builder</param>
public void Configure(IWebJobsBuilder builder)
{
try
{
bool isLocal = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));

// Gets the default configuration
var serviceConfig = builder.Services.FirstOrDefault(s => s.ServiceType.Equals(typeof(IConfiguration)));
var rootConfig = (IConfiguration)serviceConfig.ImplementationInstance;


var config = new ConfigurationBuilder()
.SetBasePath(<<FunctionAppDirectory>>)
.AddConfiguration(rootConfig)
.AddJsonFile($@"Config\botregistrationOptions.{rootConfig["environmentName"]}.json", optional: false)
.Build();

// Replace the existing config
builder.Services.AddSingleton<IConfiguration>(config);

}
catch (Exception ex)
{
//log
throw;
}
}
}
}

最佳答案

您可以将 context.FunctionAppDirectory 替换为 Environment.CurrentDirectory

至少,它可以在本地工作,并且正是您需要 local.settings.json 工作的地方,因此此更改应该是安全的。

当您在azure上使用它时,您需要将.AddEnvironmentVariables()添加到config

更新:

当您在本地和 Azure 上工作时,请使用以下代码。

var config = new ConfigurationBuilder()
.SetBasePath(<<FunctionAppDirectory>>)
.AddConfiguration(rootConfig)
.AddJsonFile($@"Config\botregistrationOptions.{rootConfig["environmentName"]}.json", optional: false)
.AddEnvironmentVariables()
.Build();

关于azure - 如何在Azure函数中的Configure(IWebJobsBuilder builder)中获取FunctionAppDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55837896/

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