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