From 07413404c7699f2295886bfba062a5d0816e22e3 Mon Sep 17 00:00:00 2001 From: DavidEggenberger Date: Fri, 26 Apr 2024 21:55:13 +0200 Subject: [PATCH] registering configuration --- .../Web/Server/SubscriptionsModuleStartup.cs | 10 ++-- .../ExternalLoginCallbackController.cs | 2 +- .../Web/Server/SignalR/NotificationHub.cs | 53 ------------------- .../Server/SignalR/SignalRDIRegistrator.cs | 17 ------ .../SignalR/SignalRRoutingRegistrator.cs | 14 ----- Source/Shared/Features/Registrator.cs | 35 ++++++++++++ .../Features/SignalR/NotificationHub.cs | 8 +++ .../SignalR/NotificationHubService.cs | 26 +++++++++ Source/Shared/Features/SignalR/Registrator.cs | 27 ++++++++++ .../Shared/Features/SignalR/UserIdProvider.cs | 15 ++++++ .../Constants/NotificationHubConstants.cs | 7 +++ .../Web/Server/Properties/launchSettings.json | 2 +- Source/Web/Server/Startup.cs | 8 +++ Source/Web/Server/appsettings.Docker.json | 18 ------- 14 files changed, 134 insertions(+), 108 deletions(-) delete mode 100644 Source/Modules/TenantIdentity/Web/Server/SignalR/NotificationHub.cs delete mode 100644 Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRDIRegistrator.cs delete mode 100644 Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRRoutingRegistrator.cs create mode 100644 Source/Shared/Features/Registrator.cs create mode 100644 Source/Shared/Features/SignalR/NotificationHub.cs create mode 100644 Source/Shared/Features/SignalR/NotificationHubService.cs create mode 100644 Source/Shared/Features/SignalR/Registrator.cs create mode 100644 Source/Shared/Features/SignalR/UserIdProvider.cs create mode 100644 Source/Shared/Kernel/Constants/NotificationHubConstants.cs delete mode 100644 Source/Web/Server/appsettings.Docker.json diff --git a/Source/Modules/Subscription/Web/Server/SubscriptionsModuleStartup.cs b/Source/Modules/Subscription/Web/Server/SubscriptionsModuleStartup.cs index aec081bb..d8f53ab9 100644 --- a/Source/Modules/Subscription/Web/Server/SubscriptionsModuleStartup.cs +++ b/Source/Modules/Subscription/Web/Server/SubscriptionsModuleStartup.cs @@ -2,7 +2,11 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Modules.Subscriptions.Features.Infrastructure.Configuration; +using Modules.Subscriptions.Features.Infrastructure.EFCore; +using Shared.Features.EFCore; using Shared.Features.Modules; +using Shared.Features.Modules.Configuration; using Stripe; using System.Reflection; @@ -14,11 +18,9 @@ public class SubscriptionsModuleStartup : IModuleStartup public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { StripeConfiguration.ApiKey = configuration["Stripe:StripeKey"]; - //services.Configure(configuration); - //services.AddScoped(); - services.AddControllers() - .AddApplicationPart(typeof(SubscriptionsModuleStartup).Assembly); + services.RegisterDbContext("subscriptions"); + services.RegisterModuleConfiguration(configuration); } public void Configure(IApplicationBuilder app, IHostEnvironment env) diff --git a/Source/Modules/TenantIdentity/Web/Server/Controllers/ExternalLoginCallbackController.cs b/Source/Modules/TenantIdentity/Web/Server/Controllers/ExternalLoginCallbackController.cs index 31c1f25b..23e99fc3 100644 --- a/Source/Modules/TenantIdentity/Web/Server/Controllers/ExternalLoginCallbackController.cs +++ b/Source/Modules/TenantIdentity/Web/Server/Controllers/ExternalLoginCallbackController.cs @@ -8,7 +8,7 @@ using Modules.TenantIdentity.Features.DomainFeatures.UserAggregate.Application.Commands; using Shared.Features.Server; -namespace Web.Server.Controllers.Identity +namespace Modules.TenantIdentity.Web.Server { [Route("api/[controller]")] [AllowAnonymous] diff --git a/Source/Modules/TenantIdentity/Web/Server/SignalR/NotificationHub.cs b/Source/Modules/TenantIdentity/Web/Server/SignalR/NotificationHub.cs deleted file mode 100644 index 9dad5e23..00000000 --- a/Source/Modules/TenantIdentity/Web/Server/SignalR/NotificationHub.cs +++ /dev/null @@ -1,53 +0,0 @@ -namespace Modules.TenantIdentity.Web.Server.SignalR -{ - //[Authorize] - //public class NotificationHub : Hub, ISignalRHub - //{ - // private ApplicationUserManager applicationUserManager; - // private IdentityDbContext identificationDbContext; - // public NotificationHub(ApplicationUserManager applicationUserManager, IdentityDbContext identificationDbContext) - // { - // this.applicationUserManager = applicationUserManager; - // this.identificationDbContext = identificationDbContext; - // } - // public override async Task OnConnectedAsync() - // { - // ApplicationUser applicationUser = await applicationUserManager.FindByClaimsPrincipalAsync(Context.User); - // //ApplicationUserTeam applicationUserTeam = await applicationUserManager.GetCurrentTeamMembership(applicationUser); - // //await Groups.AddToGroupAsync(Context.ConnectionId, $"{applicationUserTeam.TeamId}{applicationUserTeam.Role}"); - // //await Groups.AddToGroupAsync(Context.ConnectionId, $"{applicationUserTeam.TeamId}"); - // if (applicationUser.IsOnline is false) - // { - // applicationUser.IsOnline = true; - // applicationUser.TabsOpen = 1; - // await applicationUserManager.UpdateAsync(applicationUser); - // await Web.Clients.All.SendAsync("UpdateOnlineUsers"); - // return; - // } - // if (applicationUser.IsOnline) - // { - // applicationUser.TabsOpen++; - // await applicationUserManager.UpdateAsync(applicationUser); - // } - // } - // public override async Task OnDisconnectedAsync(Exception ex) - // { - // ApplicationUser appUser = await applicationUserManager.FindByClaimsPrincipalAsync(Context.User); - // if (appUser.TabsOpen > 0) - // { - // appUser.TabsOpen--; - // await applicationUserManager.UpdateAsync(appUser); - // } - // if (appUser.TabsOpen == 0) - // { - // appUser.IsOnline = false; - // await applicationUserManager.UpdateAsync(appUser); - // ApplicationUser applicationUser = await applicationUserManager.FindByClaimsPrincipalAsync(Context.User); - // //ApplicationUserTeam applicationUserTeam = await applicationUserManager.GetCurrentTeamMembership(applicationUser); - // //await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"{applicationUserTeam.TeamId}{applicationUserTeam.Role}"); - // //await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"{applicationUserTeam.TeamId}"); - // await Web.Clients.AllExcept(appUser.Id.ToString()).SendAsync("UpdateOnlineUsers"); - // } - // } - //} -} diff --git a/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRDIRegistrator.cs b/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRDIRegistrator.cs deleted file mode 100644 index bf68b973..00000000 --- a/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRDIRegistrator.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; - -namespace Modules.TenantIdentity.Web.Server.SignalR -{ - public static class SignalRDIRegistrator - { - public static IServiceCollection RegisterSignalR(this IServiceCollection services) - { - services.AddSignalR(); - - //services.AddSingleton(); - //services.AddScoped(); - - return services; - } - } -} diff --git a/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRRoutingRegistrator.cs b/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRRoutingRegistrator.cs deleted file mode 100644 index 1393dda4..00000000 --- a/Source/Modules/TenantIdentity/Web/Server/SignalR/SignalRRoutingRegistrator.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Routing; - -namespace Modules.TenantIdentity.Web.Server.SignalR -{ - public static class SignalRRoutingRegistrator - { - public static HubEndpointConventionBuilder MapSignalR(this IEndpointRouteBuilder endpointRouteBuilder) - { - return null; - //return endpointRouteBuilder.MapHub(SignalRConstants.NotificationEndpoint); - } - } -} diff --git a/Source/Shared/Features/Registrator.cs b/Source/Shared/Features/Registrator.cs new file mode 100644 index 00000000..d7e45d13 --- /dev/null +++ b/Source/Shared/Features/Registrator.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Shared.Features.Messaging; +using Shared.Features.EFCore; +using Shared.Features.Modules; +using Shared.Features.Server.ExecutionContext; + +namespace Shared.Features +{ + public static class Registrator + { + public static IServiceCollection AddSharedFeatures(this IServiceCollection services) + { + var serviceProvider = services.BuildServiceProvider(); + var configuration = serviceProvider.GetRequiredService(); + + services.AddMessaging(); + services.AddEFCore(configuration); + services.AddServerExecutionContext(); + + return services; + } + + public static IApplicationBuilder UseSharedFeaturesMiddleware(this IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseEFCoreMiddleware(); + app.UseServerExecutionContextMiddleware(); + app.UseModulesMiddleware(env); + + return app; + } + } +} diff --git a/Source/Shared/Features/SignalR/NotificationHub.cs b/Source/Shared/Features/SignalR/NotificationHub.cs new file mode 100644 index 00000000..eea5249a --- /dev/null +++ b/Source/Shared/Features/SignalR/NotificationHub.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.SignalR; + +namespace Shared.Features.SignalR +{ + public class NotificationHub : Hub + { + } +} diff --git a/Source/Shared/Features/SignalR/NotificationHubService.cs b/Source/Shared/Features/SignalR/NotificationHubService.cs new file mode 100644 index 00000000..d5f3fbac --- /dev/null +++ b/Source/Shared/Features/SignalR/NotificationHubService.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.SignalR; +using Shared.Kernel.Constants; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Shared.Features.SignalR +{ + public class NotificationHubService + { + private readonly IHubContext notificationHubContext; + + public NotificationHubService(IHubContext notificationHubContext) + { + this.notificationHubContext = notificationHubContext; + } + + public async Task SendNotificationAsync(Guid userId, string triggeredMethodName) + { + await notificationHubContext.Clients.User(userId.ToString()).SendAsync(triggeredMethodName); + } + } +} diff --git a/Source/Shared/Features/SignalR/Registrator.cs b/Source/Shared/Features/SignalR/Registrator.cs new file mode 100644 index 00000000..607b40bb --- /dev/null +++ b/Source/Shared/Features/SignalR/Registrator.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; +using Shared.Kernel.Constants; + +namespace Shared.Features.SignalR +{ + public static class Registrator + { + public static IServiceCollection Add_SignalR(this IServiceCollection services) + { + services.AddSingleton(); + services.AddScoped(); + return services; + } + + public static IApplicationBuilder UseSignalRMiddleware(this IApplicationBuilder app) + { + app.UseEndpoints(endpoints => + { + endpoints.MapHub(NotificationHubConstants.Hub); + }); + + return app; + } + } +} diff --git a/Source/Shared/Features/SignalR/UserIdProvider.cs b/Source/Shared/Features/SignalR/UserIdProvider.cs new file mode 100644 index 00000000..069ae576 --- /dev/null +++ b/Source/Shared/Features/SignalR/UserIdProvider.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.SignalR; +using Shared.Kernel.Extensions.ClaimsPrincipal; + +namespace Shared.Features.SignalR +{ + public class UserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) + { + return connection.User.Identity.IsAuthenticated + ? connection.User?.GetUserId() + : string.Empty; + } + } +} diff --git a/Source/Shared/Kernel/Constants/NotificationHubConstants.cs b/Source/Shared/Kernel/Constants/NotificationHubConstants.cs new file mode 100644 index 00000000..0ea3affd --- /dev/null +++ b/Source/Shared/Kernel/Constants/NotificationHubConstants.cs @@ -0,0 +1,7 @@ +namespace Shared.Kernel.Constants +{ + public class NotificationHubConstants + { + public const string Hub = "NotificationHub"; + } +} diff --git a/Source/Web/Server/Properties/launchSettings.json b/Source/Web/Server/Properties/launchSettings.json index 0725268f..58ee9057 100644 --- a/Source/Web/Server/Properties/launchSettings.json +++ b/Source/Web/Server/Properties/launchSettings.json @@ -23,7 +23,7 @@ }, "applicationUrl": "https://localhost:5001", "dotnetRunMessages": "true", - "launchUrl": "/" + "launchUrl": "" }, "Docker": { "commandName": "Docker", diff --git a/Source/Web/Server/Startup.cs b/Source/Web/Server/Startup.cs index f49047a7..4ce5c0d1 100644 --- a/Source/Web/Server/Startup.cs +++ b/Source/Web/Server/Startup.cs @@ -10,6 +10,7 @@ using Web.Server.BuildingBlocks; using Modules.Subscriptions.Features; using Modules.TenantIdentity.Features; +using Shared.Features; namespace Web.Server { @@ -22,9 +23,15 @@ public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironm Configuration = configuration; WebHostEnvironment = webHostEnvironment; } + // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddControllers(); + services.AddRazorPages(); + services.AddServerSideBlazor(); + + services.AddSharedFeatures(); services.AddBuildingBlocks(); services.AddModule(Configuration); @@ -46,6 +53,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseAuthentication(); app.UseAuthorization(); + app.UseSharedFeaturesMiddleware(env); app.UseBuildingBlocks(); app.UseEndpoints(endpoints => diff --git a/Source/Web/Server/appsettings.Docker.json b/Source/Web/Server/appsettings.Docker.json deleted file mode 100644 index 4cce3889..00000000 --- a/Source/Web/Server/appsettings.Docker.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "DetailedErrors": true, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "ConnectionStrings": { - "IdentityDbLocalConnectionString": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Identity;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=true;Trust Server Certificate = true", - "ApplicationDbContextConnection": "Server=sqlserver,1433;Integrated Security=true;Initial Catalog=Identity;User Id=sa;Password=@someThingComplicated1234;Trusted_Connection=true;Trust Server Certificate = true" - }, - "Redis": { - "Password": "@someThingComplicated1234", - "ConnectionString": "localhost:6379" - } -}