dotnet / WatsonWebserver

Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
MIT License
412 stars 85 forks source link

No way to catch port in use #46

Closed at1993 closed 4 years ago

at1993 commented 4 years ago

In the AcceptConnections method, the httplisterner.Start method may fail (ex. port in use). Since the HttpListernerException is not signaled to the application. It is sometime difficult to figure out why the application is not working. In the meantime I added the try/catch around the _HttpListener.Start() method

try { _HttpListener.Start(); } catch (HttpListenerException er) { Events.ExceptionEncountered?.Invoke(null, 0, er); }

@jchristn, am I correct or am I missing something?

Thanks.

jchristn commented 4 years ago

Hi @at1993 - does the ServerStopped event fire correctly? We could definitely fire the ExceptionEncountered event there too if it's of value. Since the listener runs as a background task, one remedy would be to make an explicit .Start() method that returns a bool indicating success. Would that meet the need of helping to identify the port was in use, along with the ExceptionEncountered event?

at1993 commented 4 years ago

Thanks @jchristn for the quick reply. Yes, the ServerStopped event was fired correctly. It would be nice to know why the server stopped right after it was started. I really like the second idea where you make an explicit.Start() method. That will be awesome. Thank you.

jchristn commented 4 years ago

Awesome. I'll probably throw the exception as well in addition to firing the event. Will add it to my list (will be a breaking change so I'll bump to v3.2)

at1993 commented 4 years ago

Thank you!

jchristn commented 4 years ago

Starting work on this now

jchristn commented 4 years ago

Hi @at1993 please let me know if this works out for you (please feel free to close the issue if it does).

Note you'll have to call Start() now (and there's also a Stop() API)

NuGet v3.2.0: https://www.nuget.org/packages/Watson/3.2.0 Commit: https://github.com/jchristn/WatsonWebserver/commit/743cce1374b2f1ae537ec8324c601d3ca366634f

jchristn commented 4 years ago

Closing, please re-open if issues