Nick Wright
Nick Wright

Reputation: 111

SignalR service crashing on start up

I've just picked up a Signal R service from a predecessor. After installing, it will start and stop successfully. It might even do that a few times before failing to start with an error

at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(Owin.IAppBuilder)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(Microsoft.Owin.Hosting.Engine.StartContext)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(Microsoft.Owin.Hosting.Engine.StartContext)
   at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(Microsoft.Owin.Hosting.StartOptions)
   at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(Microsoft.Owin.Hosting.StartOptions)`

I've put error handling everywhere, but this error is not caught within the application, just the event log.

I found the following code in the service startup:

        using (WebApp.Start<Startup>(this._signalRURL))
        {
            Thread.Sleep(Timeout.Infinite);

        }

The service starts a thread and then sleeps forever! I don't know why he would have done this, but I'm assuming there was a very good reason. I've removed the thread.sleep line and the service now runs and does not throw an error.

So, my questions are:

I don't want to take out the code until I know why it was there in the first place.

Upvotes: 1

Views: 342

Answers (1)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

why is the thread sent to sleep forever?

He is trying to create a long running thread or message loop on the current thread in order to avoid the exit of the main thread. It's a kind of Application.Run see here for more information.

Because when you exit the using statement the WebApp will be disposed and SignalR will be no more available

why is he using a "using" statement?

There is no reasons to use the using statement because the SignalR was intended to run until the end of application instance life

update

what I can suggest is to declare the var of type IDiposable like the folllowing

//your windows service
public class RunningInstance : ServiceBase
    { 
       //this will  be outscope of method start and WebbApp will  not disposed 
       static IDisposable signalRApp;
       //and in your onstart   

        protected override void OnStart(string[] args)
        {

             signalRApp=WebApp.Start<Startup>(this._signalRURL);
        }

    }

Note

when you assign your IDisposable to a static field it will be loaded when your type is loaded (ServiceBase in this case), so your object will be disposed when your containing type is unloaded

Upvotes: 2

Related Questions