Search code examples
c#msbuild.net-8.0nswag

NSwag start the .NET 8 API Project in Command="$(NSwagExe_Net80) run NSwag.json /variables:Configuration=$(Configuration)"


In the Dotnet API project, recently we are upgradign .NET version from 6 to 8. In the new version of the application, in the nswag.json file "documentGenerator" engine is changed from "webApiToOpenApi" to "aspNetCoreToOpenApi". Here is my nswag.json file:

{
  "runtime": "Net80",
  "defaultVariables": "Configuration=Release,OutDir=bin/Release/net8.0/,HomeDir=%USERPROFILE%",
  "documentGenerator": {
    "aspNetCoreToOpenApi": {
      "project": "AppServices.csproj",
      "controllerNames": [],
      "isAspNetCore": true,
      "resolveJsonOptions": false,
      "defaultUrlTemplate": "api/{controller}/{id?}",
      "addMissingPathParameters": false,
      "includedVersions": null,
      "defaultPropertyNameHandling": "CamelCase",
      "defaultReferenceTypeNullHandling": "Null",
      "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
      "defaultResponseReferenceTypeNullHandling": "Null",
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "generateKnownTypes": true,
      "generateEnumMappingDescription": false,
      "generateXmlObjects": false,
      "generateAbstractProperties": false,
      "generateAbstractSchemas": true,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "excludedTypeNames": [],
      "serviceHost": null,
      "serviceBasePath": null,
      "serviceSchemes": [],
      "infoTitle": "App Content Services",
      "infoDescription": null,
      "infoVersion": "1.0.0",
      "documentTemplate": null,
      "documentProcessorTypes": [],
      "operationProcessorTypes": [],
      "typeNameGeneratorType": null,
      "schemaNameGeneratorType": null,
      "contractResolverType": null,
      "serializerSettingsType": null,
      "useDocumentProvider": true,
      "documentName": "v1",
      "aspNetCoreEnvironment": null,
      "createWebHostBuilderMethod": null,
      "startupType": null,
      "allowNullableBodyParameters": true,
      "output": null,
      "outputType": "Swagger2",
      "newLineBehavior": "Auto",
      "assemblyPaths": [],
      "assemblyConfig": null,
      "referencePaths": [
        "$(HomeDir)/.nuget/packages"
      ],
      "validateEnums": false,
      "generateEnums": false,
      "msBuildProjectExtensionsPath": null,
      "configuration": null,
      "runtime": null,
      "targetFramework": null,
      "noBuild": true,
      "msBuildOutputPath": null,
      "verbose": true,
      "workingDirectory": null,
      "requireParametersWithoutDefault": false,
      "apiGroupNames": null,
      "useNuGetCache": false
    }
  },
  "codeGenerators": {
    "openApiToCSharpClient": {
      "clientBaseClass": "",
      "configurationClass": "",
      "generateClientClasses": true,
      "generateClientInterfaces": true,
      "clientBaseInterface": null,
      "injectHttpClient": true,
      "disposeHttpClient": true,
      "protectedMethods": [],
      "generateExceptionClasses": true,
      "exceptionClass": "SwaggerException",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": true,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": false,
      "useBaseUrl": false,
      "generateBaseUrlProperty": true,
      "generateSyncMethods": true,
      "generatePrepareRequestAndProcessResponseAsAsyncMethods": false,
      "exposeJsonSerializerSettings": false,
      "clientClassAccessModifier": "public",
      "typeAccessModifier": "public",
      "generateContractsOutput": false,
      "contractsNamespace": null,
      "contractsOutputFilePath": null,
      "parameterDateTimeFormat": "s",
      "parameterDateFormat": "yyyy-MM-dd",
      "generateUpdateJsonSerializerSettingsMethod": true,
      "useRequestAndResponseSerializationSettings": false,
      "serializeTypeInformation": false,
      "queryNullValue": "",
      "className": "{controller}Client",
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "additionalNamespaceUsages": [],
      "additionalContractNamespaceUsages": [],
      "generateOptionalParameters": false,
      "generateJsonMethods": true,
      "enforceFlagEnums": false,
      "parameterArrayType": "System.Collections.Generic.IEnumerable",
      "parameterDictionaryType": "System.Collections.Generic.IDictionary",
      "responseArrayType": "System.Collections.Generic.List",
      "responseDictionaryType": "System.Collections.Generic.Dictionary",
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "namespace": "AppService.Clients",
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "jsonConverters": null,
      "anyType": "object",
      "dateTimeType": "System.DateTime",
      "timeType": "System.TimeSpan",
      "timeSpanType": "System.TimeSpan",
      "arrayType": "System.Collections.Generic.List",
      "arrayInstanceType": null,
      "dictionaryType": "System.Collections.Generic.Dictionary",
      "dictionaryInstanceType": null,
      "arrayBaseType": "System.Collections.Generic.List",
      "dictionaryBaseType": "System.Collections.Generic.Dictionary",
      "classStyle": "Inpc",
      "jsonLibrary": "NewtonsoftJson",
      "generateDefaultValues": true,
      "generateDataAnnotations": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateImmutableArrayProperties": false,
      "generateImmutableDictionaryProperties": false,
      "jsonSerializerSettingsTransformationMethod": null,
      "inlineNamedArrays": false,
      "inlineNamedDictionaries": false,
      "inlineNamedTuples": true,
      "inlineNamedAny": false,
      "generateDtoTypes": true,
      "generateOptionalPropertiesAsNullable": false,
      "generateNullableReferenceTypes": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "../AppServices.Clients/GeneratedClients.cs",
      "newLineBehavior": "Auto",
      "validateEnums": false,
      "generateEnums": false
    }
  }
}

