Dispose properly
MODIFY
HiddenWallet.ChaumianTumbler/Global.cs
HiddenWallet.ChaumianTumbler/Global.cs
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Threading;
using System.Threading.Tasks;
namespace HiddenWallet.ChaumianTumbler
@@ -11,6 +12,8 @@ namespace HiddenWallet.ChaumianTumbler
public static Config Config;
public static TumblerStateMachine StateMachine;
+ public static Task StateMachineJob;
+ public static CancellationTokenSource StateMachineJobCancel;
private static string _dataDir = null;
public static string DataDir

MODIFY
HiddenWallet.ChaumianTumbler/HiddenWallet.ChaumianTumbler.csproj
HiddenWallet.ChaumianTumbler/HiddenWallet.ChaumianTumbler.csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-alpha2-final" />
+ <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
</ItemGroup>
<ItemGroup>

MODIFY
HiddenWallet.ChaumianTumbler/Program.cs
HiddenWallet.ChaumianTumbler/Program.cs
@@ -33,23 +33,16 @@ namespace HiddenWallet.ChaumianTumbler
Console.WriteLine($"Config file did not exist. Created at path: {configFilePath}");
}
+ Global.StateMachine = new TumblerStateMachine();
+ Global.StateMachineJobCancel = new CancellationTokenSource();
+ Global.StateMachineJob = Global.StateMachine.StartAsync(Global.StateMachineJobCancel.Token);
+
using (var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build())
- using (var ctsSource = new CancellationTokenSource())
- using (Global.StateMachine = new TumblerStateMachine())
- {
- var stateMachineTask = Global.StateMachine.StartAsync(ctsSource.Token);
- try
{
await host.RunAsync();
}
- finally
- {
- ctsSource.Cancel();
- await stateMachineTask;
- }
- }
}
}
}

MODIFY
HiddenWallet.ChaumianTumbler/Startup.cs
HiddenWallet.ChaumianTumbler/Startup.cs
@@ -9,6 +9,8 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.SignalR;
+using System.IO;
+using Swashbuckle.AspNetCore.Swagger;
namespace HiddenWallet.ChaumianTumbler
{
@@ -26,6 +28,10 @@ namespace HiddenWallet.ChaumianTumbler
{
services.AddSignalR(); //Must come before .AddMvc()
services.AddMvc();
+ services.AddSwaggerGen(c =>
+ {
+ c.SwaggerDoc("v1", new Info { Title = "HiddenWallet.ChaumianTumbler", Version = "v1" });
+ });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -43,8 +49,29 @@ namespace HiddenWallet.ChaumianTumbler
app.UseMvc();
+ var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
+ applicationLifetime.ApplicationStopping.Register(OnShutdownAsync);
+
+ app.UseSwagger();
+
+ // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
+ app.UseSwaggerUI(c =>
+ {
+ c.SwaggerEndpoint("/swagger/v1/swagger.json", "HiddenWallet.ChaumianTumbler V1");
+ });
+
TumblerPhaseBroadcaster tumblerPhaseBroadcast = TumblerPhaseBroadcaster.Instance;
tumblerPhaseBroadcast.SignalRHub = context;
}
+
+ private async void OnShutdownAsync()
+ {
+ Global.StateMachineJobCancel?.Cancel();
+ if(Global.StateMachineJob != null)
+ {
+ await Global.StateMachineJob;
+ }
+ Global.StateMachine?.Dispose();
+ }
}
}

Dispose properly