Nswag: Error in asp.net core 2.1 project to swagger

Created on 18 Jun 2018  路  12Comments  路  Source: RicoSuter/NSwag

System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.

Runtime: NetCore21
at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\AspNetCore\AspNetCoreToSwaggerCommand.cs:line 172
at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 268
at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81
at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 77
at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 31
at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 54

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'NSwag.CodeGeneration, Version=11.17.15.0, Culture=neutral, PublicKeyToken=c2d88086e098d109'. The system cannot find the file specified.
Could not load file or assembly 'NSwag.CodeGeneration.CSharp, Version=11.17.15.0, Culture=neutral, PublicKeyToken=c2d88086e098d109'. The system cannot find the file specified.
Could not load file or assembly 'NSwag.CodeGeneration, Version=11.17.15.0, Culture=neutral, PublicKeyToken=c2d88086e098d109'. The system cannot find the file specified.
Could not load file or assembly 'NSwag.CodeGeneration.CSharp, Version=11.17.15.0, Culture=neutral, PublicKeyToken=c2d88086e098d109'. The system cannot find the file specified.
Could not load file or assembly 'NSwag.CodeGeneration.TypeScript, Version=11.17.15.0, Culture=neutral, PublicKeyToken=c2d88086e098d109'. The system cannot find the file specified.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeAssembly.get_DefinedTypes()
at System.Linq.Enumerable.SelectManySingleSelectorIterator2.ToArray() at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action1 additionalInitAction, IEnumerable1 assembliesToScan)
at Mizekar.Micro.Idea.Startup.ConfigureServices(IServiceCollection services) in D:\Workspace\Mizekar.Micro.Idea\src\Mizekar.Micro.Idea\Startup.cs:line 46
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerGeneratorCommandEntryPoint.GetServiceProvider(String applicationName) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\AspNetCore\AspNetCoreToSwaggerGeneratorCommandEntryPoint.cs:line 64
at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\AspNetCore\AspNetCoreToSwaggerGeneratorCommandEntryPoint.cs:line 21
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in C:\projects\nswag\src\NSwag.AspNetCore.Launcher\Program.cs:line 125

NSwag.Commands NSwag.SwaggerGeneration.AspNetCore await-repro bug

All 12 comments

Do you see more error output before this stacktrace? Can you provide a repro sample proj?

Add NSwag.CodeGeneration.CSharp package to my project and that work fine!

Good to know, but still bothers me that this is required...

agree with you.
I think something is unnatural here.
I have two project with same format and technical details, only different in business and domain entities.
in project one nswag need NSwag.CodeGeneration.CSharp package for work, but in project two no need and that work fine.
i don't know why!
i have a nswag.config that call it from powershell and generate c# client code from aspnet core project.

nswag.config content:

{
"runtime": "NetCore21",
"defaultVariables": "Configuration=Release,ProjectName=Example",
"swaggerGenerator": {
"aspNetCoreToSwagger": {
"project": "$(ProjectName).csproj",
"noBuild": false,
"verbose": false,
"defaultPropertyNameHandling": "Default",
"defaultReferenceTypeNullHandling": "Null",
"defaultEnumHandling": "Integer",
"flattenInheritanceHierarchy": false,
"generateKnownTypes": true,
"generateXmlObjects": false,
"generateAbstractProperties": false,
"ignoreObsoleteProperties": false,
"allowReferencesWithProperties": false,
"excludedTypeNames": [],
"infoTitle": "My Title",
"infoVersion": "1.0.0",
"output": null,
"outputType": "Swagger2",
"assemblyPaths": [],
"referencePaths": []
}
},
"codeGenerators": {
"swaggerToCSharpClient": {
"generateClientClasses": true,
"generateClientInterfaces": true,
"generateDtoTypes": true,
"injectHttpClient": true,
"disposeHttpClient": true,
"generateExceptionClasses": true,
"exceptionClass": "SwaggerException",
"wrapDtoExceptions": true,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": false,
"useBaseUrl": false,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"typeAccessModifier": "public",
"generateContractsOutput": false,
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "{controller}Client",
"operationGenerationMode": "MultipleClientsFromOperationId",
"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,
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "$(ProjectName).Client",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTime",
"dateTimeType": "System.DateTime",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "Inpc",
"generateDefaultValues": true,
"generateDataAnnotations": true,
"excludedTypeNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"output": "$(ProjectName).Client/RemoteApi.cs"
}
}
}

I have no idea what it actually does and new to all this swagger, openapi stuff ... using the ASP.NET Core via API Explorer (new) ...

I have a similar issue with a dotnetcore 2.1 project ...

So, just looking at the exception ... it seems it tried to start the whole project since it's trying to locate the appsettings.json ... ofcause it's not in the "C:\Program Files (x86)\Rico Suter\NSwagStudio\appsettings.json" path ...

Am I doing something wrong or missing something here?

It's clearly an issue in this part of the code ...

.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
            .Build();

I guess this could be rewritten or something ... but I would like the authors input here first?

( Sorry for hijacking the thread, if ... I can create a new issue if you want me to )

Creating the settings from NSwagStudio and running from the website home dir with:

nswag run .\nswag.nswag /runtime:NetCore21

This works ...

@syska I haven't seen this before, maybe we have to change the current working directory to the startup dll directory in the NSwag CLI? Or what is correct directory?

@RSuter

For this to work, CurrentDirectory needs to be the root directory of the ASPNETCORE website ( for me at least ... since it's looking for the appsettings.json etc. from this directory and be able to start it while browsing and gathering information.

So ... I guess a setting to specify the start directory

Please create a new issue to add this setting (default: nswag.json directory)

@RSuter I have created the issue here: https://github.com/RSuter/NSwag/issues/1484

Can we close this issue?

yes. thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AnthonySteele picture AnthonySteele  路  3Comments

alanedwardes picture alanedwardes  路  3Comments

akamyshanov picture akamyshanov  路  4Comments

Rui90 picture Rui90  路  4Comments

rh78 picture rh78  路  3Comments