Windows服务:定义启动后运行的asynchnous方法 - SignalR Client

时间:2014-02-07 14:12:19

标签: c# windows-services signalr

我的Windows服务很简单。我需要它成为SignalR客户端。 SignalR启动代码包括await所以我需要一个异步方法。我不能把它放在OnStart方法中,但我需要在启动后发生这种情况。

SignalR代码如下:

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();

如何让它在启动后运行?

1 个答案:

答案 0 :(得分: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);
            }
        }