merge netstandard into main (#26)

* #23

* #23 "Newtonsoft.Json" Version="10.0.2"

* owin

* AspNetCore

* Fix appveyor build

* fix start/stop in untitests
This commit is contained in:
Stef Heyenrath
2017-04-26 21:28:12 +02:00
committed by GitHub
parent 0f8f9c508f
commit 453cef90e5
106 changed files with 17753 additions and 24342 deletions

View File

@@ -0,0 +1,116 @@
#if NET45
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Validation;
using Owin;
using Microsoft.Owin.Hosting;
namespace WireMock.Owin
{
internal class OwinSelfHost : IOwinSelfHost
{
private readonly WireMockMiddlewareOptions _options;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private System.Threading.Thread _internalThread;
public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
{
Check.NotNull(options, nameof(options));
Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
foreach (string uriPrefix in uriPrefixes)
{
var uri = new Uri(uriPrefix);
Urls.Add(uri);
Ports.Add(uri.Port);
}
_options = options;
}
public bool IsStarted { get; private set; }
public List<Uri> Urls { get; } = new List<Uri>();
public List<int> Ports { get; } = new List<int>();
[PublicAPI]
public Task StartAsync()
{
return Task.Run(() =>
{
StartServers();
}, _cts.Token);
//if (_internalThread != null)
// throw new InvalidOperationException("Cannot start a multiple threads.");
//_internalThread = new Thread(ThreadWorkInternal);
//_internalThread.Start();
}
[PublicAPI]
public Task StopAsync()
{
_cts.Cancel();
var tcs = new TaskCompletionSource<bool>();
var timer = new System.Timers.Timer(999);
timer.Elapsed += (sender, e) =>
{
if (_internalThread == null)
{
timer.Stop();
tcs.SetResult(true);
}
};
timer.Start();
return tcs.Task;
}
private void ThreadWorkInternal()
{
try
{
StartServers();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
_internalThread = null;
}
}
private void StartServers()
{
Action<IAppBuilder> startup = app =>
{
app.Use<WireMockMiddleware>(_options);
};
var servers = new List<IDisposable>();
foreach (var url in Urls)
{
servers.Add(WebApp.Start(url.ToString(), startup));
}
IsStarted = true;
while (!_cts.IsCancellationRequested)
Thread.Sleep(1000);
IsStarted = false;
foreach (var server in servers)
server.Dispose();
}
}
}
#endif