修改代码

This commit is contained in:
cli 2025-04-25 17:41:43 +08:00
parent bf4f631a8c
commit 5adabb3b7e
7 changed files with 59 additions and 88 deletions

View File

@ -0,0 +1,6 @@
namespace JiShe.CollectBus.Protocol.Contracts.ProtocolPools
{
public interface IPluginContainer
{
}
}

View File

@ -0,0 +1,26 @@
using JiShe.CollectBus.Interfaces;
namespace JiShe.CollectBus.Protocol.Contracts.ProtocolPools
{
public class PluginContainer: IPluginContainer
{
public Dictionary<string, object> ProtocolPools;
public PluginContainer()
{
}
}
public static class ServiceProviderKeyedServiceExtensions
{
//public static Task AddKeyedSingleton<TImp>(this IServiceProvider provider, string key)
//{
// //var aa = Activator.CreateInstance<TImp>();
//}
}
}

View File

@ -18,7 +18,6 @@ namespace JiShe.CollectBus.Protocol.Test
{
Console.WriteLine("TestProtocolPlugin OnApplicationInitializationAsync");
var protocol = context.ServiceProvider.GetRequiredKeyedService<IProtocolPlugin>(nameof(TestProtocolPlugin));
RemoveServiceAtRuntime(context.ServiceProvider);
await protocol.LoadAsync();
}
@ -27,19 +26,5 @@ namespace JiShe.CollectBus.Protocol.Test
Console.WriteLine("TestProtocolPlugin OnApplicationShutdown");
base.OnApplicationShutdown(context);
}
public void RemoveServiceAtRuntime(IServiceProvider serviceProvider)
{
var services = serviceProvider.GetService<IServiceCollection>();
services?.AddKeyedSingleton<IProtocolPlugin, TestProtocolPlugin>(nameof(TestProtocolPlugin));
//var services = (IServiceCollection)serviceProvider.GetService(typeof(IServiceCollection));
//var pluginService = serviceProvider.GetKeyedService<IProtocolPlugin>(nameof(TestProtocolPlugin));
//if (services != null && pluginService!=null)
//{
// services.Remove(pluginService);
//}
}
}
}

View File

@ -20,7 +20,7 @@ namespace JiShe.CollectBus.Protocol.Test
{
}
public sealed override ProtocolInfo Info => new(nameof(TestProtocolPlugin), "Test", "TCP", "Test协议", "DTS1980-TEST");
public sealed override ProtocolInfo Info => new(nameof(TestProtocolPlugin), "Test", "TCP", "Test协议", "DTS1980-TEST-01");
public override Task<T> AnalyzeAsync<T>(ITcpSessionClient client, string messageReceived, Action<T>? receivedAction = null)
{

View File

@ -1,34 +1,24 @@
using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.FreeSql;
using JiShe.CollectBus.Kafka;
using JiShe.CollectBus.Kafka.AdminClient;
using JiShe.CollectBus.Protocol.Contracts;
using JiShe.CollectBus.ScheduledMeterReading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Cassandra.Mapping;
using JiShe.CollectBus.Cassandra;
using JiShe.CollectBus.FreeRedis;
using JiShe.CollectBus.FreeSql;
using JiShe.CollectBus.Interceptors;
using JiShe.CollectBus.IoTDB;
using JiShe.CollectBus.Kafka;
using JiShe.CollectBus.Mappers;
using JiShe.CollectBus.Protocol.Contracts;
using JiShe.CollectBus.ScheduledMeterReading;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Application;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
using Microsoft.Extensions.Options;
using JiShe.CollectBus.Kafka.Internal;
using JiShe.CollectBus.Interceptors;
using JiShe.CollectBus.Common.Attributes;
using ZstdSharp.Unsafe;
namespace JiShe.CollectBus;
@ -45,7 +35,7 @@ namespace JiShe.CollectBus;
typeof(CollectBusIoTDbModule),
typeof(CollectBusCassandraModule),
typeof(CollectBusProtocolModule)
)]
)]
public class CollectBusApplicationModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
@ -53,10 +43,7 @@ public class CollectBusApplicationModule : AbpModule
var configuration = context.Services.GetConfiguration();
context.Services.AddAutoMapperObjectMapper<CollectBusApplicationModule>();
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<CollectBusApplicationModule>(validate: true);
});
Configure<AbpAutoMapperOptions>(options => { options.AddMaps<CollectBusApplicationModule>(true); });
context.Services.AddSingleton(new MappingConfiguration()
.Define(new CollectBusMapping()));
@ -65,11 +52,8 @@ public class CollectBusApplicationModule : AbpModule
context.Services.OnRegistered(ctx =>
{
var methods = ctx.ImplementationType.GetMethods();
var any = methods.Any(a=>a.GetCustomAttribute<LogInterceptAttribute>()!=null);
if (any)
{
ctx.Interceptors.TryAdd<LogInterceptor>();
}
var any = methods.Any(a => a.GetCustomAttribute<LogInterceptAttribute>() != null);
if (any) ctx.Interceptors.TryAdd<LogInterceptor>();
});
}
@ -77,11 +61,9 @@ public class CollectBusApplicationModule : AbpModule
ApplicationInitializationContext context)
{
var assembly = Assembly.GetExecutingAssembly();
var types = assembly.GetTypes().Where(t => typeof(ICollectWorker).IsAssignableFrom(t) && !t.IsInterface).ToList();
foreach (var type in types)
{
await context.AddBackgroundWorkerAsync(type);
}
var types = assembly.GetTypes().Where(t => typeof(ICollectWorker).IsAssignableFrom(t) && !t.IsInterface)
.ToList();
foreach (var type in types) await context.AddBackgroundWorkerAsync(type);
Task.Run(() =>
{
@ -90,7 +72,5 @@ public class CollectBusApplicationModule : AbpModule
dbContext.InitAmmeterCacheData();
//await dbContext.InitWatermeterCacheData();
}).ConfigureAwait(false);
}
}

