289 lines
10 KiB
C#
289 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xPushService.Configurations;
|
|
using xPushService.Constants;
|
|
using xPushService.Helpers;
|
|
using xPushService.Interfaces;
|
|
using xPushService.Providers;
|
|
using xPushService.Store;
|
|
|
|
namespace xPushService.DI {
|
|
public static partial class XDIHelperExtension {
|
|
/// <summary>
|
|
/// Extract Module Configuration from provided IConfiguration
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
/// <returns>an instance of XPushServiceConfiguration</returns>
|
|
public static XPushServiceConfiguration GetXPushServiceConfiguration (this IConfiguration config) {
|
|
//
|
|
var xPushConfigSection = config.GetSection (XPushServiceConstants.XConfigurationNodes.XPushServiceConfiguration);
|
|
return xPushConfigSection.Get<XPushServiceConfiguration> ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register XPushServiceHelper ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="helper"></param>
|
|
public static void AddXPushServiceHelper (
|
|
this IServiceCollection services,
|
|
XPushServiceHelper helper
|
|
) {
|
|
//
|
|
// Validate Args ...
|
|
if (helper.IsNull ()) {
|
|
//
|
|
Log ("AddXPushServiceHelper failed, helper not provided ...");
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
// Register Helper ...
|
|
services.AddSingleton (helper);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Module Provided Service on DI
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
public static void AddXPushService (
|
|
this IServiceCollection services,
|
|
IConfiguration config,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
) {
|
|
AddPushService (
|
|
services,
|
|
config.GetXPushServiceConfiguration (),
|
|
lifeTime
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Module Provided Service on DI
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
public static void AddXPushService (
|
|
this IServiceCollection services,
|
|
XPushServiceConfiguration config,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
) {
|
|
AddPushService (
|
|
services,
|
|
config,
|
|
lifeTime
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use Module Middlewares on Application Builder
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="helper"></param>
|
|
public static void UseXPushService (
|
|
this IApplicationBuilder app,
|
|
XPushServiceHelper helper
|
|
) {
|
|
//
|
|
// Validate Args ...
|
|
var isHelperExists = !helper.IsNull ();
|
|
if (!isHelperExists) {
|
|
//
|
|
Log ("AddXPushServiceHelper failed, helper not provided ...");
|
|
throw XException.InvalidArgs.ToException ();
|
|
}
|
|
|
|
//
|
|
// Create an Scope for Accessing Registered Services ...
|
|
using (var scope = app.ApplicationServices.CreateScope ()) {
|
|
//
|
|
// try to Retrieve Configuration ....
|
|
var config = scope.ServiceProvider.GetService<XPushServiceConfiguration> ();
|
|
if (
|
|
config.IsNull () ||
|
|
config.BaseRoute.IsNullOrEmpty () ||
|
|
config.BaseRoute.Trim ().IsNullOrEmpty ()
|
|
) {
|
|
//
|
|
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;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
// add XPushService Hub to Helper ...
|
|
//
|
|
var hubRoutes = new List<string> ();
|
|
|
|
//
|
|
// Try to Register Hubs ...
|
|
//
|
|
// Use SignalR in App ...
|
|
app.UseSignalR (routes => {
|
|
//
|
|
helper.GetHubs ().ForEach (hubDescriptor => {
|
|
//
|
|
var hubRoute = Path.Combine (config.BaseRoute, hubDescriptor.Route);
|
|
hubRoutes.Add (hubRoute);
|
|
Console.WriteLine ($"XPushService HubRoute: {hubRoute}");
|
|
|
|
//
|
|
Type routesType = routes.GetType ();
|
|
MethodInfo mapHubMethod = routesType
|
|
.GetMethods ()
|
|
.SingleOrDefault (m =>
|
|
m.Name == "MapHub" &&
|
|
m.GetParameters ().Length == 1
|
|
);
|
|
if (mapHubMethod.IsNull ()) {
|
|
throw XException.ActionFailed.ToException ();
|
|
}
|
|
|
|
//
|
|
object[] genericMapHubMethodArgs = { new PathString (hubRoute) };
|
|
MethodInfo genericMapHubMethod = mapHubMethod.MakeGenericMethod (hubDescriptor.Hub);
|
|
genericMapHubMethod.Invoke (routes, genericMapHubMethodArgs);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for XPushService ...
|
|
/// </summary>
|
|
private static string XLogTag = "XPushService";
|
|
|
|
/// <summary>
|
|
/// print a log in Console ...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private static void Log (string message) {
|
|
Console.WriteLine ($"{XLogTag} => {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Push Service ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="config"></param>
|
|
/// <param name="lifeTime"></param>
|
|
private static void AddPushService (
|
|
IServiceCollection services,
|
|
XPushServiceConfiguration config,
|
|
ServiceLifetime lifeTime
|
|
) {
|
|
//
|
|
#region Validate Args ...
|
|
//
|
|
var exception = XException.InvalidArgs.ToException ();;
|
|
|
|
//
|
|
// Services ...
|
|
var isServicesExists = !services.IsNull ();
|
|
if (!isServicesExists) {
|
|
//
|
|
Log ("AddXPushServiceHelper failed, services not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Config ...
|
|
var isConfigExists = !config.IsNull ();
|
|
if (!isConfigExists) {
|
|
//
|
|
Log ("AddXPushServiceHelper failed, configuration not provided ...");
|
|
throw exception;
|
|
}
|
|
|
|
//
|
|
// Lifetime ...
|
|
var isLifetimeExists = !lifeTime.IsNull ();
|
|
if (!isLifetimeExists) {
|
|
//
|
|
Log ("AddXPushServiceHelper failed, lifetime not provided ...");
|
|
throw exception;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
// Register XPushService Configuration ...
|
|
services.AddSingleton<XPushServiceConfiguration> (config);
|
|
|
|
//
|
|
#region Try to Register Stores ...
|
|
//
|
|
var groupStore = services.GetRegisteredService<IXPushGroupStore> ();
|
|
if (groupStore.IsNull ()) {
|
|
//
|
|
Log ($"there is no provided PushGroupStore, try to register XPushGroupInMemoryStore ...");
|
|
groupStore = new XPushGroupInMemoryStore ();
|
|
services.AddSingleton<IXPushGroupStore> (groupStore);
|
|
}
|
|
|
|
//
|
|
var connectionStore = services.GetRegisteredService<IXPushConnectionStore> ();
|
|
if (connectionStore.IsNull ()) {
|
|
//
|
|
Log ($"there is no provided XPushConnectionStore, try to register XPushConnectionInMemoryStore ...");
|
|
connectionStore = new XPushConnectionInMemoryStore ();
|
|
services.AddSingleton<IXPushConnectionStore> (connectionStore);
|
|
}
|
|
|
|
//
|
|
var webRtcConnectionStore = services.GetRegisteredService<IXWebRTCConnectionStore> ();
|
|
if (webRtcConnectionStore.IsNull ()) {
|
|
//
|
|
Log ($"there is no provided XWebRTCConnectionStore, try to register XWebRTCConnectionInMemoryStore ...");
|
|
webRtcConnectionStore = new XWebRTCConnectionInMemoryStore ();
|
|
services.AddSingleton<IXWebRTCConnectionStore> (webRtcConnectionStore);
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
// Register SignalR ...
|
|
var builder = services.AddSignalR ();
|
|
|
|
//
|
|
// Add NewtonSoftJson Protocol ...
|
|
builder.AddNewtonsoftJsonProtocol (x => {
|
|
x.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
x.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver ();
|
|
});
|
|
|
|
//
|
|
// Register XPushServiceUserNameProvider ...
|
|
services.AddSingleton<IUserIdProvider, XPushServiceUserNameProvider> ();
|
|
|
|
//
|
|
// add Message Protocol Support ...
|
|
if (config.AddSupportMessageProtocol) {
|
|
builder.AddMessagePackProtocol ();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |