Skip to content

Commit

Permalink
Fix issue PlatformNotSupportedException opening image viewer
Browse files Browse the repository at this point in the history
- Under some conditions it's not possible to start a HttpListener even it's generally supported on the platform.
- So check the IsSupported property first and continue without if necessary.
  • Loading branch information
thomas694 committed May 5, 2022
1 parent 6b722f7 commit badcec1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,16 @@ private void Initialize()
_mediaFileServer = new MediaFileServer($"http://localhost:{port}/", RequestHandlerMethod);
port = 0;
}
catch (NotSupportedException)
{
port = 0;
}
catch (HttpListenerException)
{
port = new Random().Next(50000, 65000);
}
} while (port != 0 || count < 3);
_mediaFileServer.Run();
} while (port != 0 && count < 3);
_mediaFileServer?.Run();
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/MediaFileServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ namespace WpfImageViewer
{
internal class MediaFileServer
{
private readonly HttpListener _httpListener = new HttpListener();
private readonly HttpListener _httpListener;
private readonly Func<HttpListenerRequest, byte[]> _requestHandlerMethod;
private string _prefix;

public string Prefix { get => _prefix; }

public MediaFileServer(string prefix, Func<HttpListenerRequest, byte[]> requestHandlerMethod)
{
if (!HttpListener.IsSupported) throw new NotSupportedException();

_httpListener = new HttpListener();

_ = prefix ?? throw new ArgumentNullException(nameof(prefix));
_ = requestHandlerMethod ?? throw new ArgumentNullException(nameof(requestHandlerMethod));

Expand Down
6 changes: 3 additions & 3 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WpfImageViewer")]
[assembly: AssemblyCopyright("Copyright © 2021 thomas694 (@GH)")]
[assembly: AssemblyCopyright("Copyright © 2022 thomas694 (@GH)")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down Expand Up @@ -48,5 +48,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyVersion("1.3.2.0")]
[assembly: AssemblyFileVersion("1.3.2.0")]

0 comments on commit badcec1

Please sign in to comment.