private void Listen() { if (_tokenSource != null && !_tokenSource.IsCancellationRequested) { _startAgain = true; InterruptListening(); return; } _tokenSource = new CancellationTokenSource(); var token = _tokenSource.Token; Task.Run(async() => { while (!_tokenSource.IsCancellationRequested) { var message = await _queueListener.StartListening(); if (message != null) { OnMessageDelivered(message); } } }, token).ContinueWith(t => { if (!_startAgain) { return; } _startAgain = false; StartListening(); }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); _queueListener = new QueueListener(); _queueListener.Received += _queueListener_Received; _queueListener.StartListening("response"); }
static void Main(string[] args) { var host = new ServiceHost(typeof(StockService)); host.Open(); var queueListener = new QueueListener(); queueListener.StartListening("request", true); Console.WriteLine("Press <Enter> to stop the services."); Console.ReadLine(); queueListener.Dispose(); host.Close(); }
void StartListening() { _queueListener = new QueueListener(); _queueListener.StartListening("request", true); }