But while executing the below command, it starts the application.

<Exec Command="$(NSwagExe_NET60) run NSwag.json /variables:Configuration=$(Configuration),OutDir=$(OutDir),HomeDir=&quot;$(HomeDir)&quot; " />

and produce the below log:

10>NSwag command line tool for .NET Core Net80, toolchain v14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))
10>Visit http://NSwag.org for more information.
10>NSwag bin directory: C:\.....\packages\nswag.msbuild\14.0.7\tools\Net80
10>
10>Executing file 'NSwag.json' with variables 'Configuration=Release,OutDir=bin\Release\net8.0\,HomeDir=C:\Users\MA0021'...
10>Launcher directory: C:\....\.nuget\packages\nswag.msbuild\14.0.7\tools\Net80
10>init main
10>Configuring to Use DbConfigurationSource.
10>info: DbConfigurationProvider[0]
10>      DbConfigurationProvider - Static Logger Configured
10>info: WebHostBootstrapper[0]
10>      WebHostBootstrapper - Static Logger Configured
10>dbug: Microsoft.AspNetCore.Hosting.Diagnostics[3]
10>      Hosting starting
10>dbug: Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory[12]
10>      Registered model binder providers, in the following order: Microsoft.AspNetCore.Mvc.ModelBinding.......
10>dbug: Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultViewCompiler[4]
10>      Initializing Razor view compiler with no compiled views.
10>info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[63]
10>      User profile is available. Using 'C:\.....\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
10>dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[37]
10>      Reading data from file 'C:\...\ASP.NET\DataProtection-Keys\key-8b371780-fd9d-496b-bb67-2db2152258dc.xml'.
................
10>dbug: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionHostedService[65]
10>      Key ring with default key {8b371780-fd9d-496b-bb67-2db2152258dc} was loaded during application startup.
10>dbug: Microsoft.AspNetCore.Server.Kestrel[0]
10>      No listening endpoints were configured. Binding to http://localhost:5000 by default.
10>dbug: Microsoft.AspNetCore.Hosting.Diagnostics[4]
10>      Hosting started
10>dbug: Microsoft.AspNetCore.Hosting.Diagnostics[13]
10>      Loaded hosting startup assembly AppServices
10>Hosting environment: Production
10>Content root path: C:\xxxxProj\AppServices
10>Now listening on: http://localhost:5000
10>Application started. Press Ctrl+C to shut down.
10>info: DbConfigurationProvider[0]
10>      DbConfigurationProvider - Static Logger Configured
10>info: WebHostBootstrapper[0]
10>      WebHostBootstrapper - Static Logger Configured
10>trce: DbConfigurationProvider[0]
10>      OnReloadTimerElapsed Triggered
10>Application is shutting down...
10>dbug: Microsoft.AspNetCore.Hosting.Diagnostics[5]
10>      Hosting shutdown
10>Done.
10>
10>Duration: 00:00:34.7767604
10>Done building project "AppServices.csproj".

Tried to add below paraeters in nswag but still it is starting the application. "validateEnums": false, "generateEnums": false,

Anything is missing?


Solution

  • According to https://github.com/RicoSuter/NSwag/issues/4524:

    i.nswag.json now only supports .csproj based specification generation (reflection/assembly based removed, only aspnetcore2openapi) ii.Removed WebApiToOpenApiCommand: Use the WebApiOpenApiDocumentGenerator to build your own CLI referencing controllers project to generate OpenAPI without starting the app.

    Hope it clarify you question.