Fix Hub Route issue on Registering ...

This commit is contained in:
2025-07-03 15:07:33 +03:30
parent d862146642
commit c7cf0c4df1
+110 -85
View File
@@ -19,17 +19,20 @@ using xPushService.Interfaces;
using xPushService.Providers; using xPushService.Providers;
using xPushService.Store; using xPushService.Store;
namespace xPushService.DI { namespace xPushService.DI
public static partial class XDIHelperExtension { {
public static partial class XDIHelperExtension
{
/// <summary> /// <summary>
/// Extract Module Configuration from provided IConfiguration /// Extract Module Configuration from provided IConfiguration
/// </summary> /// </summary>
/// <param name="config"></param> /// <param name="config"></param>
/// <returns>an instance of XPushServiceConfiguration</returns> /// <returns>an instance of XPushServiceConfiguration</returns>
public static XPushServiceConfiguration GetXPushServiceConfiguration (this IConfiguration config) { public static XPushServiceConfiguration GetXPushServiceConfiguration(this IConfiguration config)
{
// //
var xPushConfigSection = config.GetSection (XPushServiceConstants.XConfigurationNodes.XPushServiceConfiguration); var xPushConfigSection = config.GetSection(XPushServiceConstants.XConfigurationNodes.XPushServiceConfiguration);
return xPushConfigSection.Get<XPushServiceConfiguration> (); return xPushConfigSection.Get<XPushServiceConfiguration>();
} }
/// <summary> /// <summary>
@@ -37,21 +40,23 @@ namespace xPushService.DI {
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="helper"></param> /// <param name="helper"></param>
public static void AddXPushServiceHelper ( public static void AddXPushServiceHelper(
this IServiceCollection services, this IServiceCollection services,
XPushServiceHelper helper XPushServiceHelper helper
) { )
{
// //
// Validate Args ... // Validate Args ...
if (helper.IsNull ()) { if (helper.IsNull())
{
// //
Log ("AddXPushServiceHelper failed, helper not provided ..."); Log("AddXPushServiceHelper failed, helper not provided ...");
throw XException.InvalidArgs.ToException (); throw XException.InvalidArgs.ToException();
} }
// //
// Register Helper ... // Register Helper ...
services.AddSingleton (helper); services.AddSingleton(helper);
} }
/// <summary> /// <summary>
@@ -59,14 +64,15 @@ namespace xPushService.DI {
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="config"></param> /// <param name="config"></param>
public static void AddXPushService ( public static void AddXPushService(
this IServiceCollection services, this IServiceCollection services,
IConfiguration config, IConfiguration config,
ServiceLifetime lifeTime = ServiceLifetime.Singleton ServiceLifetime lifeTime = ServiceLifetime.Singleton
) { )
AddPushService ( {
AddPushService(
services, services,
config.GetXPushServiceConfiguration (), config.GetXPushServiceConfiguration(),
lifeTime lifeTime
); );
} }
@@ -76,12 +82,13 @@ namespace xPushService.DI {
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="config"></param> /// <param name="config"></param>
public static void AddXPushService ( public static void AddXPushService(
this IServiceCollection services, this IServiceCollection services,
XPushServiceConfiguration config, XPushServiceConfiguration config,
ServiceLifetime lifeTime = ServiceLifetime.Singleton ServiceLifetime lifeTime = ServiceLifetime.Singleton
) { )
AddPushService ( {
AddPushService(
services, services,
config, config,
lifeTime lifeTime
@@ -93,41 +100,46 @@ namespace xPushService.DI {
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="helper"></param> /// <param name="helper"></param>
public static void UseXPushService ( public static void UseXPushService(
this IApplicationBuilder app, this IApplicationBuilder app,
XPushServiceHelper helper XPushServiceHelper helper
) { )
{
// //
// Validate Args ... // Validate Args ...
var isHelperExists = !helper.IsNull (); var isHelperExists = !helper.IsNull();
if (!isHelperExists) { if (!isHelperExists)
{
// //
Log ("AddXPushServiceHelper failed, helper not provided ..."); Log("AddXPushServiceHelper failed, helper not provided ...");
throw XException.InvalidArgs.ToException (); throw XException.InvalidArgs.ToException();
} }
// //
// Create an Scope for Accessing Registered Services ... // Create an Scope for Accessing Registered Services ...
using (var scope = app.ApplicationServices.CreateScope ()) { using (var scope = app.ApplicationServices.CreateScope())
{
// //
// try to Retrieve Configuration .... // try to Retrieve Configuration ....
var config = scope.ServiceProvider.GetService<XPushServiceConfiguration> (); var config = scope.ServiceProvider.GetService<XPushServiceConfiguration>();
if ( if (
config.IsNull () || config.IsNull() ||
config.BaseRoute.IsNullOrEmpty () || config.BaseRoute.IsNullOrEmpty() ||
config.BaseRoute.Trim ().IsNullOrEmpty () config.BaseRoute.Trim().IsNullOrEmpty()
) { )
{
// //
Log ("AddXPushServiceHelper failed, Configuration not provided ..."); Log("AddXPushServiceHelper failed, Configuration not provided ...");
throw XException.InvalidConfiguration.ToException (); throw XException.InvalidConfiguration.ToException();
} }
// //
#region Normalize Routes ... #region Normalize Routes ...
// //
// Base Reoute ... // Base Reoute ...
config.BaseRoute = config.BaseRoute.ToNormalString (); config.BaseRoute = config.BaseRoute.ToNormalString();
if (!config.BaseRoute.StartsWith ("/")) { if (!config.BaseRoute.StartsWith("/"))
{
config.BaseRoute = "/" + config.BaseRoute; config.BaseRoute = "/" + config.BaseRoute;
} }
#endregion #endregion
@@ -135,36 +147,39 @@ namespace xPushService.DI {
// //
// add XPushService Hub to Helper ... // add XPushService Hub to Helper ...
// //
var hubRoutes = new List<string> (); var hubRoutes = new List<string>();
// //
// Try to Register Hubs ... // Try to Register Hubs ...
// //
// Use SignalR in App ... // Use SignalR in App ...
app.UseSignalR (routes => { app.UseSignalR(routes =>
{
// //
helper.GetHubs ().ForEach (hubDescriptor => { helper.GetHubs().ForEach(hubDescriptor =>
{
// //
var hubRoute = Path.Combine (config.BaseRoute, hubDescriptor.Route); var hubRoute = config.BaseRoute + "/" + hubDescriptor.Route; // Path.Combine (config.BaseRoute, hubDescriptor.Route);
hubRoutes.Add (hubRoute); hubRoutes.Add(hubRoute);
Console.WriteLine ($"XPushService HubRoute: {hubRoute}"); Console.WriteLine($"XPushService HubRoute: {hubRoute}");
// //
Type routesType = routes.GetType (); Type routesType = routes.GetType();
MethodInfo mapHubMethod = routesType MethodInfo mapHubMethod = routesType
.GetMethods () .GetMethods()
.SingleOrDefault (m => .SingleOrDefault(m =>
m.Name == "MapHub" && m.Name == "MapHub" &&
m.GetParameters ().Length == 1 m.GetParameters().Length == 1
); );
if (mapHubMethod.IsNull ()) { if (mapHubMethod.IsNull())
throw XException.ActionFailed.ToException (); {
throw XException.ActionFailed.ToException();
} }
// //
object[] genericMapHubMethodArgs = { new PathString (hubRoute) }; object[] genericMapHubMethodArgs = { new PathString(hubRoute) };
MethodInfo genericMapHubMethod = mapHubMethod.MakeGenericMethod (hubDescriptor.Hub); MethodInfo genericMapHubMethod = mapHubMethod.MakeGenericMethod(hubDescriptor.Hub);
genericMapHubMethod.Invoke (routes, genericMapHubMethodArgs); genericMapHubMethod.Invoke(routes, genericMapHubMethodArgs);
}); });
}); });
} }
@@ -181,8 +196,9 @@ namespace xPushService.DI {
/// print a log in Console ... /// print a log in Console ...
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message"></param>
private static void Log (string message) { private static void Log(string message)
Console.WriteLine ($"{XLogTag} => {message}"); {
Console.WriteLine($"{XLogTag} => {message}");
} }
/// <summary> /// <summary>
@@ -191,97 +207,106 @@ namespace xPushService.DI {
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="lifeTime"></param> /// <param name="lifeTime"></param>
private static void AddPushService ( private static void AddPushService(
IServiceCollection services, IServiceCollection services,
XPushServiceConfiguration config, XPushServiceConfiguration config,
ServiceLifetime lifeTime ServiceLifetime lifeTime
) { )
{
// //
#region Validate Args ... #region Validate Args ...
// //
var exception = XException.InvalidArgs.ToException ();; var exception = XException.InvalidArgs.ToException(); ;
// //
// Services ... // Services ...
var isServicesExists = !services.IsNull (); var isServicesExists = !services.IsNull();
if (!isServicesExists) { if (!isServicesExists)
{
// //
Log ("AddXPushServiceHelper failed, services not provided ..."); Log("AddXPushServiceHelper failed, services not provided ...");
throw exception; throw exception;
} }
// //
// Config ... // Config ...
var isConfigExists = !config.IsNull (); var isConfigExists = !config.IsNull();
if (!isConfigExists) { if (!isConfigExists)
{
// //
Log ("AddXPushServiceHelper failed, configuration not provided ..."); Log("AddXPushServiceHelper failed, configuration not provided ...");
throw exception; throw exception;
} }
// //
// Lifetime ... // Lifetime ...
var isLifetimeExists = !lifeTime.IsNull (); var isLifetimeExists = !lifeTime.IsNull();
if (!isLifetimeExists) { if (!isLifetimeExists)
{
// //
Log ("AddXPushServiceHelper failed, lifetime not provided ..."); Log("AddXPushServiceHelper failed, lifetime not provided ...");
throw exception; throw exception;
} }
#endregion #endregion
// //
// Register XPushService Configuration ... // Register XPushService Configuration ...
services.AddSingleton<XPushServiceConfiguration> (config); services.AddSingleton<XPushServiceConfiguration>(config);
// //
#region Try to Register Stores ... #region Try to Register Stores ...
// //
var groupStore = services.GetRegisteredService<IXPushGroupStore> (); var groupStore = services.GetRegisteredService<IXPushGroupStore>();
if (groupStore.IsNull ()) { if (groupStore.IsNull())
{
// //
Log ($"there is no provided PushGroupStore, try to register XPushGroupInMemoryStore ..."); Log($"there is no provided PushGroupStore, try to register XPushGroupInMemoryStore ...");
groupStore = new XPushGroupInMemoryStore (); groupStore = new XPushGroupInMemoryStore();
services.AddSingleton<IXPushGroupStore> (groupStore); services.AddSingleton<IXPushGroupStore>(groupStore);
} }
// //
var connectionStore = services.GetRegisteredService<IXPushConnectionStore> (); var connectionStore = services.GetRegisteredService<IXPushConnectionStore>();
if (connectionStore.IsNull ()) { if (connectionStore.IsNull())
{
// //
Log ($"there is no provided XPushConnectionStore, try to register XPushConnectionInMemoryStore ..."); Log($"there is no provided XPushConnectionStore, try to register XPushConnectionInMemoryStore ...");
connectionStore = new XPushConnectionInMemoryStore (); connectionStore = new XPushConnectionInMemoryStore();
services.AddSingleton<IXPushConnectionStore> (connectionStore); services.AddSingleton<IXPushConnectionStore>(connectionStore);
} }
// //
var webRtcConnectionStore = services.GetRegisteredService<IXWebRTCConnectionStore> (); var webRtcConnectionStore = services.GetRegisteredService<IXWebRTCConnectionStore>();
if (webRtcConnectionStore.IsNull ()) { if (webRtcConnectionStore.IsNull())
{
// //
Log ($"there is no provided XWebRTCConnectionStore, try to register XWebRTCConnectionInMemoryStore ..."); Log($"there is no provided XWebRTCConnectionStore, try to register XWebRTCConnectionInMemoryStore ...");
webRtcConnectionStore = new XWebRTCConnectionInMemoryStore (); webRtcConnectionStore = new XWebRTCConnectionInMemoryStore();
services.AddSingleton<IXWebRTCConnectionStore> (webRtcConnectionStore); services.AddSingleton<IXWebRTCConnectionStore>(webRtcConnectionStore);
} }
#endregion #endregion
// //
// Register SignalR ... // Register SignalR ...
var builder = services.AddSignalR (); var builder = services.AddSignalR();
// //
// Add NewtonSoftJson Protocol ... // Add NewtonSoftJson Protocol ...
builder.AddNewtonsoftJsonProtocol (x => { builder.AddNewtonsoftJsonProtocol(x =>
{
x.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; x.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
x.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver (); x.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}); });
// //
// Register XPushServiceUserNameProvider ... // Register XPushServiceUserNameProvider ...
services.AddSingleton<IUserIdProvider, XPushServiceUserNameProvider> (); services.AddSingleton<IUserIdProvider, XPushServiceUserNameProvider>();
// //
// add Message Protocol Support ... // add Message Protocol Support ...
if (config.AddSupportMessageProtocol) { if (config.AddSupportMessageProtocol)
builder.AddMessagePackProtocol (); {
builder.AddMessagePackProtocol();
} }
} }
#endregion #endregion