Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nswag cli - "Changing the URL is not supported because no valid IServerAddressesFeature was found" #5012

Open
zyberzero opened this issue Oct 31, 2024 · 0 comments

Comments

@zyberzero
Copy link

Hello,

If I start my Aspnetcore app and use app.Run("http://*:5000") in my app, nswag aspnetcore2openapi crashes.

Minimum example to recreate the issue,
Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument();

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


app.MapGet("/testendpoint", () => { })
    .WithName("testendpoint");

app.Run("http://*:5000");

project.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="NSwag.AspNetCore" Version="14.1.0" />
    </ItemGroup>
</Project>

If I then in my project working directory run this command: npx nswag aspnetcore2openapi /output:./test.swagger
it yells at me:

NSwag NPM CLI
NSwag command line tool for .NET Core Net80, toolchain v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80
Executing dotnet exec --depsfile /Users/zyber/RiderProjects/NswagMinimumExample/NswagMinimumExample/bin/Debug/net8.0/NswagMinimumExample.deps.json --runtimeconfig /Users/zyber/RiderProjects/NswagMinimumExample/NswagMinimumExample/bin/Debug/net8.0/NswagMinimumExample.runtimeconfig.json /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80/NSwag.AspNetCore.Launcher.dll /var/folders/lw/nll50n7s615bktfmrr9czhrh0000gn/T/tmpeL4gwa.tmp /var/folders/lw/nll50n7s615bktfmrr9czhrh0000gn/T/tmphOInUr.tmp NswagMinimumExample /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80/
Launcher directory: /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.AggregateException: One or more errors occurred. (Changing the URL is not supported because no valid IServerAddressesFeature was found.)
 ---> System.InvalidOperationException: Changing the URL is not supported because no valid IServerAddressesFeature was found.
   at Microsoft.AspNetCore.Builder.WebApplication.Listen(String url)
   at Microsoft.AspNetCore.Builder.WebApplication.Run(String url)
   at Program.<Main>$(String[] args) in /Users/zyber/RiderProjects/NswagMinimumExample/NswagMinimumExample/Program.cs:line 16
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at NSwag.Commands.ServiceProviderResolver.GetServiceProviderWithHostFactoryResolver(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 171
   at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 70
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 27
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132


Done executing command. Exit Code: 1.
System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 195
   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 65node:child_process:960
    throw err;
    ^

Error: Command failed: dotnet "/Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80/dotnet-nswag.dll" aspnetcore2openapi /output:./test.swagger
    at checkExecSyncError (node:child_process:885:11)
    at Object.execSync (node:child_process:957:15)
    at /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/nswag.js:62:27
    at ChildProcess.exithandler (node:child_process:411:7)
    at ChildProcess.emit (node:events:514:28)
    at maybeClose (node:internal/child_process:1091:16)
    at ChildProcess._handle.onexit (node:internal/child_process:302:5) {
  status: 255,
  signal: null,
  output: [ null, null, null ],
  pid: 34824,
  stdout: null,
  stderr: null
}

Node.js v18.17.1

nswag version:

% npx nswag version  
NSwag NPM CLI
NSwag command line tool for .NET Core Net80, toolchain v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: /Users/zyber/.npm/_npx/58e08e1c8752b753/node_modules/nswag/bin/binaries/Net80

NSwag version: 14.1.0.0
NJsonSchema version: 11.0.2.0 (Newtonsoft.Json v13.0.0.0)

However, I can start the project and run it, access Swagger UI with the correct endpoint data.

If I move http://*:5000 from app.Run() into Services.Webhost.UseUrls() it works, but I'd like to avoid that - as I would use this in a "common" package amongst several services, and I'd like it to be a drop in replacement.

Is this the intended behavior? I've failed to find any documentation with regards to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant