You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Azure Functions V4 Worker模型后,替代ExecutionContext获取FunctionDirectory的方案

升级Azure Functions V1到V4 Worker模型:替换ExecutionContext获取应用目录的方案

问题背景

我正在将基于.NET Framework 4.6.1的Azure Functions V1升级至基于.NET Framework 4.8的Azure Functions V4(Worker模型)。原代码依赖Microsoft.Azure.WebJobs命名空间下的ExecutionContext,通过以下代码获取应用目录路径:

string appDirectoryPath = context.FunctionDirectory;

现在需要找到Microsoft.Azure.Functions.Worker环境下的替代实现方案。

项目配置对比

旧项目(V1).csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <AzureFunctionsVersion>v1</AzureFunctionsVersion>
  </PropertyGroup>
 <ItemGroup>
   <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
   <PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.4.0" />
   <PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.2.0" />
   <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
   <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
   <PackageReference Include="SSH.NET" Version="2016.1.0" />
   <PackageReference Include="WindowsAzure.Storage" Version="9.3.2" />
 </ItemGroup>
</Project>

新项目(V4 Worker).csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.17.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
</Project>

替代实现方案

在Azure Functions V4 Worker模型中,没有直接对应ExecutionContext.FunctionDirectory的API,可通过以下两种方式获取应用/函数目录:

方案1:依赖注入IConfiguration读取环境变量

通过注入IConfiguration读取AzureWebJobsScriptRoot环境变量(该变量指向函数应用的根目录),结合FunctionContext中的函数名拼接得到具体函数的目录:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using System.IO;

public class MyFunction
{
    private readonly IConfiguration _configuration;

    public MyFunction(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [Function("MyFunction")]
    public void Run([ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] string message, FunctionContext context)
    {
        // 获取应用根目录
        string appRootPath = _configuration["AzureWebJobsScriptRoot"];
        // 拼接当前函数所在目录
        string functionDirectory = Path.Combine(appRootPath, context.FunctionDefinition.Name);
    }
}

方案2:直接读取环境变量

如果不需要依赖注入,也可以直接通过Environment.GetEnvironmentVariable获取根目录:

using System;
using System.IO;
using Microsoft.Azure.Functions.Worker;

public class MyFunction
{
    [Function("MyFunction")]
    public void Run([ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] string message, FunctionContext context)
    {
        string appRootPath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
        string functionDirectory = Path.Combine(appRootPath, context.FunctionDefinition.Name);
    }
}

注意事项

  • AzureWebJobsScriptRoot环境变量在本地开发和Azure部署环境中都会由Functions Runtime自动配置,无需手动设置。
  • Worker模型默认支持构造函数注入IConfiguration,无需额外配置依赖注入容器。

内容的提问来源于stack exchange,提问作者Hiten Samalia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 13:55:05