diff --git a/src/Samples/PingPong/Pinger/Worker.cs b/src/Samples/PingPong/Pinger/Worker.cs index f351d8113..b39c53d8a 100644 --- a/src/Samples/PingPong/Pinger/Worker.cs +++ b/src/Samples/PingPong/Pinger/Worker.cs @@ -8,23 +8,25 @@ namespace Pinger; public class Worker : BackgroundService { private readonly ILogger _logger; - private readonly IMessageBus _bus; + private readonly IServiceProvider _serviceProvider; - public Worker(ILogger logger, IMessageBus bus) + public Worker(ILogger logger, IServiceProvider serviceProvider) { _logger = logger; - _bus = bus; + _serviceProvider = serviceProvider; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var pingNumber = 1; + await using var scope= _serviceProvider.CreateAsyncScope(); + var bus = scope.ServiceProvider.GetRequiredService(); while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); _logger.LogInformation("Sending Ping #{Number}", pingNumber); - await _bus.PublishAsync(new Ping { Number = pingNumber }); + await bus.PublishAsync(new Ping { Number = pingNumber }); pingNumber++; } } diff --git a/src/Samples/PingPong/Ponger/Program.cs b/src/Samples/PingPong/Ponger/Program.cs index b814ee619..b7627b183 100644 --- a/src/Samples/PingPong/Ponger/Program.cs +++ b/src/Samples/PingPong/Ponger/Program.cs @@ -8,6 +8,8 @@ return await Host.CreateDefaultBuilder(args) .UseWolverine(opts => { + opts.ApplicationAssembly = typeof(Program).Assembly; + // Using Wolverine's built in TCP transport opts.ListenAtPort(5581); }) diff --git a/src/Samples/PingPongWithRabbitMq/Pinger/PingerService.cs b/src/Samples/PingPongWithRabbitMq/Pinger/PingerService.cs index 63c262277..639ca13df 100644 --- a/src/Samples/PingPongWithRabbitMq/Pinger/PingerService.cs +++ b/src/Samples/PingPongWithRabbitMq/Pinger/PingerService.cs @@ -9,38 +9,37 @@ public class PingerService : IHostedService { // IMessagePublisher is an interface you can use // strictly to publish messages through Wolverine - private readonly IMessageBus _bus; + private readonly IServiceProvider _serviceProvider; - public PingerService(IMessageBus bus) + public PingerService(IServiceProvider serviceProvider) { - _bus = bus; + _serviceProvider = serviceProvider; } - public Task StartAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { - return Task.Run(async () => - { - var count = 0; + var count = 0; - while (!cancellationToken.IsCancellationRequested) + await using var scope= _serviceProvider.CreateAsyncScope(); + var bus = scope.ServiceProvider.GetRequiredService(); + while (!cancellationToken.IsCancellationRequested) + { + try { - try + var message = new PingMessage { - var message = new PingMessage - { - Number = ++count - }; + Number = ++count + }; - await _bus.SendAsync(message); + await bus.SendAsync(message); - await Task.Delay(1.Seconds(), cancellationToken); - } - catch (TaskCanceledException) - { - return; - } + await Task.Delay(1.Seconds(), cancellationToken); + } + catch (TaskCanceledException) + { + return; } - }, cancellationToken); + } } public Task StopAsync(CancellationToken cancellationToken) diff --git a/src/Samples/PingPongWithRabbitMq/Pinger/Program.cs b/src/Samples/PingPongWithRabbitMq/Pinger/Program.cs index efff2fe4d..8fbf0217f 100644 --- a/src/Samples/PingPongWithRabbitMq/Pinger/Program.cs +++ b/src/Samples/PingPongWithRabbitMq/Pinger/Program.cs @@ -9,9 +9,10 @@ return await Host.CreateDefaultBuilder(args) .UseWolverine(opts => { + opts.ApplicationAssembly = typeof(Program).Assembly; + // Listen for messages coming into the pongs queue - opts - .ListenToRabbitQueue("pongs"); + opts.ListenToRabbitQueue("pongs"); // Publish messages to the pings queue opts.PublishMessage().ToRabbitExchange("pings"); diff --git a/src/Samples/PingPongWithRabbitMq/Pinger/Properties/launchSettings.json b/src/Samples/PingPongWithRabbitMq/Pinger/Properties/launchSettings.json index 511465c1d..b1ce65f8d 100644 --- a/src/Samples/PingPongWithRabbitMq/Pinger/Properties/launchSettings.json +++ b/src/Samples/PingPongWithRabbitMq/Pinger/Properties/launchSettings.json @@ -1,30 +1,10 @@ { - "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:64643", - "sslPort": 44397 - } - }, "profiles": { - "Pinger": { + "PingerWithRabbitMQ": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "https://localhost:7295;http://localhost:5295", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "DOTNET_ENVIRONMENT": "Development" } } } diff --git a/src/Samples/PingPongWithRabbitMq/Ponger/Properties/launchSettings.json b/src/Samples/PingPongWithRabbitMq/Ponger/Properties/launchSettings.json index dbd357718..abd9e01fe 100644 --- a/src/Samples/PingPongWithRabbitMq/Ponger/Properties/launchSettings.json +++ b/src/Samples/PingPongWithRabbitMq/Ponger/Properties/launchSettings.json @@ -1,6 +1,6 @@ { "profiles": { - "Ponger": { + "PongerWithRabbitMQ": { "commandName": "Project", "dotnetRunMessages": true, "environmentVariables": {