View File

@ -31,30 +31,6 @@ namespace JiShe.CollectBus.DynamicModule
return _moduleContainer.Modules.Select(m => m.Type).ToArray();
}
public async Task InitializeModuleAsync(Type moduleType)
{
//if (!typeof(IAbpModule).IsAssignableFrom(moduleType))
//{
// throw new ArgumentException($"指定的类型 {moduleType.FullName} 不是有效的ABP模块类型", nameof(moduleType));
//}
//var module = (IAbpModule)Activator.CreateInstance(moduleType);
//// 配置服务
//var configureServicesMethod = moduleType.GetMethod("ConfigureServices",
// BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
//if (configureServicesMethod != null)
//{
// var serviceConfigurationContext = CreateServiceConfigurationContext();
// configureServicesMethod.Invoke(module, new object[] { serviceConfigurationContext });
//}
//await CallModuleMethodAsync(module, "OnApplicationInitializationAsync", new object[] { new ApplicationInitializationContext(_serviceProvider) });
}
public async Task ReinitializeModuleAsync(Type moduleType)
{
if (!typeof(IAbpModule).IsAssignableFrom(moduleType))
@ -70,16 +46,14 @@ namespace JiShe.CollectBus.DynamicModule
var module = moduleDescriptor.Instance;
await CallModuleMethodAsync(module, "OnApplicationShutdown", new object[] { new ApplicationShutdownContext(_serviceProvider) });
//var configureServicesMethod = moduleType.GetMethod("ConfigureServices",
// BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
//if (configureServicesMethod != null)
//{
// var serviceConfigurationContext = CreateServiceConfigurationContext();
// configureServicesMethod.Invoke(module, new object[] { serviceConfigurationContext });
//}
await CallModuleMethodAsync(module, "OnApplicationInitializationAsync", new object[] { new ApplicationInitializationContext(_serviceProvider) });
await CallModuleMethodAsync(module, "OnApplicationShutdown", [new ApplicationShutdownContext(_serviceProvider)]);
var configureServicesMethod = moduleType.GetMethod("ConfigureServices",BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
if (configureServicesMethod != null)
{
var serviceConfigurationContext = CreateServiceConfigurationContext();
configureServicesMethod.Invoke(module, [serviceConfigurationContext]);
}
await CallModuleMethodAsync(module, "OnApplicationInitializationAsync", [new ApplicationInitializationContext(_serviceProvider)]);
}
public async Task UnloadModuleAsync(Type moduleType)
@ -157,7 +131,7 @@ namespace JiShe.CollectBus.DynamicModule
services.Add(service);
}
return (ServiceConfigurationContext)constructor.Invoke(new object[] { services });
return (ServiceConfigurationContext)constructor.Invoke([services]);
}
// 如果反射失败,使用备选方案

View File

@ -141,5 +141,5 @@
"DefaultIdempotence": true
}
},
"PlugInFolder": ""
"PlugInFolder": "C:\\Users\\Dai Zan\\Desktop\\Plugins"
}