azure - 访问 .NET 5 Azure Function 中的 FunctionAppDirectory

标签 azure azure-functions .net-5

我需要访问 Azure Function 中的 FunctionAppDirectory

这是该函数的简化版本

[Function("Test")]
public static HttpResponseData Test([HttpTrigger(AuthorizationLevel.Function, "post", Route = "Test")] HttpRequestData req, 
ExecutionContext context, FunctionContext fContext)
{
    var log = fContext.GetLogger(nameof(TestOperations));
    log.LogInformation(context?.FunctionAppDirectory?.ToString());
    return req.CreateResponse(HttpStatusCode.OK);
}

ExecutionContext此处为空。

我的Program.cs文件

class Program
{
    static Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder.AddCommandLine(args);
            })
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(services =>
            {
                // Add Logging
                services.AddLogging();
            })
            .Build();

        return host.RunAsync();
    }
}

在 .NET 5 中运行的 Azure 函数

如何配置 ExecutionContext 的绑定(bind)或以其他方式获取 FunctionAppDirectory?

最佳答案

正如 Alex 在评论中提到的,azure function .net 5 现在不支持“context.FunctionAppDirectory”来获取函数应用程序的目录。

在函数应用3.0中,“ExecutionContext”被设计在“Microsoft.Azure.WebJobs”包中。但在您的代码中,ExecutionContext 来自“System.Threading”。所以这些是不同的类。

您可以使用以下代码获取.NET 5.0 azure函数中的azure函数目录:

using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp6
{
    public static class Function1
    {
        [Function("Function1")]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger("Function1");
            logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            var local_root = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
            var azure_root = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
            var actual_root = local_root ?? azure_root;

            response.WriteString(actual_root);

            return response;
        }
    }
}

关于azure - 访问 .NET 5 Azure Function 中的 FunctionAppDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68082798/

相关文章:

r - 打开 RStudio 项目时出现系统错误 183

Azure CLI - 如何更新磁盘加密设置

azure - KQL如何根据与匹配值同一行的项目来查找查询中的匹配值和原始值

javascript - 如何强制Azure函数将JSON输出到浏览器

.net-5 - 无法创建控制台应用程序目标 .NET 5

azure - 您可以在不部署整个网站的情况下替换 Azure 网站中的选定文件吗?

azure - 如何从函数应用程序更新 azure 应用程序配置值?

python - Azure Functions Python blobTrigger 如何修复 "Microsoft.Azure.WebJobs.Extensions.Storage: Object reference not set to an instance of an object."?

azure - 使用 postman 进行测试,即使 Azure AD 身份验证正常,也会出现错误 401 未经授权

mysql - docker-compose 首次启动并构建,MySql 服务器启动速度比 .net5 API 慢