0

I have a Windows Service that is bare-bones. I need it to be a SignalR Client. The SignalR startup code includes an await so I need an asynchronous method. I can't put it in the OnStart method, yet I need this to happen after the startup has occured.

The SignalR code would be something like:

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();

How do I this having it run after startup?

1 Answer 1

0
 protected override async void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
            try
            {
                var hubConnection = new HubConnection("http://localhost/AlphaFrontEndService/signalr", useDefaultUrl: false);
                IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");

                await hubConnection.Start();
                // Invoke method on hub
                await alphaProxy.Invoke("Hello", "Message from Service");

            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message);
            }
        }
1
  • If OnStart itself can't be async, you can simply put the above code in another async method that is called from OnStart. You can then choose to use Task.Wait or Task.ContinueWith when you call the async method in OnStart depending on whether you want to block or not block.
    – halter73
    Feb 7, 2014 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.