diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index 176ffb2..b98e4d6 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -85,7 +85,7 @@ public class ApiServer(string apiUrl) : IApiServer protected void ListenerCallback(IAsyncResult result) { - HttpListenerContext context; + HttpListenerContext? context; // Skip if not lisstening anymore if (!httpListener.IsListening) @@ -96,15 +96,20 @@ public class ApiServer(string apiUrl) : IApiServer { context = httpListener.EndGetContext(result); } - catch (HttpListenerException) + catch (Exception ex) { - return; + Log.ErrorFormat("Error at getting context: ", ex.ToString()); + context = null; } // Immitatly listen for new request if (AllowMultipleRequests) Receive(); + // Cancel if we don't have a context + if (context is null) + return; + // Check context OnCheckContext?.Invoke(this, new(context)); try @@ -113,7 +118,7 @@ public class ApiServer(string apiUrl) : IApiServer } catch (Exception ex) { - Log.Error(ex.ToString()); + Log.ErrorFormat("Error at checking context: ", ex.ToString()); if (DebugMode) throw; }