Skip to content

Commit

Permalink
Dev (#113)
Browse files Browse the repository at this point in the history
* EventRouter Updates

* Updated dependencies to latest and fixed MassTransit example.

* Updated MassTransit test projects.

* Promoted EventHandling assembly to RCommon.Core as it is becoming more essential to have loosely coupled events locally in RCommon as well as in dependencies!

* Removed deprecated project.

---------

Co-authored-by: Jason Webb <46631010+JasonMWebb@users.noreply.github.com>
  • Loading branch information
jasonmwebb-lv and JasonMWebb authored Mar 25, 2024
1 parent 2d515a7 commit e608eee
Show file tree
Hide file tree
Showing 72 changed files with 161 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;</TargetFrameworks>
Expand All @@ -9,21 +9,20 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Bogus" Version="35.4.1" />
<PackageReference Include="Bogus" Version="35.5.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using HR.LeaveManagement.Application.Models;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using HR.LeaveManagement.Application.Constants;
using RCommon.Persistence;
using RCommon.Security.Users;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.ApplicationServices\RCommon.ApplicationServices.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

<ItemGroup>
<PackageReference Include="LocalStorage" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.3" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.4.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.JsonWebTokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
Expand Down Expand Up @@ -39,8 +39,8 @@ public async Task InvokeAsync(HttpContext httpContext)
if (tokenExists)
{
var token = _localStorageService.GetStorageValue<string>("token");
JwtSecurityTokenHandler tokenHandler = new();
var tokenContent = tokenHandler.ReadJwtToken(token);
JsonWebTokenHandler tokenHandler = new();
var tokenContent = tokenHandler.ReadJsonWebToken(token);
var expiry = tokenContent.ValidTo;
if (expiry < DateTime.UtcNow)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.JsonWebTokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Claims;
Expand All @@ -19,15 +19,15 @@ public class AuthenticationService : BaseHttpService, IAuthenticationService
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IMapper _mapper;
private JwtSecurityTokenHandler _tokenHandler;
private JsonWebTokenHandler _tokenHandler;

public AuthenticationService(IClient client, ILocalStorageService localStorage, IHttpContextAccessor httpContextAccessor,
IMapper mapper)
: base(client, localStorage)
{
this._httpContextAccessor = httpContextAccessor;
this._mapper = mapper;
this._tokenHandler = new JwtSecurityTokenHandler();
this._tokenHandler = new JsonWebTokenHandler();
}

public async Task<bool> Authenticate(string email, string password)
Expand All @@ -40,7 +40,7 @@ public async Task<bool> Authenticate(string email, string password)
if (authenticationResponse.Token != string.Empty)
{
//Get Claims from token and Build auth user object
var tokenContent = _tokenHandler.ReadJwtToken(authenticationResponse.Token);
var tokenContent = _tokenHandler.ReadJsonWebToken(authenticationResponse.Token);
var claims = ParseClaims(tokenContent);
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
var login = _httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);
Expand Down Expand Up @@ -76,7 +76,7 @@ public async Task Logout()
await _httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}

private IList<Claim> ParseClaims(JwtSecurityToken tokenContent)
private IList<Claim> ParseClaims(JsonWebToken tokenContent)
{
var claims = tokenContent.Claims.ToList();
claims.Add(new Claim(ClaimTypes.Name, tokenContent.Subject));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.EntityFrameworkCore;
using RCommon;
using RCommon.Entities;
using RCommon.Mediator;
using RCommon.Persistence.EFCore;
using RCommon.Security.Users;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.EntityFrameworkCore;
using RCommon;
using RCommon.Entities;
using RCommon.Mediator;
using RCommon.Security.Users;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.EventHandling\RCommon.EventHandling.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.Core\RCommon.Core.csproj" />
</ItemGroup>

</Project>
6 changes: 0 additions & 6 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.Messaging.MassTran
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.Messaging.Wolverine", "Messaging\Samples.Messaging.WolverineRabbitMq\Examples.Messaging.Wolverine.csproj", "{B792333E-245B-48F0-A88E-F36EEF1F7C11}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RCommon.EventHandling", "..\Src\RCommon.EventHandling\RCommon.EventHandling.csproj", "{8D143FCC-AD56-4CE7-9AFA-24144BFF1C56}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Providers", "Providers", "{3199F749-0082-41D0-91D3-ECED117F8B08}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RCommon.DotNet6.Tests", "..\Tests\RCommon.DotNet6.Tests\RCommon.DotNet6.Tests.csproj", "{B93C2373-7ADD-4D1F-B8CB-8FF32EAF364E}"
Expand Down Expand Up @@ -271,10 +269,6 @@ Global
{B792333E-245B-48F0-A88E-F36EEF1F7C11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B792333E-245B-48F0-A88E-F36EEF1F7C11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B792333E-245B-48F0-A88E-F36EEF1F7C11}.Release|Any CPU.Build.0 = Release|Any CPU
{8D143FCC-AD56-4CE7-9AFA-24144BFF1C56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D143FCC-AD56-4CE7-9AFA-24144BFF1C56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D143FCC-AD56-4CE7-9AFA-24144BFF1C56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D143FCC-AD56-4CE7-9AFA-24144BFF1C56}.Release|Any CPU.Build.0 = Release|Any CPU
{B93C2373-7ADD-4D1F-B8CB-8FF32EAF364E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B93C2373-7ADD-4D1F-B8CB-8FF32EAF364E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B93C2373-7ADD-4D1F-B8CB-8FF32EAF364E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit.RabbitMQ" Version="8.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,13 @@
eventHandling.AddProducer<PublishWithMassTransitEventProducer>();
eventHandling.AddSubscriber<TestEvent, TestEventHandler>();
});

services.AddHostedService<Worker>();
}).Build();

