JAELYS 2 年之前
父节点
当前提交
3bf3ca7bb7

+ 26 - 1
TEAMModelOS.SDK/DI/AzureSignalR/AzureSignalRExtensions.cs

@@ -6,11 +6,36 @@ using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using System.Linq;
 using System.Linq;
 using StackExchange.Redis;
 using StackExchange.Redis;
+using System.Collections.Concurrent;
+using Microsoft.Azure.SignalR.Management;
 
 
 namespace TEAMModelOS.SDK.DI
 namespace TEAMModelOS.SDK.DI
 {
 {
     public static class AzureSignalRExtensions
     public static class AzureSignalRExtensions
     {
     {
-       
+        private static ConcurrentDictionary<string, ServiceHubContext> ServiceHubContexts { get; } = new();
+
+
+
+        /// <summary>
+        /// µo°e«H®§¦Ü¹ï¦C©Î¥DÃD
+        /// </summary>       
+        /// <param name="name">QueueName or TopicName</param>
+        /// <param name="message">°T®§</param>
+        /// <returns></returns>
+        public static ServiceHubContext GetHubContext(this ServiceManager sm, string name)
+        {
+            try
+            {
+                ServiceHubContext hub = ServiceHubContexts.GetOrAdd(name, (x) =>
+                sm.CreateHubContextAsync(name, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult());
+
+                return hub;
+            }
+            catch
+            {
+                throw;
+            }
+        }
     }
     }
 }
 }

+ 9 - 9
TEAMModelOS.SDK/DI/AzureSignalR/AzureSignalRFactory.cs

@@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
 using Microsoft.Extensions.Options;
 using StackExchange.Redis;
 using StackExchange.Redis;
 using System;
 using System;
+using System.Collections.Concurrent;
 
 
 namespace TEAMModelOS.SDK.DI
 namespace TEAMModelOS.SDK.DI
 {
 {
@@ -10,6 +11,7 @@ namespace TEAMModelOS.SDK.DI
     {
     {
         private readonly IServiceProvider _services;
         private readonly IServiceProvider _services;
         private readonly IOptionsMonitor<AzureSignalRFactoryOptions> _optionsMonitor;
         private readonly IOptionsMonitor<AzureSignalRFactoryOptions> _optionsMonitor;
+        private ConcurrentDictionary<string, ServiceManager> ServiceManagers { get; } = new();
         private readonly ILogger _logger;
         private readonly ILogger _logger;
 
 
         public AzureSignalRFactory(IServiceProvider services, IOptionsMonitor<AzureSignalRFactoryOptions> optionsMonitor, ILogger<AzureSignalRFactory> logger)
         public AzureSignalRFactory(IServiceProvider services, IOptionsMonitor<AzureSignalRFactoryOptions> optionsMonitor, ILogger<AzureSignalRFactory> logger)
@@ -20,18 +22,16 @@ namespace TEAMModelOS.SDK.DI
         }
         }
 
 
         public ServiceManager GetServiceManager(string name = "Default")
         public ServiceManager GetServiceManager(string name = "Default")
-        {            
+        {
             try
             try
             {
             {
+                var serviceManager = ServiceManagers.GetOrAdd(name, x =>
                 // TODO(Mickey) : Signalr SDK 1.10.0之後,aud會無法擴充,會變成asrs.u.aud,會造成無法使用restAPI,故先降回舊版
                 // TODO(Mickey) : Signalr SDK 1.10.0之後,aud會無法擴充,會變成asrs.u.aud,會造成無法使用restAPI,故先降回舊版
-                var serviceManager = new ServiceManagerBuilder()
-                     .WithOptions(option =>
-                     {
-                         option.ConnectionString = _optionsMonitor.Get(name).SignalRConnectionString;
-                         option.ServiceTransportType = ServiceTransportType.Persistent;
-                         // option.ServiceTransportType = ServiceTransportType.Persistent
-                     })
-                     .BuildServiceManager();
+                new ServiceManagerBuilder().WithOptions(option =>
+                {
+                    option.ConnectionString = _optionsMonitor.Get(name).SignalRConnectionString;
+                    option.ServiceTransportType = ServiceTransportType.Persistent;
+                }).BuildServiceManager());
 
 
                 return serviceManager;
                 return serviceManager;
             }
             }

+ 1 - 1
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -29,7 +29,7 @@
     <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
     <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
     <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.3" />
     <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.3" />
     <PackageReference Include="Microsoft.Azure.SignalR.Management" Version="1.18.0" />
     <PackageReference Include="Microsoft.Azure.SignalR.Management" Version="1.18.0" />
-    <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="6.0.5" />
+    <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="6.0.6" />
     <PackageReference Include="Microsoft.Identity.Client" Version="4.39.0" />
     <PackageReference Include="Microsoft.Identity.Client" Version="4.39.0" />
     <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
     <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
     <PackageReference Include="NUnit" Version="3.13.2" />
     <PackageReference Include="NUnit" Version="3.13.2" />

+ 25 - 29
TEAMModelOS/Controllers/OpenApi/IRS/TianboController.cs

@@ -71,46 +71,42 @@ namespace TEAMModelOS.Controllers
 
 
                     if (!string.IsNullOrWhiteSpace(stu?.channel) && !string.IsNullOrWhiteSpace(stu?.userid))
                     if (!string.IsNullOrWhiteSpace(stu?.channel) && !string.IsNullOrWhiteSpace(stu?.userid))
                     {
                     {
-                        var serviceManager = _azureSignalR.GetServiceManager();
-                        var cancel = new CancellationToken();
-                        var hub = await serviceManager.CreateHubContextAsync($"C{stu.channel}", cancel);
-                        try
+                        var hub = _azureSignalR.GetServiceManager().GetHubContext($"C{stu.channel}");
+                        //var cancel = new CancellationToken();
+                        //using var hub = await serviceManager.CreateHubContextAsync($"C{stu.channel}", cancel);
+
+                        var con = content.GetString();
+                        var ans = con switch
                         {
                         {
-                            var con = content.GetString();
-                            var ans = con switch
-                            {
-                                string _ when con.Length == 1 => new string[] { con },
-                                string _ when con.Contains('+', StringComparison.OrdinalIgnoreCase) => con.Split('+'),
-                                string _ when con.Equals("true", StringComparison.OrdinalIgnoreCase) => new string[] { "A" },
-                                string _ when con.Equals("false", StringComparison.OrdinalIgnoreCase) => new string[] { "B" },
-                                _ => throw new ArgumentNullException(nameof(content))
-                            };
+                            string _ when con.Length == 1 => new string[] { con },
+                            string _ when con.Contains('+', StringComparison.OrdinalIgnoreCase) => con.Split('+'),
+                            string _ when con.Equals("true", StringComparison.OrdinalIgnoreCase) => new string[] { "A" },
+                            string _ when con.Equals("false", StringComparison.OrdinalIgnoreCase) => new string[] { "B" },
+                            _ => throw new ArgumentNullException(nameof(content))
+                        };
 
 
-                            var common = JsonSerializer.Serialize(new
-                            {
-                                action = "DirectIRS.Answer",
-                                clientType = "DI",
-                                sender = stu.stuid,
-                                timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
-                                waitReturn = false,
-                                payload = new { irsno = "", answer = ans }
-                            });
+                        var common = JsonSerializer.Serialize(new
+                        {
+                            action = "DirectIRS.Answer",
+                            clientType = "DI",
+                            sender = stu.stuid,
+                            timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
+                            waitReturn = false,
+                            payload = new { irsno = "", answer = ans }
+                        });
 
 
-                            await hub.Clients.User(stu.userid).SendCoreAsync("onMessage", new[]{new {
+                        await hub.Clients.User(stu.userid).SendCoreAsync("onMessage", new[]{new {
                                 connectionId = (string)null,
                                 connectionId = (string)null,
                                 to = (string)null,
                                 to = (string)null,
                                 groupname = (string)null,
                                 groupname = (string)null,
                                 sender = stu.stuid,
                                 sender = stu.stuid,
                                 text = common
                                 text = common
                             }});
                             }});
-                        }
-                        finally
-                        {
-                            hub.Dispose();
-                        }
+
                     }
                     }
+                    await _dingDing.SendBotMsg($"IES5,{_option.Location},tianbo/api/course/cardUploadData()\n{stu?.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
                 }
                 }
-                await _dingDing.SendBotMsg($"IES5,{_option.Location},tianbo/api/course/cardUploadData()\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+
                 return Ok(new { code = 200, msg = "成功", data = (string)null });
                 return Ok(new { code = 200, msg = "成功", data = (string)null });
             }
             }
             catch (Exception ex)
             catch (Exception ex)