diff --git a/Build/Build.cs b/Build/Build.cs index 5f9bdce8..19c77030 100644 --- a/Build/Build.cs +++ b/Build/Build.cs @@ -130,11 +130,12 @@ protected override void OnBuildInitialized() Target Pack => _ => _ .DependsOn(Compile) + .Requires(() => GitRepository.IsOnMainBranch()) .Executes(() => { Log.Information("Generating NuGet packages for projects in solution"); int commitNum = 0; - string NuGetVersionCustom = "2.0.0.869"; + string NuGetVersionCustom = "2.0.0.870"; //if it's not a tagged release - append the commit number to the package version @@ -157,8 +158,7 @@ protected override void OnBuildInitialized() { Log.Information("Project: {0}", project.Name); } - //Log.Information("Project: {0}", Solution.GetProject("RCommon.Emailing")); - //var path = projects.FirstOrDefault(x => x.Name == "RCommon.Emailing").Path.ToString(); + DotNetTasks .DotNetPack(_ => _ .SetPackageId("RCommon.Emailing") @@ -470,6 +470,7 @@ protected override void OnBuildInitialized() Target Push => _ => _ .DependsOn(Pack) + .Requires(() => GitRepository.IsOnMainBranch()) .Requires(() => NuGetApiUrl) .Requires(() => NUGETAPIKEY) .Requires(() => Configuration.Equals(Configuration.Release)) diff --git a/Src/RCommon.Core/Configuration/RCommonBuilder.cs b/Src/RCommon.Core/Configuration/RCommonBuilder.cs index cd45e536..8be11d5a 100644 --- a/Src/RCommon.Core/Configuration/RCommonBuilder.cs +++ b/Src/RCommon.Core/Configuration/RCommonBuilder.cs @@ -22,7 +22,7 @@ public RCommonBuilder(IServiceCollection services) Services = services; // Event Bus - services.AddScoped(sp => + services.AddSingleton(sp => { return new InMemoryEventBus(sp, services); }); diff --git a/Src/RCommon.Core/EventHandling/InMemoryEventBusBuilder.cs b/Src/RCommon.Core/EventHandling/InMemoryEventBusBuilder.cs index 62e9a315..45dd4846 100644 --- a/Src/RCommon.Core/EventHandling/InMemoryEventBusBuilder.cs +++ b/Src/RCommon.Core/EventHandling/InMemoryEventBusBuilder.cs @@ -14,11 +14,6 @@ public InMemoryEventBusBuilder(IRCommonBuilder builder) { Services = builder.Services; - /*builder.Services.AddSingleton(sp => - { - return new InMemoryEventBus(sp, builder.Services); - });*/ - } public IServiceCollection Services { get; } diff --git a/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs b/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs index e5868e0f..4c340119 100644 --- a/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs +++ b/Src/RCommon.Core/EventHandling/Producers/PublishWithEventBusEventProducer.cs @@ -18,11 +18,14 @@ public PublishWithEventBusEventProducer(IEventBus eventBus, ILogger(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + + // This should already be using a Scoped publish method await _eventBus.PublishAsync(@event); } } diff --git a/Src/RCommon.Core/Extensions/ILoggerExtensions.cs b/Src/RCommon.Core/Extensions/ILoggerExtensions.cs new file mode 100644 index 00000000..519bb0ae --- /dev/null +++ b/Src/RCommon.Core/Extensions/ILoggerExtensions.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace Microsoft.Extensions.Logging +{ + public static class ILoggerExtensions + { + + public static void LogInformation(this ILogger logger, string? message, object[]? @params, bool outputToConsole = false) + { + logger.LogInformation(message, @params); + + if (outputToConsole) + { + OutputToConsole(message, @params); + } + } + + public static void LogInformation(this ILogger logger, EventId eventId, string? message, object[]? @params, bool outputToConsole = false) + { + logger.LogInformation(eventId, message, @params); + + if (outputToConsole) + { + OutputToConsole(message, @params); + } + } + + public static void LogInformation(this ILogger logger, Exception exception, string? message, object[]? @params, bool outputToConsole = false) + { + logger.LogInformation(exception, message, @params); + + if (outputToConsole) + { + OutputToConsole(message, @params); + } + } + + public static void LogInformation(this ILogger logger, EventId eventId, Exception exception, string? message, object[]? @params, bool outputToConsole = false) + { + logger.LogInformation(eventId, exception, message, @params); + + if (outputToConsole) + { + OutputToConsole(message, @params); + } + } + + private static void OutputToConsole(string? message, object[]? @params) + { + System.Console.WriteLine(message, @params); + } + } +} diff --git a/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs b/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs index 6f4542df..be3728e8 100644 --- a/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs +++ b/Src/RCommon.MassTransit/MassTransitEventHandlingBuilderExtensions.cs @@ -74,8 +74,8 @@ public static IRCommonBuilder WithEventHandling(this IRCommonBuilder builder, { // MassTransit Event Bus - builder.Services.AddTransient(typeof(IMassTransitEventHandler<>), typeof(MassTransitEventHandler<>)); - builder.Services.AddTransient(typeof(MassTransitEventHandler<>)); + builder.Services.AddScoped(typeof(IMassTransitEventHandler<>), typeof(MassTransitEventHandler<>)); + builder.Services.AddScoped(typeof(MassTransitEventHandler<>)); builder.AddMassTransit(actions); return builder; diff --git a/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs b/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs index 9b1de1cb..d75e4fd1 100644 --- a/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs +++ b/Src/RCommon.MassTransit/Producers/PublishWithMassTransitEventProducer.cs @@ -1,4 +1,5 @@ using MassTransit; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; @@ -14,18 +15,24 @@ public class PublishWithMassTransitEventProducer : IEventProducer { private readonly IBus _bus; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public PublishWithMassTransitEventProducer(IBus bus, ILogger logger) + public PublishWithMassTransitEventProducer(IBus bus, ILogger logger, IServiceProvider serviceProvider) { _bus = bus ?? throw new ArgumentNullException(nameof(bus)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _bus.Publish(@event, cancellationToken); + + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); + await _bus.Publish(@event, cancellationToken); + } } } } diff --git a/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs b/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs index 087d725c..d234129b 100644 --- a/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs +++ b/Src/RCommon.MassTransit/Producers/SendWithMassTransitEventProducer.cs @@ -1,4 +1,5 @@ using MassTransit; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; @@ -15,18 +16,23 @@ public class SendWithMassTransitEventProducer : IEventProducer private readonly IBus _bus; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public SendWithMassTransitEventProducer(IBus bus, ILogger logger) + public SendWithMassTransitEventProducer(IBus bus, ILogger logger, IServiceProvider serviceProvider) { _bus = bus ?? throw new ArgumentNullException(nameof(bus)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _bus.Send(@event, cancellationToken); + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); + await _bus.Send(@event, cancellationToken); + } } } } diff --git a/Src/RCommon.MassTransit/Subscribers/MassTransitEventHandler.cs b/Src/RCommon.MassTransit/Subscribers/MassTransitEventHandler.cs index d627deef..8d85d882 100644 --- a/Src/RCommon.MassTransit/Subscribers/MassTransitEventHandler.cs +++ b/Src/RCommon.MassTransit/Subscribers/MassTransitEventHandler.cs @@ -1,4 +1,5 @@ using MassTransit; +using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Subscribers; using System; @@ -12,16 +13,18 @@ namespace RCommon.MassTransit.Subscribers public class MassTransitEventHandler : IMassTransitEventHandler, IConsumer where TEvent : class, ISerializableEvent { + private readonly ILogger> _logger; private readonly ISubscriber _subscriber; - public MassTransitEventHandler(ISubscriber subscriber) + public MassTransitEventHandler(ILogger> logger, ISubscriber subscriber) { - _subscriber = subscriber; + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber)); } public async Task Consume(ConsumeContext context) { - Console.WriteLine("{0} handling event {1} from MassTransit", new object[] { this.GetGenericTypeName(), context.Message }); + _logger.LogDebug("{0} handling event {1}", new object[] { this.GetGenericTypeName(), context.Message }); await _subscriber.HandleAsync(context.Message); } } diff --git a/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs b/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs index 83263234..4caa9b68 100644 --- a/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs +++ b/Src/RCommon.Mediatr/Producers/PublishWithMediatREventProducer.cs @@ -1,4 +1,5 @@ using MediatR; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; @@ -16,19 +17,24 @@ public class PublishWithMediatREventProducer : IEventProducer { private readonly IMediatorService _mediatorService; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public PublishWithMediatREventProducer(IMediatorService mediatorService, ILogger logger) + public PublishWithMediatREventProducer(IMediatorService mediatorService, ILogger logger, IServiceProvider serviceProvider) { _mediatorService = mediatorService ?? throw new ArgumentNullException(nameof(mediatorService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(TEvent @event, CancellationToken cancellationToken = default) where TEvent : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _mediatorService.Publish(@event, cancellationToken); + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + await _mediatorService.Publish(@event, cancellationToken); + } } } } diff --git a/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs b/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs index 0b3a845c..40725bd1 100644 --- a/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs +++ b/Src/RCommon.Mediatr/Producers/SendWithMediatREventProducer.cs @@ -9,6 +9,7 @@ using RCommon.MediatR.Subscribers; using RCommon.Mediator; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; namespace RCommon.MediatR.Producers { @@ -16,19 +17,24 @@ public class SendWithMediatREventProducer : IEventProducer { private readonly IMediatorService _mediatorService; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public SendWithMediatREventProducer(IMediatorService mediatorService, ILogger logger) + public SendWithMediatREventProducer(IMediatorService mediatorService, ILogger logger, IServiceProvider serviceProvider) { _mediatorService = mediatorService ?? throw new ArgumentNullException(nameof(mediatorService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(TEvent @event, CancellationToken cancellationToken = default) where TEvent : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _mediatorService.Send(@event, cancellationToken); + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + await _mediatorService.Send(@event, cancellationToken); + } } } } diff --git a/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs b/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs index 94b9d560..c81c0ff2 100644 --- a/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs +++ b/Src/RCommon.Wolverine/Producers/PublishWithWolverineEventProducer.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Configuration; using RCommon.EventHandling; using RCommon.EventHandling.Producers; @@ -15,18 +16,23 @@ public class PublishWithWolverineEventProducer : IEventProducer { private readonly IMessageBus _messageBus; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public PublishWithWolverineEventProducer(IMessageBus messageBus, ILogger logger) + public PublishWithWolverineEventProducer(IMessageBus messageBus, ILogger logger, IServiceProvider serviceProvider) { _messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _messageBus.PublishAsync(@event); + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} publishing event: {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + await _messageBus.PublishAsync(@event); + } } } } diff --git a/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs b/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs index d0fa3b73..cbfa0166 100644 --- a/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs +++ b/Src/RCommon.Wolverine/Producers/SendWithWolverineEventProducer.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using RCommon.EventHandling; using RCommon.EventHandling.Producers; using System; @@ -14,19 +15,24 @@ public class SendWithWolverineEventProducer : IEventProducer { private readonly IMessageBus _messageBus; private readonly ILogger _logger; + private readonly IServiceProvider _serviceProvider; - public SendWithWolverineEventProducer(IMessageBus messageBus, ILogger logger) + public SendWithWolverineEventProducer(IMessageBus messageBus, ILogger logger, IServiceProvider serviceProvider) { _messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } public async Task ProduceEventAsync(T @event, CancellationToken cancellationToken = default) where T : ISerializableEvent { Guard.IsNotNull(@event, nameof(@event)); - _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event }); - await _messageBus.SendAsync(@event); + using (IServiceScope scope = _serviceProvider.CreateScope()) + { + _logger.LogInformation("{0} sending event: {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + await _messageBus.SendAsync(@event); + } } } } diff --git a/Src/RCommon.Wolverine/Subscribers/WolverineEventHandler.cs b/Src/RCommon.Wolverine/Subscribers/WolverineEventHandler.cs index a1181175..52d7da64 100644 --- a/Src/RCommon.Wolverine/Subscribers/WolverineEventHandler.cs +++ b/Src/RCommon.Wolverine/Subscribers/WolverineEventHandler.cs @@ -1,4 +1,5 @@ -using RCommon.EventHandling; +using Microsoft.Extensions.Logging; +using RCommon.EventHandling; using RCommon.EventHandling.Subscribers; using System; using System.Collections.Generic; @@ -13,16 +14,18 @@ public class WolverineEventHandler : IWolverineEventHandler, IWo where TEvent : class, ISerializableEvent { private readonly ISubscriber _subscriber; + private readonly ILogger> _logger; - public WolverineEventHandler(ISubscriber subscriber) + public WolverineEventHandler(ISubscriber subscriber, ILogger> logger) { - _subscriber = subscriber; + _subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public async Task HandleAsync(TEvent distributedEvent, CancellationToken cancellationToken = default) + public async Task HandleAsync(TEvent @event, CancellationToken cancellationToken = default) { - Console.WriteLine("{0} handling event {1} from MassTransit", new object[] { this.GetGenericTypeName(), distributedEvent }); - await _subscriber.HandleAsync(distributedEvent); + _logger.LogDebug("{0} handling event {1}", new object[] { this.GetGenericTypeName(), @event.GetGenericTypeName() }); + await _subscriber.HandleAsync(@event); } } } diff --git a/Src/RCommon.sln b/Src/RCommon.sln index aca6eeff..429a4ff4 100644 --- a/Src/RCommon.sln +++ b/Src/RCommon.sln @@ -194,7 +194,7 @@ Global {3A6EE1AD-5E24-4CA4-B64D-E1517EB2DF63} = {F1158595-0560-42FE-BB98-464BAD3B8C1D} {4936D8C3-7C82-4014-97AD-1FD3B1BAAE70} = {0A72EADE-8CF6-49A8-A594-55F54BBF61C6} {AB009DA1-E781-4EBF-B1A5-EA31A82F004E} = {F8D8C009-564C-4F33-A49B-CE7830575A09} - {F8D8C009-564C-4F33-A49B-CE7830575A09} = {0AF37317-F10E-47E8-A4C8-9EA886E00E40} + {F8D8C009-564C-4F33-A49B-CE7830575A09} = {F1158595-0560-42FE-BB98-464BAD3B8C1D} {C5AAB002-5E01-4E0A-8259-A29A8FAEA581} = {0A72EADE-8CF6-49A8-A594-55F54BBF61C6} {F9A029E9-E0A2-4439-9F26-9DC4D9D770A8} = {0AF37317-F10E-47E8-A4C8-9EA886E00E40} {63CC3BD7-CCD0-4703-92D9-EAEC1EA8AA18} = {F9A029E9-E0A2-4439-9F26-9DC4D9D770A8}