Console.WriteLine("Example Starting");
var eventProducers = host.Services.GetServices<IEventProducer>();
var testEvent = new TestEvent(DateTime.Now, Guid.NewGuid());

foreach (var producer in eventProducers)
{
Console.WriteLine($"Producer: {producer}");
await producer.ProduceEventAsync(testEvent);
}
Console.WriteLine("Example Complete");
Console.ReadLine();



await host.RunAsync();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public TestEventHandler()
public async Task HandleAsync(TestEvent notification, CancellationToken cancellationToken = default)
{
Console.WriteLine("I just handled this event {0}", new object[] { notification.ToString() });
Console.WriteLine("Example Complete");
await Task.CompletedTask;
}
}
Expand Down
43 changes: 43 additions & 0 deletions Examples/Messaging/Samples.Messaging.MassTransitRabbitMq/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using MassTransit;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RCommon.EventHandling.Producers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace Examples.Messaging.MassTransit
{
public class Worker : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine("Example Starting");
var eventProducers = _serviceProvider.GetServices<IEventProducer>();
var testEvent = new TestEvent(DateTime.Now, Guid.NewGuid());

foreach (var producer in eventProducers)
{
Console.WriteLine($"Producer: {producer}");
await producer.ProduceEventAsync(testEvent);
}
await Task.Delay(5000, stoppingToken);

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Description>A cohesive set of infrastructure libraries for .NET 6, .NET 7, and .NET 8 that utilizes abstractions for persistence, unit of work/transactions, distributed events, distributed transactions, and more.</Description>
<Authors>Jason Webb</Authors>
<Company>RCommon</Company>
<Version>2.0.0.0</Version>
<Version>2.0.0.1</Version>
<RepositoryType>GitHub</RepositoryType>
<PackageTags>RCommon, application services, CQRS, auto web api, commands, command handlers, queries, query handlers, command bus, query bus, c#, .NET</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0;</TargetFrameworks>
<Version>2.0.0.0</Version>
<Version>2.0.0.1</Version>
<Authors>Jason Webb</Authors>
<Company>RCommon</Company>
<Description>A cohesive set of infrastructure libraries for .NET 6, .NET 7, and .NET 8 that utilizes abstractions for persistence, unit of work/transactions, distributed events, distributed transactions, and more.</Description>
Expand Down
6 changes: 6 additions & 0 deletions Src/RCommon.Core/Configuration/RCommonBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using RCommon.EventHandling;
using RCommon.EventHandling.Producers;

namespace RCommon
{
Expand All @@ -18,6 +20,10 @@ public RCommonBuilder(IServiceCollection services)
{
Guard.Against<NullReferenceException>(services == null, "IServiceCollection cannot be null");
Services = services;

// Event Bus
Services.AddSingleton<IEventBus, InMemoryEventBus>(); // Set up default event bus
Services.AddSingleton<IEventRouter, EventRouter>();
}

public IRCommonBuilder WithSequentialGuidGenerator(Action<SequentialGuidGeneratorOptions> actions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using RCommon.EventHandling;
using RCommon.EventHandling.Producers;
using RCommon.EventHandling.Subscribers;
using System;
Expand All @@ -10,7 +11,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace RCommon.EventHandling
namespace RCommon
{
public static class EventHandlingBuilderExtensions
{
Expand All @@ -23,13 +24,6 @@ public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder)
public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder, Action<T> actions)
where T : IEventHandlingBuilder
{

// Event Bus
builder.Services.AddSingleton<IEventBus, InMemoryEventBus>();

// Event Routing
builder.Services.AddTransient<IEventRouter, EventRouter>();

// Event Handling Configurations
var eventHandlingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder });
actions(eventHandlingConfig);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ public class EventRouter : IEventRouter
{
private readonly IServiceProvider _serviceProvider;

//private readonly IEventProducerRegistry _eventProducerRegistry;

public EventRouter(IServiceProvider serviceProvider)
{
this._serviceProvider = serviceProvider;
//_eventProducerRegistry = eventProducerRegistry ?? throw new ArgumentNullException(nameof(eventProducerRegistry));
}

public async Task RouteEvents(IEnumerable<ISerializableEvent> localEvents)
Expand Down
8 changes: 4 additions & 4 deletions Src/RCommon.Core/RCommon.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0;</TargetFrameworks>
<Version>2.0.0.0</Version>
<Version>2.0.0.1</Version>
<Authors>Jason Webb</Authors>
<Company>RCommon</Company>
<Description>A cohesive set of infrastructure libraries for .NET 6, .NET 7, and .NET 8 that utilizes abstractions for persistence, unit of work/transactions, distributed events, distributed transactions, and more.</Description>
Expand All @@ -26,23 +26,23 @@
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.1.2" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.1.2" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.1.2" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit e608eee

Please sign in to comment.