diff --git a/CV19.Web/CV19.Web.csproj b/CV19.Web/CV19.Web.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/CV19.Web/CV19.Web.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/CV19.Web/WebServer.cs b/CV19.Web/WebServer.cs new file mode 100644 index 0000000..432075c --- /dev/null +++ b/CV19.Web/WebServer.cs @@ -0,0 +1,80 @@ +using System; +using System.Net; +using System.Threading.Tasks; + +namespace CV19.Web +{ + public class WebServer + { + public event EventHandler RequestReceived; + + //private TcpListener _Listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8080)); + private HttpListener _Listener; + private readonly int _Port; + private bool _Enabled; + private readonly object _SyncRoot = new object(); + + public int Port => _Port; + + public bool Enabled { get => _Enabled; set { if (value) Start(); else Stop(); } } + + public WebServer(int Port) => _Port = Port; + + public void Start() + { + if (_Enabled) return; + lock (_SyncRoot) + { + if (_Enabled) return; + + _Listener = new HttpListener(); + _Listener.Prefixes.Add($"http://*:{_Port}/"); // netsh http add urlacl url=http://*:8080/ user=user_name + _Listener.Prefixes.Add($"http://+:{_Port}/"); + _Enabled = true; + ListenAsync(); + } + } + + public void Stop() + { + if (!_Enabled) return; + lock (_SyncRoot) + { + if (!_Enabled) return; + + _Listener = null; + _Enabled = false; + } + } + + private async void ListenAsync() + { + var listener = _Listener; + + listener.Start(); + + HttpListenerContext context = null; + while (_Enabled) + { + var get_context_task = listener.GetContextAsync(); + if (context != null) + ProcessRequestAsync(context); + context = await get_context_task.ConfigureAwait(false); + } + + listener.Stop(); + } + + private async void ProcessRequestAsync(HttpListenerContext context) + { + await Task.Run(() => RequestReceived?.Invoke(this, new RequestReceiverEventArgs(context))); + } + } + + public class RequestReceiverEventArgs : EventArgs + { + public HttpListenerContext Context { get; } + + public RequestReceiverEventArgs(HttpListenerContext context) => Context = context; + } +} diff --git a/CV19.sln b/CV19.sln index 78a07fe..975e3b3 100644 --- a/CV19.sln +++ b/CV19.sln @@ -3,13 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30104.148 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CV19", "CV19\CV19.csproj", "{FC92495F-1218-49A6-B295-E2EC2FEE3A67}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CV19", "CV19\CV19.csproj", "{FC92495F-1218-49A6-B295-E2EC2FEE3A67}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{E4416DE8-06BF-433C-A992-1FD7803895E3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CV19Console", "Tests\CV19Console\CV19Console.csproj", "{6C8C5A2C-B181-49C3-A218-1150B0635A3A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CV19Console", "Tests\CV19Console\CV19Console.csproj", "{6C8C5A2C-B181-49C3-A218-1150B0635A3A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CV19WPFTest", "Tests\CV19WPFTest\CV19WPFTest.csproj", "{16133638-8F17-4B64-9815-7E6178544A1C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CV19WPFTest", "Tests\CV19WPFTest\CV19WPFTest.csproj", "{16133638-8F17-4B64-9815-7E6178544A1C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CV19WinFormsTest", "Tests\CV19WinFormsTest\CV19WinFormsTest.csproj", "{8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CV19.Web", "CV19.Web\CV19.Web.csproj", "{16558D4A-3611-4D65-B6ED-97ED75DBCAC7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -29,6 +33,14 @@ Global {16133638-8F17-4B64-9815-7E6178544A1C}.Debug|Any CPU.Build.0 = Debug|Any CPU {16133638-8F17-4B64-9815-7E6178544A1C}.Release|Any CPU.ActiveCfg = Release|Any CPU {16133638-8F17-4B64-9815-7E6178544A1C}.Release|Any CPU.Build.0 = Release|Any CPU + {8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D}.Release|Any CPU.Build.0 = Release|Any CPU + {16558D4A-3611-4D65-B6ED-97ED75DBCAC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {16558D4A-3611-4D65-B6ED-97ED75DBCAC7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {16558D4A-3611-4D65-B6ED-97ED75DBCAC7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {16558D4A-3611-4D65-B6ED-97ED75DBCAC7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -36,6 +48,7 @@ Global GlobalSection(NestedProjects) = preSolution {6C8C5A2C-B181-49C3-A218-1150B0635A3A} = {E4416DE8-06BF-433C-A992-1FD7803895E3} {16133638-8F17-4B64-9815-7E6178544A1C} = {E4416DE8-06BF-433C-A992-1FD7803895E3} + {8EB32BD1-1FAD-4FE5-BA31-D974CD1CAF9D} = {E4416DE8-06BF-433C-A992-1FD7803895E3} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9234878E-A7A4-4B01-BBF9-00E7090A01BA} diff --git a/CV19/App.xaml b/CV19/App.xaml index cb659b4..585d6e9 100644 --- a/CV19/App.xaml +++ b/CV19/App.xaml @@ -12,6 +12,7 @@ + diff --git a/CV19/App.xaml.cs b/CV19/App.xaml.cs index f96593e..b9c727c 100644 --- a/CV19/App.xaml.cs +++ b/CV19/App.xaml.cs @@ -37,15 +37,9 @@ protected override async void OnExit(ExitEventArgs e) __Host = null; } - public static void ConfigureServices(HostBuilderContext host, IServiceCollection services) - { - services.AddSingleton(); - //services.AddTransient(); - //services.AddScoped(); - - services.AddSingleton(); - services.AddSingleton(); - } + public static void ConfigureServices(HostBuilderContext host, IServiceCollection services) => services + .RegisterServices() + .RegisterViewModels(); public static string CurrentDirectory => IsDesignMode ? Path.GetDirectoryName(GetSourceCodePath()) diff --git a/CV19/CV19.csproj b/CV19/CV19.csproj index bb50a0b..090ed08 100644 --- a/CV19/CV19.csproj +++ b/CV19/CV19.csproj @@ -14,12 +14,26 @@ + + + + + - + - + + + + + + + + + + diff --git a/CV19/Infrastructure/Behaviors/CloseWindow.cs b/CV19/Infrastructure/Behaviors/CloseWindow.cs new file mode 100644 index 0000000..73cc8ce --- /dev/null +++ b/CV19/Infrastructure/Behaviors/CloseWindow.cs @@ -0,0 +1,17 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using Microsoft.Xaml.Behaviors; + +namespace CV19.Infrastructure.Behaviors +{ + class CloseWindow : Behavior