azure - 在 .NET 5 Azure 函数中访问 FunctionAppDirectory
问题描述
我需要FunctionAppDirectory
在 Azure Function中访问
这是函数的简化版本
[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 配置绑定或以其他方式获取 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;
}
}
}
推荐阅读
- javascript - Jquery 在 HTML 表中添加单元格我希望我设置它不显示,因为这些是 id?MVC
- reactjs - 从 useSelector() 编辑局部变量时,Redux 状态正在发生变化
- oracle - 更改 Oracle 应用程序中的查询以获取报价
- python - 如何在python中将一年添加到日期?
- flutter - Flutter Http 调用列表
在 UI 中总是导致 Null - python-3.x - 无法将 ipynb 转换为 PDF
- c++ - 试图更多地了解 C++ 中的 bitset
- vba - 在 vba 中卡住 Internet Explorer 自动化使用谷歌地图计算两地之间的距离
- android - 如何在棒棒糖以上的android版本中获取浏览器历史记录
- react-native - 修饰并移动图像返回初始位置后反应本机 Panresponder 问题