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.
I am trying to get the following working:
Here's what I did so far
I followed these instructions from https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax#integration

{
"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. 馃憤
However, when calling my API the controller is not called.
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.
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...