RicoSuter / NSwag

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

NSwagger Studio ASP.NET Core via API Explorer not working #3770

Open abuzaforfagun opened 3 years ago

abuzaforfagun commented 3 years ago

Project Option: It generates empty Swagger Specification. Assembly Option: It return an error about BuildWebHost or CreateWebHostBuilder not found.

System.InvalidOperationException: NSwag requires the assembly ZEnergy.LoyaltyCards.Frontend.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null to have either an BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x for suggestions on ways to refactor your startup type.

Runtime: Net60
   at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in D:\a\NSwag\NSwag\src\NSwag.Commands\HostApplication.cs:line 87
   at NSwag.Commands.Generation.OpenApiGeneratorCommandBase`1.GetServiceProvider(AssemblyLoader assemblyLoader) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Generation\OpenApiGeneratorCommandBase.cs:line 299
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 323
   at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 76
   at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__271_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at NSwag.Commands.IsolatedCommandBase`1.RunIsolatedAsync(String configurationFile) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 51
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 98
   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in D:\a\NSwag\NSwag\src\NSwag.Commands\NSwagDocumentBase.cs:line 280
   at NSwag.Commands.NSwagDocument.ExecuteAsync() in D:\a\NSwag\NSwag\src\NSwag.Commands\NSwagDocument.cs:line 81
   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 85
   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in D:\a\NSwag\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32
   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 D:\a\NSwag\NSwag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 55

Steps to reproduce:


using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
    });
builder.Services.AddApiVersioning(options =>
{
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ReportApiVersions = true;
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.ApiVersionReader = new UrlSegmentApiVersionReader();
}).AddVersionedApiExplorer(options =>
{
    options.SubstituteApiVersionInUrl = true;
});
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddHttpClient();

builder.Services.AddOpenApiDocument(document =>
{
    document.DocumentName = "v1";
    document.Version = "v1";
    document.Title = "Test API";
    document.Description = "Test API";
});

var app = builder.Build();
app.UseOpenApi();
app.UseSwaggerUi3();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

NSwagger Studio Version: 13.14.7.0

RicoSuter commented 2 years ago

Using assembly is obsolete and should not be used, controllers with minimal APIs do not work (use Startup.cs), see https://github.com/RicoSuter/NSwag/issues/3794

arielnahom commented 1 year ago

Hi @RicoSuter I'd appreciate your help:

I get the same error, I'm using version NSwag.MSBuild 13.19 and dotnet7, no minimal APIs.

maybe it's because of this condition on the above mentioned PR not having NET7 ?

my csproj :

`

</Target>`

nswag is default with "runtime": "Net70",

my program.cs is simple and usual startup-less without even a Main function :

` try { var builder = WebApplication.CreateBuilder(args);

var healthCheckBuilder = builder.Services.AddHealthChecks();

_ = builder.Services.AddSingleton<IServiceCollection>(builder.Services);
_ = builder.Services.AddOptions();

_ = builder.Host.ConfigureServices((hostContext, services) =>
{
    _ = services.AddSingleton<IExampleService, gt178.Services.ExampleService>();
    _ = services.AddSingleton<gt178.Repositories.Interfaces.IExampleRepository, IExampleRepository>();
});
var app = builder.Build();

app.Run();

} catch (Exception e) { //log }

///

/// THe Program object /// public partial class Program { }`

the error :

System.InvalidOperationException: NSwag requires the assembly gt178.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null to have either an BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method. See https://docs.micros oft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x for suggestions on ways to refactor your startup type. at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in //src/NSwag.Commands/HostApplication.cs:line 87 at NSwag.Commands.Generation.OpenApiGeneratorCommandBase`1.GetServiceProvider(AssemblyLoader assemblyLoader) in //src/NSwag.Commands/Commands/Generation/OpenApiGeneratorCommandBase.cs:line 313 at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in //src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 323 at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in //src/NSwag.Commands/Commands/IsolatedCommandBase.cs:line 76
at NSwag.Commands.IsolatedCommandBase1.RunIsolatedAsync(String configurationFile) in /_/src/NSwag.Commands/Commands/IsolatedCommandBase.cs:line 61 at NSwag.Commands.IsolatedSwaggerOutputCommandBase1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/IsolatedSwaggerOutputCommandBase.cs:line 51 at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 98 at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in //src/NSwag.Commands/NSwagDocumentBase.cs:line 275 at NSwag.Commands.NSwagDocument.ExecuteAsync() in //src/NSwag.Commands/NSwagDocument.cs:line 81 at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in //src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 85 at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in //src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 32 at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input) at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input) at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 61