使用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 --versionto check your current version, then update to the latest stable release withnpm install -g autorest. - OpenAPI Override: If your
swagger.jsonincludes extensions likex-ms-internalthat 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 thecomponentssection:
Then apply this scheme globally by adding a root-level"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" } } } } } }securitysection:"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
相关产品推荐
相关产品推荐

