I am writing a wpf application where the user should be able to start and stop a web server. I'm using owin, signalr, and nancy-fx for my server. The problem I'm having is that after stopping the web server I can not start it again. I get a timeout error when trying to do _hubConnection.Start().Wait()
. From what I've found online I am stopping the server correctly but maybe not?
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at WebServer.StartSocketConnection() in C:\...\WebServer.vb:line 102
at MainWindow.mnuEnableWebServer_OnClick(Object sender, RoutedEventArgs e) in C:\...\MainWindow.xaml.vb:line 320
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
Inner Exception 1:
TimeoutException: Transport timed out trying to connect
This is my webserver class
Public Class WebServer
Dim _hubConnection As HubConnection
Dim _hubProxy As HubProxy
Dim _server As IDisposable
Private Shared _instance As WebServer
Private Property Bootstrapper As CustomBootstrapper
Public Property LocalAddress As String
Public Property Port As Integer
Public ReadOnly Property Url As String
Get
Return String.Format("http://{0}:{1}/", LocalAddress, Port)
End Get
End Property
Public Shared ReadOnly Property Instance As WebServer
Get
If _instance Is Nothing Then
_instance = New WebServer()
End If
Return _instance
End Get
End Property
Public Sub New()
LocalAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList().ToList().Last().ToString()
Port = 8080
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(6)
End Sub
Public Sub StartHost()
Bootstrapper = New CustomBootstrapper()
_server = WebApp.Start(Url, Function(app)
app.Map("/signalr", Function(map)
map.UseCors(CorsOptions.AllowAll)
Dim hubConfiguration = New HubConfiguration()
map.RunSignalR(hubConfiguration)
End Function).UseNancy(Function(options)
options.Bootstrapper = Bootstrapper
End Function)
End Function)
End Sub
Public Sub StartSocketConnection()
_hubConnection = New HubConnection(Url)
_hubProxy = _hubConnection.CreateHubProxy("myHub")
_hubConnection.Start().Wait()
End Sub
Public Sub StopServer()
_hubConnection.Stop()
_server.Dispose()
End Sub
End Class
And this is how I call my start and stop methods
Private Sub mnuEnableWebServer_OnClick(sender As Object, e As RoutedEventArgs)
WebServer.Instance.StartHost()
WebServer.Instance.StartSocketConnection()
End Sub
Private Sub mnuDisableWebServer_OnClick(sender As Object, e As RoutedEventArgs)
WebServer.Instance.StopServer()
End Sub
User contributions licensed under CC BY-SA 3.0