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

使用dotnet-isolated运行时的Azure Functions部署后无法运行且不可见

问题解决:.NET 7 隔离模式 Azure Functions 部署后门户不显示函数

问题背景

我有一个基于.NET 7运行时的Azure Functions项目,配置为dotnet-isolated模式。本地通过Visual Studio发布+Azurite模拟器运行正常,但通过GitLab/Kubernetes Runner构建、Octopus部署后,Azure门户的「Functions」标签页不显示任何函数,Azure运行时无法识别构建产物中的独立.exe文件,导致函数无法运行。

构建环境:自托管GitLab的Kubernetes Runner,使用mcr.microsoft.com/dotnet/sdk:7.0镜像;托管环境是EP1层级的Windows Azure Functions应用。

当前流水线执行命令:

- dotnet restore --force --no-cache
- dotnet build --configuration Release

之前非隔离模式+Windows Runner的流水线正常,但切换为隔离模式+Linux Runner后出现问题。发现未指定--runtime win时,Runner仅生成Linux产物,无.exe文件;且尝试过dotnet publish但没生成可用部署包,因此省略了该命令。

项目文件内容:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.16.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.1.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.11.0" />
    <PackageReference Include="OpenAI" Version="1.7.2" />
</ItemGroup>
<ItemGroup>
    <ProjectReference Include="..\Core.Jobs\Core.Jobs.csproj" />
    <ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
<ItemGroup>
    <None Update="host.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
    <None Update="nrdiag.exe">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="nrdiag_arm64.exe">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="nrdiag_x64.exe">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>
</Project>

应用设置(FUNCTIONS_WORKER_RUNTIME):

{
"IsEncrypted": false,
"Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
},
"ConnectionStrings": {
}
}

根本原因分析

  1. 未使用dotnet publish生成部署包:dotnet-isolated模式的Azure Functions必须通过publish命令生成符合Azure要求的部署产物,dotnet build仅生成编译后的二进制文件,缺少Azure运行时所需的部署配置和依赖打包。
  2. 构建目标 runtime 不匹配:Linux Runner默认生成Linux平台的产物(无.exe),但托管环境是Windows,必须指定Windows runtime(如win-x64)才能生成可执行文件。
  3. 流水线配置遗漏关键参数:缺少--self-contained或--runtime参数,导致构建产物不符合Windows环境要求。

解决步骤

1. 修改流水线构建命令

替换dotnet build为dotnet publish,并添加Windows runtime参数:

- dotnet restore --force --no-cache
- dotnet publish --configuration Release --runtime win-x64 --self-contained false --output ./publish
  • --runtime win-x64:指定生成Windows x64平台的产物,确保生成.exe文件
  • --self-contained false:依赖Azure Functions宿主环境中的.NET 7运行时(若需独立部署可设为true,但Windows函数应用已预装对应runtime,建议设为false)
  • --output ./publish:将产物输出到指定目录,便于打包部署

2. 确认项目文件配置

确保项目文件中已包含以下关键配置(当前已满足):

  • <OutputType>Exe</OutputType>:隔离模式必须设为Exe
  • <AzureFunctionsVersion>v4</AzureFunctionsVersion>:匹配Azure Functions v4宿主
  • 包含Microsoft.Azure.Functions.Worker和Microsoft.Azure.Functions.Worker.Sdk包引用

3. 验证Azure应用设置

确保Azure Functions应用的配置中:

  • FUNCTIONS_WORKER_RUNTIME设为dotnet-isolated
  • FUNCTIONS_EXTENSION_VERSION设为~4(默认已配置,若未设置需手动添加)

4. 调整Octopus部署配置

确保Octopus部署的是./publish目录下的所有文件,而不是build目录的产物。部署包需包含:

  • 函数应用的.exe文件
  • host.json、appsettings.json等配置文件
  • 所有依赖的DLL文件

验证方法

  1. 构建完成后检查./publish目录,确认存在对应名称的.exe文件
  2. 部署后查看Azure门户的「部署中心」,确认部署状态成功
  3. 重启Azure Functions应用,刷新「Functions」标签页,查看函数是否显示
  4. 查看Azure的「日志流」,排查是否有启动错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 14:57:15