Fix Hub Route issue on Registering ...

This commit is contained in:
2025-07-03 15:07:33 +03:30
parent d862146642
commit c7cf0c4df1
+51 -26
View File
@@ -19,14 +19,17 @@ 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>();
@@ -40,10 +43,12 @@ namespace xPushService.DI {
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();
@@ -63,7 +68,8 @@ namespace xPushService.DI {
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(),
@@ -80,7 +86,8 @@ namespace xPushService.DI {
this IServiceCollection services, this IServiceCollection services,
XPushServiceConfiguration config, XPushServiceConfiguration config,
ServiceLifetime lifeTime = ServiceLifetime.Singleton ServiceLifetime lifeTime = ServiceLifetime.Singleton
) { )
{
AddPushService( AddPushService(
services, services,
config, config,
@@ -96,11 +103,13 @@ namespace xPushService.DI {
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();
@@ -108,7 +117,8 @@ namespace xPushService.DI {
// //
// 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>();
@@ -116,7 +126,8 @@ namespace xPushService.DI {
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();
@@ -127,7 +138,8 @@ namespace xPushService.DI {
// //
// 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
@@ -141,11 +153,13 @@ namespace xPushService.DI {
// 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}");
@@ -157,7 +171,8 @@ namespace xPushService.DI {
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();
} }
@@ -181,7 +196,8 @@ 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}");
} }
@@ -195,7 +211,8 @@ namespace xPushService.DI {
IServiceCollection services, IServiceCollection services,
XPushServiceConfiguration config, XPushServiceConfiguration config,
ServiceLifetime lifeTime ServiceLifetime lifeTime
) { )
{
// //
#region Validate Args ... #region Validate Args ...
// //
@@ -204,7 +221,8 @@ namespace xPushService.DI {
// //
// 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;
@@ -213,7 +231,8 @@ namespace xPushService.DI {
// //
// 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;
@@ -222,7 +241,8 @@ namespace xPushService.DI {
// //
// 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;
@@ -237,7 +257,8 @@ namespace xPushService.DI {
#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();
@@ -246,7 +267,8 @@ namespace xPushService.DI {
// //
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();
@@ -255,7 +277,8 @@ namespace xPushService.DI {
// //
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();
@@ -269,7 +292,8 @@ namespace xPushService.DI {
// //
// 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();
}); });
@@ -280,7 +304,8 @@ namespace xPushService.DI {
// //
// add Message Protocol Support ... // add Message Protocol Support ...
if (config.AddSupportMessageProtocol) { if (config.AddSupportMessageProtocol)
{
builder.AddMessagePackProtocol(); builder.AddMessagePackProtocol();
} }
} }