mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 09:18:27 +02:00
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:
116
src/WireMock.Net/Owin/OwinSelfHost.cs
Normal file
116
src/WireMock.Net/Owin/OwinSelfHost.cs
Normal 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
|
||||
Reference in New Issue
Block a user