private async Task StartPublisher(IRabbitMqPublisherBus bus, CancellationToken cancellationToken) { var counter = 0; await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken); while (!cancellationToken.IsCancellationRequested) { counter += 1; bus.Publish(new PingMessage { Timestamp = DateTime.UtcNow, Message = counter.ToString() }); bus.Publish(new PingNotification { Timestamp = DateTime.UtcNow, Message = counter.ToString() }); bus.Publish(new PingRequest { Timestamp = DateTime.UtcNow, Message = counter.ToString() }); bus.Publish(new PingPongRequest { Timestamp = DateTime.UtcNow, Message = counter.ToString() }); /* * if ((counter-1) % 10 == 0) * { * try * { * bus.Publish(new RaiseRequest()); * } * catch (Exception e) * { * Console.WriteLine($"Unable to publish RaiseRequest: {e.Message}"); * } * } */ await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IRabbitMqPublisherBus bus, IHostApplicationLifetime lifetime, IServiceProvider services) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseHttpMetrics(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapMetrics(); }); Task.Factory.StartNew(() => StartPublisher(bus, lifetime.ApplicationStopping), TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach); /* * Task.Factory.StartNew(() => publisher.PublishAsync( * new PingMessage { Timestamp = DateTime.UtcNow }, * new PublishOptions { PublisherConfirmsEnabled = true }) * ); */ }