Changed RouteListener to attempt to listen on all interfaces, if denied it falls back to local. Bumped package version to 1.4.4

This commit is contained in:
MattMo 2023-03-28 06:20:20 -07:00
parent 0c8467f942
commit 71941f5dd0
2 changed files with 23 additions and 12 deletions

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.4.3</Version>
<Version>1.4.4</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -93,18 +93,29 @@ namespace MontoyaTech.Rest.Net
{
this.HttpListener = new HttpListener();
//Support listening on Windows & Linux.
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
this.HttpListener.Prefixes.Add($"http://localhost:{this.Port}/");
this.HttpListener.Prefixes.Add($"http://127.0.0.1:{this.Port}/");
}
else
{
this.HttpListener.Prefixes.Add($"http://*:{this.Port}/");
}
//Attempt to listen on all interfaces first.
this.HttpListener.Prefixes.Add($"http://*:{this.Port}/");
this.HttpListener.Start();
try
{
this.HttpListener.Start();
}
catch (HttpListenerException ex)
{
//If this failed and it's because we are not admin, try again with the local prefixes
if (ex.Message.ToLower().StartsWith("access is denied"))
{
this.HttpListener = new HttpListener();
this.HttpListener.Prefixes.Add($"http://localhost:{this.Port}/");
this.HttpListener.Prefixes.Add($"http://127.0.0.1:{this.Port}/");
this.HttpListener.Start();
}
else
{
throw;
}
}
this.Listen();
}