This is a heck of a wall to hit at the end of a 1.5 week refactoring.
I've gotten it down to the bear minimum and I'm at an absolute loss
I start a SignalR webserver using owin selfhost (katana), connect to it. I shut it down, and then I start it and try to connect to it again.
On the second time through (it works just fine the first time) I get an error while trying to initiate a connection:
public partial class App : Application
{
void App_Startup(object sender, StartupEventArgs e)
{
using (var server = new TestWebServer()) {
server.Start();
using (var hubConnection = new HubConnection(TestWebServer.Host)) {
var proxy = hubConnection.CreateHubProxy("testHub");
hubConnection.Start().Wait();
Debug.Assert(hubConnection.State == ConnectionState.Connected);
}
}
// Makes it here fine
using (var server = new TestWebServer()) {
server.Start();
using (var hubConnection = new HubConnection(TestWebServer.Host)) {
var proxy = hubConnection.CreateHubProxy("testHub");
hubConnection.Start().Wait(); //<-throws "Transport timed out trying to connect"
Debug.Assert(hubConnection.State == ConnectionState.Connected);
}
}
}
}
With TestWebServer
:
public class TestWebServer : IDisposable {
public const string Host = "http://localhost:8081/";
public void Start() {
if (null != server) return;
this.server = WebApp.Start(Host, app => {
var cfg = new HubConfiguration { };
app.MapSignalR(cfg);
});
}
IDisposable server;
//Super-duper-for-real disposable https://lostechies.com/chrispatterson/2012/11/29/idisposable-done-right/
bool _disposed;
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
~TestWebServer() {
Dispose(false);
}
protected virtual void Dispose(bool disposing) {
if (_disposed)
return;
if (disposing) {
if (null == server) return;
server.Dispose();
server = null;
}
_disposed = true;
}
}
And TestHub
[HubName("testHub")]
public class TestHub : Hub {
public void Ping() => Clients.All.pong();
}
This is using SignalR 2.2.0 and I've tried both on .Net 4.5 and 4.6.1
This is as simple a SignalR application as I possibly know how to make it and you notice it works! Once. Then it fails when I dispose and try the exact same code again.
What is going on? Is there something else that I need to do to dispose of things?
Yes, I've tried to Thread.Sleep
between the two.