common: simplify wamp logic

previously we were opening a new channel for each topic which was unnecessary
This commit is contained in:
Abdullah Atta
2025-12-22 13:38:58 +05:00
parent 265b456c46
commit c7bb053cea
5 changed files with 103 additions and 39 deletions
@@ -26,11 +26,11 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WampSharp.AspNetCore.WebSockets.Server;
using WampSharp.Binding;
using WampSharp.V2;
using WampSharp.V2.Realm;
using Microsoft.Extensions.Hosting;
namespace Streetwriters.Common.Extensions
{
@@ -55,9 +55,9 @@ namespace Streetwriters.Common.Extensions
return app;
}
public static IApplicationBuilder UseWamp<T>(this IApplicationBuilder app, WampServer<T> server, Action<IWampHostedRealm, WampServer<T>> action) where T : new()
public static IApplicationBuilder UseWamp(this IApplicationBuilder app, WampServer server, Action<IWampHostedRealm, WampServer> action)
{
WampHost host = new WampHost();
WampHost host = new();
app.Map(server.Endpoint, builder =>
{
@@ -81,10 +81,8 @@ namespace Streetwriters.Common.Extensions
public static T GetScopedService<T>(this IApplicationBuilder app) where T : notnull
{
using (var scope = app.ApplicationServices.CreateScope())
{
return scope.ServiceProvider.GetRequiredService<T>();
}
using var scope = app.ApplicationServices.CreateScope();
return scope.ServiceProvider.GetRequiredService<T>();
}
public static IApplicationBuilder UseForwardedHeadersWithKnownProxies(this IApplicationBuilder app, IWebHostEnvironment env, string forwardedForHeaderName = null)
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Microsoft.Extensions.DependencyInjection;
using Streetwriters.Common.Accessors;
using Streetwriters.Data.DbContexts;
using Streetwriters.Data.Repositories;
@@ -25,6 +26,13 @@ namespace Streetwriters.Common.Extensions
{
public static class ServiceCollectionServiceExtensions
{
public static IServiceCollection AddWampServiceAccessor(this IServiceCollection services, Server server)
{
services.AddSingleton<WampServiceAccessor>((provider) => new WampServiceAccessor(server));
services.AddHostedService(provider => provider.GetRequiredService<WampServiceAccessor>());
return services;
}
public static IServiceCollection AddRepository<T>(this IServiceCollection services, string collectionName, string database) where T : class
{
services.AddSingleton((provider) => MongoDbContext.GetMongoCollection<T>(provider.GetRequiredService<MongoDB.Driver.IMongoClient>(), database, collectionName));