瀏覽代碼

新增 cap微服务事务总线
新增 宿主应用HostApp注入Autofac容器
优化freesql实现库从ZhonTai.Admin移动到ZhonTai.Host中

zhontai 2 年之前
父節點
當前提交
87d71de1eb

+ 24 - 2
src/hosts/ZhonTai.Host/Program.cs

@@ -1,6 +1,10 @@
 using FreeScheduler;
 using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyModel;
 using Microsoft.Extensions.Hosting;
+using Savorboard.CAP.InMemoryMessageQueue;
+using System.Linq;
+using System.Reflection;
 using ZhonTai;
 using ZhonTai.Admin.Core;
 using ZhonTai.Admin.Core.Configs;
@@ -8,12 +12,24 @@ using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Core.Startup;
 using ZhonTai.Admin.Tools.TaskScheduler;
 using ZhonTai.ApiUI;
+using ZhonTai.Common.Helpers;
 
 new HostApp(new HostAppOptions
 {
 	//配置后置服务
 	ConfigurePostServices = context =>
 	{
+        //var appConfig = ConfigHelper.Get<AppConfig>("appconfig", context.Environment.EnvironmentName);
+        //Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
+        //    .Where(a => appConfig.AssemblyNames.Contains(a.Name))
+        //    .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
+        //context.Services.AddCap(config =>
+        //{
+        //    config.UseInMemoryStorage();
+        //    config.UseInMemoryMessageQueue();
+        //    config.UseDashboard();
+        //}).AddSubscriberAssembly(assemblies);
+
         //context.Services.AddTiDb(context);
 
         //添加任务调度
@@ -41,8 +57,14 @@ new HostApp(new HostAppOptions
         //context.Services.AddOSS();
     },
 
-    //配置mvc
-    ConfigureMvcBuilder = (mvcBuilder, options) =>
+    //配置Autofac容器
+    ConfigureAutofacContainer = (builder, context) =>
+    {
+
+    },
+
+    //配置Mvc
+    ConfigureMvcBuilder = (builder, context) =>
     {
     },
 

+ 16 - 1
src/hosts/ZhonTai.Host/ZhonTai.Host.csproj

@@ -8,13 +8,22 @@
 		<DocumentationFile></DocumentationFile>
 		<NoWarn>1701;1702;1591</NoWarn>
 	</PropertyGroup>
-
+	
 	<ItemGroup>
 	  <Content Include="Configs\createdbsql.txt">
 	    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 	  </Content>
 	</ItemGroup>
 
+	<ItemGroup Condition="'$(Configuration)'=='Debug'">
+		<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.691" />
+		<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.691" />
+		<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.691" />
+		<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.691" />
+		<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.691" />
+		<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.691" />
+	</ItemGroup>
+	
 	<ItemGroup>
 		<ProjectReference Include="..\..\platform\ZhonTai.Admin\ZhonTai.Admin.csproj" />
 		<ProjectReference Include="..\..\platform\ZhonTai.ApiUI\ZhonTai.ApiUI.csproj" />
@@ -24,6 +33,12 @@
 		<InternalsVisibleTo Include="ZhonTai.Tests" />
 	</ItemGroup>
 
+	<ItemGroup>
+	  <PackageReference Include="DotNetCore.CAP.Dashboard" Version="7.1.0" />
+	  <PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="7.1.0" />
+	  <PackageReference Include="Savorboard.CAP.InMemoryMessageQueue" Version="7.0.0" />
+	</ItemGroup>
+
 	<ItemGroup>
 		<Content Update="wwwroot\*\*">
 			<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

+ 5 - 3
src/platform/ZhonTai.Admin/Core/Extensions/RateLimitServiceCollectionExtensions.cs

@@ -26,9 +26,11 @@ public static class RateLimitServiceCollectionExtensions
         if (cacheConfig.TypeRateLimit == CacheType.Redis)
         {
             //redis
-            var redis = new RedisClient(cacheConfig.Redis.ConnectionStringRateLimit);
-            redis.Serialize = JsonConvert.SerializeObject;
-            redis.Deserialize = JsonConvert.DeserializeObject;
+            var redis = new RedisClient(cacheConfig.Redis.ConnectionStringRateLimit)
+            {
+                Serialize = JsonConvert.SerializeObject,
+                Deserialize = JsonConvert.DeserializeObject
+            };
             services.AddSingleton<IDistributedCache>(new DistributedCache(redis));
             services.AddDistributedRateLimiting();
         }

+ 9 - 0
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -112,6 +112,13 @@ public class HostApp
         //应用配置
         services.AddSingleton(appConfig);
 
+        var hostAppContext = new HostAppContext()
+        {
+            Services = services,
+            Environment = env,
+            Configuration = configuration
+        };
+
         //使用Autofac容器
         builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
         //配置Autofac容器
@@ -125,6 +132,8 @@ public class HostApp
 
             // 模块注入
             builder.RegisterModule(new RegisterModule(appConfig));
+
+            _hostAppOptions?.ConfigureAutofacContainer?.Invoke(builder, hostAppContext);
         });
 
         //配置Kestrel服务器

+ 8 - 2
src/platform/ZhonTai.Admin/Core/Startup/HostAppOptions.cs

@@ -1,4 +1,5 @@
-using FreeSql;
+using Autofac;
+using FreeSql;
 using Microsoft.Extensions.DependencyInjection;
 using System;
 using Yitter.IdGenerator;
@@ -27,10 +28,15 @@ public class HostAppOptions
     public Action<HostAppContext> ConfigurePostServices { get; set; }
 
     /// <summary>
-    /// 注入后置服务
+    /// 注入mvc构建器
     /// </summary>
     public Action<IMvcBuilder, HostAppContext> ConfigureMvcBuilder { get; set; }
 
+    /// <summary>
+    /// 注入Autofac容器
+    /// </summary>
+    public Action<ContainerBuilder, HostAppContext> ConfigureAutofacContainer { get; set; }
+
     /// <summary>
     /// 注入前置中间件
     /// </summary>

+ 1 - 9
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -14,6 +14,7 @@
 		<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
 		<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.1" />
 		<PackageReference Include="Castle.Core.AsyncInterceptor" Version="2.1.0" />
+		<PackageReference Include="DotNetCore.CAP" Version="7.1.0" />
 		<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
 		<PackageReference Include="FreeRedis" Version="1.0.7" />
 		<PackageReference Include="FreeRedis.DistributedCache" Version="1.0.5" />
@@ -37,15 +38,6 @@
 		<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
 	</ItemGroup>
 
-	<ItemGroup Condition="'$(Configuration)'=='Debug'">
-		<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.691" />
-		<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.691" />
-		<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.691" />
-		<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.691" />
-		<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.691" />
-		<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.691" />
-	</ItemGroup>
-
 	<ItemGroup>
 		<ProjectReference Include="..\ZhonTai.Common\ZhonTai.Common.csproj" />
 		<ProjectReference Include="..\ZhonTai.DynamicApi\ZhonTai.DynamicApi.csproj" />

+ 6 - 1
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -1978,7 +1978,12 @@
         </member>
         <member name="P:ZhonTai.Admin.Core.Startup.HostAppOptions.ConfigureMvcBuilder">
             <summary>
-            注入后置服务
+            注入mvc构建器
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Startup.HostAppOptions.ConfigureAutofacContainer">
+            <summary>
+            注入Autofac容器
             </summary>
         </member>
         <member name="P:ZhonTai.Admin.Core.Startup.HostAppOptions.ConfigurePreMiddleware">