ASP.NET中NSwag+MSBuild自动生成TypeScript代码遇阻求助
ASP.NET Core + NSwag MSBuild自动生成TypeScript客户端问题
已完成的基础工作
- 安装
NSwag.AspNetCore包,在Program.cs中配置Swagger:
builder.Services.AddOpenApiDocument(); app.UseOpenApi(); app.UseSwaggerUI();
- 通过SwaggerUI获取OpenAPI链接,使用NSwag Studio成功生成可用的TypeScript客户端代码。
尝试MSBuild自动生成的操作及遇到的问题
- 安装
NSwag.MSBuild包,通过NSwag Studio生成nswag.json配置:
{ "runtime": "Net70", "defaultVariables": null, "documentGenerator": { "aspNetCoreToOpenApi": { "project": "Bets.WebUI.csproj", "documentName": "v1", "msBuildProjectExtensionsPath": null, "configuration": null, "runtime": null, "targetFramework": null, "noBuild": false, "msBuildOutputPath": null, "verbose": true, "workingDirectory": null, "aspNetCoreEnvironment": null, "output": null, "newLineBehavior": "Auto" } }, "codeGenerators": { "openApiToTypeScriptClient": { "className": "{controller}Client", "moduleName": "", "namespace": "", "typeScriptVersion": 4.3, "template": "Angular", "promiseType": "Promise", "httpClass": "HttpClient", "withCredentials": false, "useSingletonProvider": true, "injectionTokenType": "InjectionToken", "rxJsVersion": 7.0, "dateTimeType": "Date", "nullValue": "Undefined", "generateClientClasses": false, "generateClientInterfaces": false, "generateOptionalParameters": false, "exportTypes": true, "wrapDtoExceptions": false, "exceptionClass": "ApiException", "clientBaseClass": null, "wrapResponses": false, "wrapResponseMethods": [], "generateResponseClasses": true, "responseClass": "SwaggerResponse", "protectedMethods": [], "configurationClass": null, "useTransformOptionsMethod": false, "useTransformResultMethod": false, "generateDtoTypes": true, "operationGenerationMode": "MultipleClientsFromOperationId", "markOptionalProperties": false, "generateCloneMethod": false, "typeStyle": "Interface", "enumStyle": "Enum", "useLeafType": false, "classTypes": [], "extendedClasses": [], "extensionCode": null, "generateDefaultValues": true, "excludedTypeNames": [], "excludedParameterNames": [], "handleReferences": false, "generateTypeCheckFunctions": false, "generateConstructorInterface": true, "convertConstructorInterfaceData": false, "importRequiredTypes": true, "useGetBaseUrlMethod": false, "baseUrlTokenName": "API_BASE_URL", "queryNullValue": "", "useAbortSignal": false, "inlineNamedDictionaries": true, "inlineNamedAny": true, "includeHttpContext": true, "templateDirectory": null, "serviceHost": null, "serviceSchemes": null, "output": "test.ts", "newLineBehavior": "Auto" } } }
- 在
Bets.WebUI.csproj中添加MSBuild目标,实现构建后自动执行NSwag:
<Target Name="NSwag" AfterTargets="Build"> <Exec Command="$(NSwagExe_Net70) run nswag.json /variables:Configuration=$(Configuration)" /> </Target>
- 构建时进程卡住,输出以下日志后无后续执行:
13>NSwag command line tool for .NET Core Net70, toolchain v14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) 13>Visit http://NSwag.org for more information. 13>NSwag bin directory: C:\Users\famou\.nuget\packages\nswag.msbuild\14.0.3\tools\Net70 13> 13>Executing file 'nswag.json' with variables 'Configuration=Debug'...
- 尝试修改
documentGenerator中的aspNetCoreToOpenApi配置,改用已编译的程序集路径:
"aspNetCoreToOpenApi": { "project": null, "documentName": "v1", "msBuildProjectExtensionsPath": null, "configuration": null, "runtime": null, "targetFramework": null, "noBuild": false, "msBuildOutputPath": null, "verbose": true, "workingDirectory": null, "aspNetCoreEnvironment": null, "output": null, "newLineBehavior": "Auto", "assemblyPaths": [ "bin/Debug/net7.0/Bets.WebUI.dll" ], "assemblyConfig": null, "referencePaths": [], "useNuGetCache": false }
但构建时NSwag会自动删除assemblyPaths等新增配置项,同时报错提示未指定项目路径。
目前无法回退.NET或NSwag版本,需要针对最新版本的解决方案。
内容的提问来源于stack exchange,提问作者Famouse
相关产品推荐
相关产品推荐

