ABP使用NSwagStudio for Swagger Api生成ServiceProxies

案例主要是使用NSwag来生成ABP for angular 2+的客户端代码。

NSwagStudio 下载地址 比较强大、可以生成TypeScript、WebApi Controller、CSharp Client 

1:运行Web.Host项目 http://localhost:22742/swagger/

2:安装NSwagStudio

3:生成代码

  3.1:ABP作者提供了.nswag的配置

{
  "swaggerGenerator": {
    "fromSwagger": {
      "url": "http://localhost:22742/swagger/v1/swagger.json",
      "output": null
    }
  },
  "codeGenerators": {
    "swaggerToTypeScriptClient": {
      "className": "{controller}ServiceProxy",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 1.8,
      "template": "Angular",
      "promiseType": "Promise",
      "dateTimeType": "MomentJS",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "wrapDtoExceptions": false,
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromPathSegments",
      "markOptionalProperties": false,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "extensionCode": "service.extensions.ts",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "importRequiredTypes": true,
      "baseUrlTokenName": "API_BASE_URL",
      "output": "../src/shared/service-proxies/service-proxies.ts"
    },
    "swaggerToCSharpClient": {
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateDtoTypes": true,
      "injectHttpClient": false,
      "disposeHttpClient": true,
      "generateExceptionClasses": true,
      "exceptionClass": "SwaggerException",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": false,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": false,
      "useBaseUrl": true,
      "generateSyncMethods": false,
      "clientClassAccessModifier": "public",
      "generateContractsOutput": false,
      "className": "{controller}Client",
      "namespace": "MyNamespace",
      "generateOptionalParameters": false,
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "dateTimeType": "System.DateTime",
      "timeType": "System.TimeSpan",
      "timeSpanType": "System.TimeSpan",
      "arrayType": "System.Collections.ObjectModel.ObservableCollection",
      "dictionaryType": "System.Collections.Generic.Dictionary",
      "classStyle": "Inpc",
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "generateDefaultValues": true,
      "generateDataAnnotations": true,
      "excludedTypeNames": [],
      "wrapResponses": false,
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "handleReferences": false,
      "generateImmutableArrayProperties": false,
      "generateImmutableDictionaryProperties": false,
      "output": null
    },
    "swaggerToCSharpController": {
      "className": "{controller}",
      "namespace": "MyNamespace",
      "additionalNamespaceUsages": [
        "System.Web.Http"
      ],
      "generateOptionalParameters": false,
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "dateTimeType": "System.DateTime",
      "timeType": "System.TimeSpan",
      "timeSpanType": "System.TimeSpan",
      "arrayType": "System.Collections.Generic.IEnumerable",
      "dictionaryType": "System.Collections.Generic.Dictionary",
      "classStyle": "Inpc",
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "generateDefaultValues": true,
      "generateDataAnnotations": true,
      "excludedTypeNames": [],
      "wrapResponses": false,
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "handleReferences": false,
      "generateImmutableArrayProperties": false,
      "generateImmutableDictionaryProperties": false,
      "output": null
    }
  }
}

  3.2:上面内容保存为.nswag的文件,打开方式以NSwagStudio

  3.3:点击 Generate Files  Soeasy  

 

 

 

posted @ 2017-11-24 15:10  危杨益  阅读(3240)  评论(0编辑  收藏  举报
危杨益