Nswag: [Question] SwaggerToCSharpController: Stuck with Swagger to .NET Framework 4.6.x generation

Created on 5 Nov 2018  路  5Comments  路  Source: RicoSuter/NSwag

TL;DR
Using SwaggerToCSharpController, how should the Controller implementation be injected so it is used?
Also, is there an example where this is done with NSwag? I see many examples but couldn't find it or overlooked it.

Details

I am trying to get the following working:

  • Input: swagger 2.0 specification
  • Output: Web API project with working controllers, on .NET Framework 4.6.2 on IIS (Express).

Here's what I did so far

Step 1: I configured the Web API project for NSwag

I followed these instructions from https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax#integration

image

Step 2: Then added the generated .cs class based on these settings:

{
  "runtime": "Default",
  "defaultVariables": null,
  "swaggerGenerator": {
    "fromSwagger": {
      "json": "swagger: '2.0'***REMOVED***",
      "url": "http://petstore.swagger.io/v2/swagger.json",
      "output": null
    }
  },
  "codeGenerators": {
    "swaggerToCSharpController": {
      "controllerBaseClass": null,
      "controllerStyle": "Partial",
      "useCancellationToken": false,
      "aspNetNamespace": "System.Web.Http",
      "routeNamingStrategy": "None",
      "className": "{controller}",
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "additionalNamespaceUsages": [],
      "additionalContractNamespaceUsages": [],
      "generateOptionalParameters": false,
      "generateJsonMethods": true,
      "enforceFlagEnums": false,
      "parameterArrayType": "System.Collections.Generic.IEnumerable",
      "parameterDictionaryType": "System.Collections.Generic.IDictionary",
      "responseArrayType": "System.Collections.ObjectModel.ObservableCollection",
      "responseDictionaryType": "System.Collections.Generic.Dictionary",
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "namespace": "***REMOVED***",
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "jsonConverters": null,
      "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.ObjectModel.ObservableCollection",
      "dictionaryBaseType": "System.Collections.Generic.Dictionary",
      "classStyle": "Inpc",
      "generateDefaultValues": true,
      "generateDataAnnotations": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateImmutableArrayProperties": false,
      "generateImmutableDictionaryProperties": false,
      "jsonSerializerSettingsTransformationMethod": null,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "generated.cs"
    }
  }
}

The results is that the /swagger documentation displays as expected. 馃憤

Step 3. Then I implemented the IController class

However, when calling my API the controller is not called.

Question: How should the Controller implementation be injected?

question

Most helpful comment

You need to register the IController interface and IController implementation in the DI system of your ASP.NET application so that it is instantiated and injected...

All 5 comments

You need to register the IController interface and IController implementation in the DI system of your ASP.NET application so that it is instantiated and injected...

Makes sense when looking at the generated code. However, I did not get this from the docs.

Feel free to update the docs:
https://github.com/RSuter/NSwag/wiki/SwaggerToCSharpControllerGenerator

There are also quite some open issues regarding controller generation:
https://github.com/RSuter/NSwag/projects/4

Thanks for teaching me about the project feature. I see it's focused on ASP.NET core though, understandably.

I see it's focused on ASP.NET core though, understandably.

Yes, but ASP.NET support will not be removed and it also supports ASP.NET Core on full .NET as a migration path.

Was this page helpful?
0 / 5 - 0 ratings