From badcec1ed20ea96d5263c23f4c8372e3db5ca5c9 Mon Sep 17 00:00:00 2001 From: Thomas <71355143+thomas694@users.noreply.github.com> Date: Thu, 5 May 2022 21:53:35 +0200 Subject: [PATCH] Fix issue PlatformNotSupportedException opening image viewer - 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. --- src/MainWindow.xaml.cs | 8 ++++++-- src/MediaFileServer.cs | 6 +++++- src/Properties/AssemblyInfo.cs | 6 +++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/MainWindow.xaml.cs b/src/MainWindow.xaml.cs index 90b185e..cf44f41 100644 --- a/src/MainWindow.xaml.cs +++ b/src/MainWindow.xaml.cs @@ -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(); } } diff --git a/src/MediaFileServer.cs b/src/MediaFileServer.cs index aef2943..b0874c8 100644 --- a/src/MediaFileServer.cs +++ b/src/MediaFileServer.cs @@ -6,7 +6,7 @@ namespace WpfImageViewer { internal class MediaFileServer { - private readonly HttpListener _httpListener = new HttpListener(); + private readonly HttpListener _httpListener; private readonly Func _requestHandlerMethod; private string _prefix; @@ -14,6 +14,10 @@ internal class MediaFileServer public MediaFileServer(string prefix, Func requestHandlerMethod) { + if (!HttpListener.IsSupported) throw new NotSupportedException(); + + _httpListener = new HttpListener(); + _ = prefix ?? throw new ArgumentNullException(nameof(prefix)); _ = requestHandlerMethod ?? throw new ArgumentNullException(nameof(requestHandlerMethod)); diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 83c60c1..0fc422f 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -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("")] @@ -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")]