Carael / CrossBusExplorer

Cross platform Azure Service Bus explorer
MIT License
51 stars 6 forks source link

Make port configurable (through config file or command line parameter?) #30

Closed Stitch10925 closed 1 year ago

Stitch10925 commented 1 year ago

Currently the application starts up and tries to open on port 8001. On my machine this port is already in use by another application which I had to kill in order to use CrossBus.

Besides that CrossBus looks very promising! It's fast, easy on the eyes and easy to use.

Carael commented 1 year ago

Theoretically the Electron wrapper should already return a free port - it's done with the following code:

  public static int FreeTcpPort()
  {
    TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 0);
    tcpListener.Start();
    int port = ((IPEndPoint) tcpListener.LocalEndpoint).Port;
    tcpListener.Stop();
    return port;
  }

What do you get when you run this local? Does it give you always 8001?

Passing the port should be possible already now. The electron wrapper uses this code to replace the ELECTRONPORT and ELECTRONWEBPORT. I'm passing the args to it so this should already work.

foreach (string str in args)
{
  if (str.ToUpper().Contains("ELECTRONPORT"))
  {
    BridgeSettings.SocketPort = str.ToUpper().Replace("/ELECTRONPORT=", "");
    BridgeConnector.Log("Use Electron Port: " + BridgeSettings.SocketPort);
  }
  else if (str.ToUpper().Contains("ELECTRONWEBPORT"))
    BridgeSettings.WebPort = str.ToUpper().Replace("/ELECTRONWEBPORT=", "");
}

According to the above code I guess you need to pass it with slash so: '/ELECTRONWEBPORT' and '/ELECTRONPORT'.

Please test and if still doesn't work we will troubleshoot further.

Stitch10925 commented 1 year ago

Your piece of code does indeed return a random port. So that seems to be working.

I did get the following errors when starting the application for the first time:

Application: Website.Host.exe CoreCLR Version: 7.0.423.11508 .NET Version: 7.0.4 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.IOException: Failed to bind to address http://127.0.0.1:8001: address already in use. ---> Microsoft.AspNetCore.Connections.AddressInUseException: Only one usage of each socket address (protocol/network address/port) is normally permitted. ---> System.Net.Sockets.SocketException (10048): Only one usage of each socket address (protocol/network address/port) is normally permitted. at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, Boolean disconnectOnFailure, String callerName) at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint) at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind() --- End of inner exception stack trace --- at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_01.<<StartAsync>g__OnBind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken) --- End of inner exception stack trace --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable1 listenOptions, AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) at Program.

$(String[] args) in /Users/runner/work/CrossBusExplorer/CrossBusExplorer/src/Ui/Website.Host/Program.cs:line 54

I tried opening and closing the application multiple times, but in the development tools it keeps showing port 8001.

image


With regards to changing the port, I am not quite sure how to test is (I don't know electron). Is the "/ELECTRONWEBPORT" a commandline parameter?

Carael commented 1 year ago

Ok I will take a look why it's forcing the 8001 - it's the same on Mac so I guess the code to select free port is somehow not used.

Regarding the parameters - yes those are command line parameters - try to pass them both and let see what happens. Sadly cannot test this ATM on Windows.

Carael commented 1 year ago

Looks like Windows specific issue. On Mac if the 8001 port is taken the next one is selected: image

Stitch10925 commented 1 year ago

I tried the following, but didn't work.

"C:\Users\xxxxxxxxxxxx\AppData\Local\Programs\cross-bus-explorer\Cross Bus Explorer.exe" /ELECTRONPORT=12345 /ELECTRONWEBPORT=12345

also the following didn't work:

"C:\Users\xxxxxxxxxxxx\AppData\Local\Programs\cross-bus-explorer\Cross Bus Explorer.exe" -ELECTRONPORT=12345 -ELECTRONWEBPORT=12345

and this also not:

"C:\Users\xxxxxxxxxxxx\AppData\Local\Programs\cross-bus-explorer\Cross Bus Explorer.exe" ELECTRONPORT=12345 ELECTRONWEBPORT=12345

Stitch10925 commented 1 year ago

When starting the application via command line I could see that .NET was having issues with the port. I managed to change the port by updating the appsettings file in the bin library, but now electron is not happy since it is still searching on port 8001.

Carael commented 1 year ago

Found the port can be configured in the electron.manifest.json. Set it now to 8010. Otherwise it's set automatically - didn't figured out why it was not happening on your Windows. The suspection is that it may be blocked by firewall - so you can either try to add an extension to the Firewall/Defender for the app or try the new release 0.3.5.

Carael commented 1 year ago

Closing this one since no new input was provided.