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

Windows环境下使用sam build构建C# Native AOT Lambda失败

解决Windows 11上SAM构建C# Native AOT Lambda时的NU1301错误

问题背景

在Windows 11上通过sam init创建.NET 8 C# Native AOT Lambda项目,执行sam build时触发容器构建,出现NU1301错误:无法加载NuGet官方源https://api.nuget.org/v3/index.json的服务索引,导致项目还原失败。个人电脑可正常构建,仅工作电脑出现该问题。

错误日志关键内容:

... docker run: /tmp/source/src/HelloWorldAot/HelloWorldAot.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
... docker run:   Failed to restore /tmp/source/src/HelloWorldAot/HelloWorldAot.csproj (in 5.79 sec).
ERROR: Container build returned 1

解决方案

以下是针对工作环境的排查和修复步骤:

1. 检查Docker容器的网络连通性

SAM构建使用的容器可能无法访问外部网络,先验证容器能否连接NuGet源:

  • 手动运行构建容器并测试网络:
    docker run --rm public.ecr.aws/sam/build-dotnet8:latest-x86_64 curl https://api.nuget.org/v3/index.json
    
  • 如果curl失败,说明容器网络存在问题:
    • 检查工作电脑的防火墙/代理设置,确保允许Docker容器访问外部HTTPS地址
    • 切换Docker的网络模式为host(仅Windows WSL2环境适用),修改sam build命令:
      sam build --container-network host
      

2. 配置NuGet镜像源(内网镜像)

如果工作环境有内部NuGet镜像源,可替换项目的NuGet源配置:

  • 在项目根目录创建NuGet.Config文件,内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="InternalNuGet" value="https://your-internal-nuget-source/v3/index.json" />
      </packageSources>
    </configuration>
    
  • 确保内部镜像源可被容器访问,且包含项目所需的所有NuGet包(如Amazon.Lambda相关包)

3. 检查Docker代理配置

工作电脑可能配置了代理,需确保Docker容器继承代理设置:

  • 在Docker Desktop的设置中,进入Resources > Proxies,勾选"Use proxy server"并填入工作环境的代理地址和端口
  • 重启Docker Desktop后重新执行sam build

4. 使用本地构建替代容器构建

如果容器网络问题无法快速解决,可切换为本地构建模式:

  • 修改sam build命令,指定跳过容器构建:
    sam build --use-container false
    
  • 注意:本地构建需确保已安装.NET 8 SDK,且执行dotnet publish时指定正确的运行时:
    dotnet publish src/HelloWorldAot -c Release -f net8.0 -r linux-x64 --self-contained true /p:GenerateRuntimeConfigurationFiles=true /p:StripSymbols=true
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 20:13:16