diff --git a/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs b/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..d68f636 --- /dev/null +++ b/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs @@ -0,0 +1,96 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using JiShe.CollectBus.Protocol.Contracts.DependencyInjection; +using TouchSocket.Core; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class DependencyInjectionExtensions + { + public static void ServiceRegister(this IServiceCollection services,params Assembly[] assemblies) + { + if (assemblies.Length <= 0) + { + assemblies = [Assembly.GetExecutingAssembly()]; + } + + foreach (var assembly in assemblies) + { + var allTypes = assembly.GetTypes(); + foreach (var type in allTypes) + { + if (typeof(ITransientDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddTransient(interfaceType, type); + } + } + } + + foreach (var type in allTypes) + { + if (typeof(ISingletonDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddSingleton(interfaceType, type); + } + } + } + + foreach (var type in allTypes) + { + if (typeof(IScopedDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency")); + foreach (var interfaceType in interfaceTypes) + { + services.AddScoped(interfaceType, type); + } + } + } + } + } + + public static void PluginServiceRegister(this IServiceCollection services,string pluginPath="") + { + if (pluginPath.IsNullOrWhiteSpace()) + { + pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins"); + } + var assemblies = GetAssembliesFromFolder(pluginPath); + ServiceRegister(services, assemblies.ToArray()); + } + + public static IEnumerable GetAssembliesFromFolder(string folderPath) + { + var directory = new DirectoryInfo(folderPath); + var files = directory.GetFiles("*.dll"); + + var assemblies = new List(); + foreach (var file in files) + { + try + { + var assembly = Assembly.LoadFrom(file.FullName); + assemblies.Add(assembly); + } + catch (Exception ex) + { + Console.WriteLine($"Error loading assembly from {file.FullName}: {ex.Message}"); + } + } + + return assemblies; + } + } +} diff --git a/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj b/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj index d5101fa..670155d 100644 --- a/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj +++ b/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj @@ -7,6 +7,16 @@ enable + + + + + + + Always + + + @@ -15,6 +25,7 @@ + diff --git a/JiShe.CollectBus.Console/Program.cs b/JiShe.CollectBus.Console/Program.cs index de1bb00..2318757 100644 --- a/JiShe.CollectBus.Console/Program.cs +++ b/JiShe.CollectBus.Console/Program.cs @@ -29,7 +29,8 @@ namespace JiShe.CollectBus.Console private static void ConfigureServices(IServiceCollection services, HostBuilderContext hostContext) { var configuration = hostContext.Configuration; - + services.ServiceRegister(); + services.PluginServiceRegister(); services.AddTcp(); services.AddUdp(); diff --git a/JiShe.CollectBus.Core/JiShe.CollectBus.Core.csproj b/JiShe.CollectBus.Core/JiShe.CollectBus.Core.csproj index b791d87..310b9fe 100644 --- a/JiShe.CollectBus.Core/JiShe.CollectBus.Core.csproj +++ b/JiShe.CollectBus.Core/JiShe.CollectBus.Core.csproj @@ -15,8 +15,4 @@ - - - - diff --git a/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/IScopedDependency.cs b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/IScopedDependency.cs new file mode 100644 index 0000000..8d5bd50 --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/IScopedDependency.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection +{ + public interface IScopedDependency + { + } +} diff --git a/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ISingletonDependency.cs b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ISingletonDependency.cs new file mode 100644 index 0000000..411e68d --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ISingletonDependency.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection +{ + public interface ISingletonDependency + { + } +} diff --git a/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ITransientDependency.cs b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ITransientDependency.cs new file mode 100644 index 0000000..e43fe49 --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/DependencyInjection/ITransientDependency.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection +{ + public interface ITransientDependency + { + } +} diff --git a/JiShe.CollectBus.Protocol/Abstracts/BaseProtocolPlugin.cs b/JiShe.CollectBus.Protocol/Abstracts/BaseProtocolPlugin.cs deleted file mode 100644 index aec53c7..0000000 --- a/JiShe.CollectBus.Protocol/Abstracts/BaseProtocolPlugin.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; - -namespace JiShe.CollectBus.Protocol.Contracts.Abstracts -{ - public abstract class BaseProtocolPlugin:IProtocolPlugin - { - public abstract Models.Protocol Get(); - - public abstract void Received(); - - public abstract void Send(); - - public void Load() - { - - } - - } -} diff --git a/JiShe.CollectBus.Protocol/IStandardProtocolPlugin.cs b/JiShe.CollectBus.Protocol/IStandardProtocolPlugin.cs new file mode 100644 index 0000000..52a190e --- /dev/null +++ b/JiShe.CollectBus.Protocol/IStandardProtocolPlugin.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.Protocol +{ + public interface IStandardProtocolPlugin + { + } +} diff --git a/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj b/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj index 8fdf043..c71cea1 100644 --- a/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj +++ b/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + diff --git a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs index bf25b61..b4acb1a 100644 --- a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs +++ b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs @@ -1,27 +1,35 @@ -using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using JiShe.CollectBus.Protocol.Contracts.Abstracts; +using JiShe.CollectBus.Protocol.Contracts.DependencyInjection; +using Microsoft.Extensions.Caching.Distributed; namespace JiShe.CollectBus.Protocol { - public class StandardProtocolPlugin:IProtocolPlugin + public class StandardProtocolPlugin : BaseProtocolPlugin, IStandardProtocolPlugin,ISingletonDependency { - public Contracts.Models.Protocol Get() + public StandardProtocolPlugin(IDistributedCache cache) : base(cache) + { + } + + public override Contracts.Models.Protocol Get() { return new Contracts.Models.Protocol("Standard", "376.1", "TCP","376.1协议","DTS1980"); } public void Load() { - throw new NotImplementedException(); + base.Load(); } - public void Received() + public override void Received() { throw new NotImplementedException(); } - public void Send() + public override void Send() { throw new NotImplementedException(); } + + } }