| | | 1 | | #if NET45 |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using WireMock.Validation; |
| | | 8 | | using Owin; |
| | | 9 | | using Microsoft.Owin.Hosting; |
| | | 10 | | |
| | | 11 | | namespace WireMock.Owin |
| | | 12 | | { |
| | | 13 | | internal class OwinSelfHost : IOwinSelfHost |
| | | 14 | | { |
| | | 15 | | private readonly WireMockMiddlewareOptions _options; |
| | 18 | 16 | | private readonly CancellationTokenSource _cts = new CancellationTokenSource(); |
| | | 17 | | private System.Threading.Thread _internalThread; |
| | | 18 | | |
| | 18 | 19 | | public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes) |
| | 18 | 20 | | { |
| | 18 | 21 | | Check.NotNull(options, nameof(options)); |
| | 18 | 22 | | Check.NotEmpty(uriPrefixes, nameof(uriPrefixes)); |
| | | 23 | | |
| | 90 | 24 | | foreach (string uriPrefix in uriPrefixes) |
| | 18 | 25 | | { |
| | 18 | 26 | | var uri = new Uri(uriPrefix); |
| | 18 | 27 | | Urls.Add(uri); |
| | 18 | 28 | | Ports.Add(uri.Port); |
| | 18 | 29 | | } |
| | | 30 | | |
| | 18 | 31 | | _options = options; |
| | 18 | 32 | | } |
| | | 33 | | |
| | 29 | 34 | | public bool IsStarted { get; private set; } |
| | | 35 | | |
| | 52 | 36 | | public List<Uri> Urls { get; } = new List<Uri>(); |
| | | 37 | | |
| | 54 | 38 | | public List<int> Ports { get; } = new List<int>(); |
| | | 39 | | |
| | | 40 | | [PublicAPI] |
| | | 41 | | public Task StartAsync() |
| | 18 | 42 | | { |
| | 18 | 43 | | return Task.Run(() => |
| | 34 | 44 | | { |
| | 34 | 45 | | StartServers(); |
| | 31 | 46 | | }, _cts.Token); |
| | | 47 | | |
| | | 48 | | //if (_internalThread != null) |
| | | 49 | | // throw new InvalidOperationException("Cannot start a multiple threads."); |
| | | 50 | | |
| | | 51 | | //_internalThread = new Thread(ThreadWorkInternal); |
| | | 52 | | //_internalThread.Start(); |
| | 18 | 53 | | } |
| | | 54 | | |
| | | 55 | | [PublicAPI] |
| | | 56 | | public Task StopAsync() |
| | 18 | 57 | | { |
| | 18 | 58 | | _cts.Cancel(); |
| | | 59 | | |
| | 18 | 60 | | var tcs = new TaskCompletionSource<bool>(); |
| | 18 | 61 | | var timer = new System.Timers.Timer(999); |
| | 18 | 62 | | timer.Elapsed += (sender, e) => |
| | 31 | 63 | | { |
| | 31 | 64 | | if (_internalThread == null) |
| | 31 | 65 | | { |
| | 31 | 66 | | timer.Stop(); |
| | 31 | 67 | | tcs.SetResult(true); |
| | 31 | 68 | | } |
| | 31 | 69 | | }; |
| | 18 | 70 | | timer.Start(); |
| | | 71 | | |
| | 18 | 72 | | return tcs.Task; |
| | 18 | 73 | | } |
| | | 74 | | |
| | | 75 | | private void ThreadWorkInternal() |
| | 0 | 76 | | { |
| | | 77 | | try |
| | 0 | 78 | | { |
| | 0 | 79 | | StartServers(); |
| | 0 | 80 | | } |
| | 0 | 81 | | catch (Exception ex) |
| | 0 | 82 | | { |
| | 0 | 83 | | Console.WriteLine(ex); |
| | 0 | 84 | | } |
| | | 85 | | finally |
| | 0 | 86 | | { |
| | 0 | 87 | | _internalThread = null; |
| | 0 | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | private void StartServers() |
| | 16 | 92 | | { |
| | 16 | 93 | | Action<IAppBuilder> startup = app => |
| | 32 | 94 | | { |
| | 32 | 95 | | app.Use<WireMockMiddleware>(_options); |
| | 32 | 96 | | }; |
| | | 97 | | |
| | 16 | 98 | | var servers = new List<IDisposable>(); |
| | 80 | 99 | | foreach (var url in Urls) |
| | 16 | 100 | | { |
| | 16 | 101 | | servers.Add(WebApp.Start(url.ToString(), startup)); |
| | 16 | 102 | | } |
| | | 103 | | |
| | 16 | 104 | | IsStarted = true; |
| | | 105 | | |
| | 27 | 106 | | while (!_cts.IsCancellationRequested) |
| | 14 | 107 | | Thread.Sleep(1000); |
| | | 108 | | |
| | 13 | 109 | | IsStarted = false; |
| | | 110 | | |
| | 65 | 111 | | foreach (var server in servers) |
| | 13 | 112 | | server.Dispose(); |
| | 13 | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | #endif |