So I was stuck on this problem for about a week. I was trying to run a project to recieve a TCP connection and start a SignalR Hub as a Service. Both worked perfectly running the project as a .exe file. The TCP part would work perfectly, however I was having problems with the SignalR side.
The reason ended up being the using statement.
Before
using (WebApp.Start<SignalrStartup>(url))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Server running on {0}", url); // was url
Console.WriteLine("ID\tMessage");
Console.ReadLine();
}
After
WebApp.Start<SignalrStartup>(url);
I had tried running the the code with the Console.WriteLine()
commented out, as I thought it might be throwing an exception as the is no console to output to once run as a service. This also didn't work, but also wouldn't work as a .exe file either as it needed the Console.ReadLine()
to keep the console open, sort of how you need it to keep HelloWorld.cs open. Once the using wrapper was removed along with the console, it would then work in both the .exe and the service.
I have read that the using statement kills objects in it once you leave the wrapper. But I don't understand how the After bit of code keeps the .exe code open once running. Is there any point in using using or have I been using it wrong?
Edit
protected override void OnStart(string[] args)
{
Task.Factory
.StartNew(() => StartTCP())
.ContinueWith(t => StartSignalR());
}
The call is being made from the StartSignalR()
method.