RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

Unable to build swagger.json file #4623

Open BitterMonster opened 8 months ago

BitterMonster commented 8 months ago

.Net 6.0 project - win api

include in csproj file :

Build project and swagger.json created.

In program.cs add:

var webApplicationOptions = new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; var builder = WebApplication.CreateBuilder(webApplicationOptions);

rather than:

var builder = WebApplication.CreateBuilder(args);

and build fails to generate swagger.json. By process of elimination go to:

var webApplicationOptions = new WebApplicationOptions() { Args = args };

and the swagger.json will be generated when build.

BitterMonster commented 8 months ago

Program.cs

using System.Diagnostics;

Debug.WriteLine(System.Diagnostics.Process.GetCurrentProcess().ProcessName); Debug.WriteLine(AppContext.BaseDirectory);

var webApplicationOptions = new WebApplicationOptions() {Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; //var webApplicationOptions = new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName }; var builder = WebApplication.CreateBuilder(webApplicationOptions); //var builder = WebApplication.CreateBuilder(args); builder.Host.UseWindowsService();

// Add services to the container.

builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerDocument();

var app = builder.Build();

// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseOpenApi(); app.UseSwaggerUi3(); }

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

BitterMonster commented 8 months ago

Narrowed down issue to presence of ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName

when present see the error:

System.IO.FileNotFoundException: Could not load file or assembly 'dotnet, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. 1>File name: 'dotnet, Culture=neutral, PublicKeyToken=null' 1> at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly) 1> at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext) 1> at System.Reflection.Assembly.Load(AssemblyName assemblyRef) 1> at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.GetApplicationPartAssemblies(String entryAssemblyName) 1> at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName) 1> at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services, IWebHostEnvironment environment) 1> at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services) 1> at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services) 1> at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services) 1> at Program.

$(String[] args) in C:\Users\timmb\source\repos\TestWebAPI\TestWebAPI\Program.cs:line 14 1>--- End of stack trace from previous location --- 1> at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost() in /_/src/NSwag.Commands/HostFactoryResolver.cs:line 271 1> at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass8_0.b_0(String[] args) in //src/NSwag.Commands/HostFactoryResolver.cs:line 75 1> at NSwag.Commands.ServiceProviderResolver.GetServiceProviderWithHostFactoryResolver(Assembly assembly) in //src/NSwag.Commands/HostApplication.cs:line 146 1> at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in //src/NSwag.Commands/HostApplication.cs:line 70 1> at NSwag.Commands.Generation.OpenApiGeneratorCommandBase1.GetServiceProvider(AssemblyLoader assemblyLoader) in /_/src/NSwag.Commands/Commands/Generation/OpenApiGeneratorCommandBase.cs:line 313 1> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 323 1> at NSwag.Commands.IsolatedCommandBase1.IsolatedCommandAssemblyLoader.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in //src/NSwag.Commands/Commands/IsolatedCommandBase.cs:line 76 1> at NSwag.Commands.IsolatedCommandBase`1.RunIsolatedAsync(String configurationFile) in //src/NSwag.Commands/Commands/IsolatedCommandBase.cs:line 61 1> at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/IsolatedSwaggerOutputCommandBase.cs:line 51 1> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 98 1> at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input) 1> at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input) 1> at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 61 1>C:\Users\timmb\source\repos\TestWebAPI\TestWebAPI\TestWebAPI.csproj(9,4): error MSB3073: The command "dotnet "C:\Users\timmb.nuget\packages\nswag.msbuild\13.20.0\buildTransitive../tools/Net60/dotnet-nswag.dll" aspnetcore2openapi /assembly:C:\Users\timmb\source\repos\TestWebAPI\TestWebAPI\bin\Debug\net6.0\TestWebAPI.dll /output:swagger.json" exited with code -1.

BitterMonster commented 8 months ago

Found solution which was :

var webApplicationOptions = System.Diagnostics.Process.GetCurrentProcess().ProcessName == "dotnet" ? new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args } : new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName };

However would be helpful if NSwag identified issue and provided a helpful error message.