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

使用AutoRest生成C# REST服务客户端时构造函数为internal且未添加凭据参数的问题排查

Troubleshooting AutoRest C# Client Generation Issues

Let's break down your two problems and walk through solutions for each:

1. Internal Constructor Despite --public-clients=true

The --public-clients=true parameter isn't taking effect likely due to one of these common issues:

  • Parameter Spelling Mismatch: Newer versions of AutoRest's C# generator use the singular --public-client=true (not plural) to mark clients as public. Swap your plural parameter for the singular version in your command.
  • Outdated Generator Version: Old AutoRest or C# generator packages can have parameter inconsistencies. Run autorest --version to check your current version, then update to the latest stable release with npm install -g autorest.
  • OpenAPI Override: If your swagger.json includes extensions like x-ms-internal that force internal access, this will override the CLI parameter. Scan your OpenAPI document for such extensions and remove them if present.

2. Missing Credential Parameter in Constructor

The --add-credential flag depends entirely on your OpenAPI document defining a valid security scheme—without this, AutoRest has no context to generate credential-aware constructors. Here's how to fix this:

  • Add a Security Scheme to swagger.json: For example, if using Azure AD OAuth2, add this to the components section:
    "components": {
      "securitySchemes": {
        "azure_auth": {
          "type": "oauth2",
          "flows": {
            "authorizationCode": {
              "authorizationUrl": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
              "tokenUrl": "https://login.microsoftonline.com/common/oauth2/v2.0/token",
              "scopes": {
                "https://management.azure.com/.default": "Default scope"
              }
            }
          }
        }
      }
    }
    
    Then apply this scheme globally by adding a root-level security section:
    "security": [
      {
        "azure_auth": []
      }
    ]
    
  • Match Your Auth Type: If using API keys or another auth method, adjust the security scheme to match your setup. AutoRest will generate the correct credential constructor once it recognizes a valid security definition.

Revised Command to Test

With the parameter fix and updated OpenAPI document, your command should look like this:

autorest --input-file="./Resources/swagger.json" --output-folder="./SomeService/Generated" --namespace="SomeService.Client" --override-client-name="SomeServiceClient" --skip-csproj --public-client=true --add-credential --csharp

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 16:17:32