private bool AttemptToOpenPort()
{
try
{
Logger.WriteMinorEvent("Attempting to start http listener on "+ ServerUrlEndingInSlash);
_listener = new HttpListener {AuthenticationSchemes = AuthenticationSchemes.Anonymous};
_listener.Prefixes.Add(ServerUrlEndingInSlash);
_listener.Start();
return true;
}
catch(HttpListenerException error)
{
Logger.WriteEvent("Here, file not found is actually what you get if the port is in use:" + error.Message);
if (!Program.RunningUnitTests)
NonFatalProblem.Report(ModalIf.None,PassiveIf.Alpha, "Could not open " + ServerUrlEndingInSlash, "Could not start server on that port", error);
try
{
if(_listener != null)
{
//_listener.Stop(); this will always throw if we failed to start, so skip it and go to the close:
_listener.Close();
}
}
catch(Exception)
{
//that's ok, we're just trying to clean up
}
finally
{
_listener = null;
}
return false;
}
}