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

如何禁用StrawberryShake在每次构建时重新生成客户端代码?

问题

基于.NET 7的客户端应用,使用StrawberryShake v12.20版本,本地编译测试全正常,但上传到用Autobahn和Docker搭建的CI/CD管道后,执行dotnet test命令失败。

错误起始信息:
CSC : error SS0006: Method not found: 'Void StrawberryShake.CodeGeneration.CSharp.CSharpGeneratorSettings.set_RequestStrategy (StrawberryShake.Tools.Configuration.RequestStrategy)...
后续还有60多条关于StrawberryShake生成的客户端代码缺失类型或命名空间的错误。

推测管道不支持构建时重新生成并覆盖客户端代码类,但不知道怎么禁用该功能。尝试在graphqlrc.json中设置emitGeneratedCode为false,没有效果。

graphqlrc.json 文件内容

{
  "schema": "schema.graphql",
  "documents": "**/*.graphql",
  "extensions": {
    "strawberryShake": {
      "name": "MyClient",
      "url": "http://localhost:5000/graphql",
      "dependencyInjection": true,
      "strictSchemaValidation": true,
      "hashAlgorithm": "md5",
      "useSingleFile": true,
      "requestStrategy": "Default",
      "outputDirectoryName": "Generated",
      "noStore": false,
      "emitGeneratedCode": true,
      "razorComponents": false,
      "records": {
        "inputs": false,
        "entities": false
      },
      "transportProfiles": [
        {
          "default": "Http",
          "subscription": "WebSocket"
        }
      ]
    }
  }
}

失败的管道命令

dotnet test *.sln /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile="Program.cs" /p:ExcludeByFile="Startup.cs"
解决方案
  • 禁用构建时代码生成
    仅修改graphqlrc.json的emitGeneratedCode无效,需在项目.csproj文件中添加配置强制关闭代码生成:

    <PropertyGroup>
      <StrawberryShake_EnableCodeGeneration>false</StrawberryShake_EnableCodeGeneration>
    </PropertyGroup>
    

    也可以直接移除项目中StrawberryShake代码生成器的引用(如果不需要动态生成)。

  • 提交生成代码到版本库
    本地已生成的Generated目录下所有文件,全部提交到Git仓库,让CI管道直接使用现成的生成代码,跳过构建时的生成步骤。

  • 统一CI环境的依赖版本
    Method not found错误大多是版本不匹配导致,确保CI环境安装的StrawberryShake工具版本和本地一致(v12.20),可在管道中添加命令指定版本安装:

    dotnet tool install StrawberryShake.Tools --version 12.20.0
    
  • 拆分构建与测试步骤
    dotnet test会自动触发构建,可先单独完成构建再执行测试,避免构建过程中的代码生成问题:

    dotnet build *.sln
    dotnet test *.sln /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile="Program.cs" /p:ExcludeByFile="Startup.cs" --no-build
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 14:24:56