Move port usage (#504)

* Moved port usage code check to before startup

* removed unused import

---------

Co-authored-by: Alex <clodanSPT@hotmail.com>
This commit is contained in:
clodanSPT
2025-07-23 08:44:34 +01:00
committed by GitHub
parent 2983edd51b
commit 2d752457b2
3 changed files with 15 additions and 32 deletions
@@ -58,30 +58,6 @@ public class HttpServerHelper(ConfigServer configServer)
return $"wss://{BuildUrl()}";
}
/// <summary>
/// Method to determine if another version of the server is already running
/// </summary>
/// <returns>bool isAlreadyRunning</returns>
public bool IsAlreadyRunning()
{
TcpListener? listener = null;
try
{
listener = new(IPAddress.Parse(_httpConfig.Ip), _httpConfig.Port);
listener.Start();
return false;
}
catch (Exception)
{
return true;
}
finally
{
listener?.Stop();
}
}
public void SendTextJson(HttpResponse resp, object output)
{
resp.Headers.Append("Content-Type", mime["json"]);
@@ -35,13 +35,6 @@ public class App(
{
ServiceLocator.SetServiceProvider(_serviceProvider);
var isAlreadyRunning = _httpServerHelper.IsAlreadyRunning();
if (isAlreadyRunning)
{
_logger.Critical(_serverLocalisationService.GetText("webserver_already_running"));
await Task.Delay(Timeout.Infinite);
}
if (_logger.IsLogEnabled(LogLevel.Debug))
{
_logger.Debug(
+15 -1
View File
@@ -1,5 +1,5 @@
using System.Net;
using System.Runtime;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Text;
@@ -126,6 +126,20 @@ public static class Program
var httpConfig = options
.ApplicationServices.GetService<ConfigServer>()
?.GetConfig<HttpConfig>()!;
// Probe the http ip and port to see if its being used, this method will throw an exception and crash
// the server if the IP/Port combination is already in use
TcpListener? listener = null;
try
{
listener = new TcpListener(IPAddress.Parse(httpConfig.Ip), httpConfig.Port);
listener.Start();
}
finally
{
listener?.Stop();
}
var certHelper = options.ApplicationServices.GetService<CertificateHelper>()!;
options.Listen(
IPAddress.Parse(httpConfig.Ip),