修改代码
This commit is contained in:
parent
de30d29cc0
commit
8fe32bbf90
@ -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<Assembly> GetAssembliesFromFolder(string folderPath)
|
||||
{
|
||||
var directory = new DirectoryInfo(folderPath);
|
||||
var files = directory.GetFiles("*.dll");
|
||||
|
||||
var assemblies = new List<Assembly>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,16 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Plugins\ignore.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Plugins\ignore.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.8" />
|
||||
@ -15,6 +25,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj" />
|
||||
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -15,8 +15,4 @@
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JiShe.CollectBus.Protocol\JiShe.CollectBus.Protocol.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection
|
||||
{
|
||||
public interface IScopedDependency
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection
|
||||
{
|
||||
public interface ISingletonDependency
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace JiShe.CollectBus.Protocol.Contracts.DependencyInjection
|
||||
{
|
||||
public interface ITransientDependency
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
12
JiShe.CollectBus.Protocol/IStandardProtocolPlugin.cs
Normal file
12
JiShe.CollectBus.Protocol/IStandardProtocolPlugin.cs
Normal file
@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<BaseOutputPath></BaseOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user