修改代码

This commit is contained in:
cli 2024-10-08 14:41:41 +08:00
parent 8a50a5eb81
commit e45ada07a8
16 changed files with 169 additions and 80 deletions

View File

@ -1,10 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using JiShe.CollectBus.Protocol.Contracts.Attributes;
using JiShe.CollectBus.Protocol.Contracts.DependencyInjection;
using TouchSocket.Core;
@ -24,37 +19,45 @@ namespace Microsoft.Extensions.DependencyInjection
var allTypes = assembly.GetTypes();
foreach (var type in allTypes)
{
if (typeof(ITransientDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
if (typeof(ITransientDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
{
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency"));
foreach (var interfaceType in interfaceTypes)
{
services.AddTransient(interfaceType, type);
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
if (attr == null) continue;
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Transient);
services.Add(serviceDescriptor);
}
}
}
foreach (var type in allTypes)
{
if (typeof(ISingletonDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
if (typeof(ISingletonDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
{
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency"));
foreach (var interfaceType in interfaceTypes)
{
services.AddSingleton(interfaceType, type);
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
if (attr == null) continue;
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Singleton);
services.Add(serviceDescriptor);
}
}
}
foreach (var type in allTypes)
{
if (typeof(IScopedDependency).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
if (typeof(IScopedDependency).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false })
{
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency"));
foreach (var interfaceType in interfaceTypes)
{
services.AddScoped(interfaceType, type);
var attr = type.GetCustomAttribute<ProtocolNameAttribute>();
if (attr == null) continue;
var serviceDescriptor = new ServiceDescriptor(interfaceType, attr.Name, type, ServiceLifetime.Scoped);
services.Add(serviceDescriptor);
}
}
}
@ -71,7 +74,7 @@ namespace Microsoft.Extensions.DependencyInjection
ServiceRegister(services, assemblies.ToArray());
}
public static IEnumerable<Assembly> GetAssembliesFromFolder(string folderPath)
private static IEnumerable<Assembly> GetAssembliesFromFolder(string folderPath)
{
var directory = new DirectoryInfo(folderPath);
if (!directory.Exists) return Enumerable.Empty<Assembly>();

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JiShe.CollectBus.Core.Plugins;
using JiShe.CollectBus.Core.Plugins;
using JiShe.CollectBus.Core.Services;
using Microsoft.Extensions.Configuration;
using TouchSocket.Core;
using TouchSocket.Sockets;
@ -15,11 +11,11 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary>
/// 添加TcpService服务。
/// </summary>
public static IServiceCollection AddTcp(this IServiceCollection services)
public static IServiceCollection AddTcp(this IServiceCollection services, IConfiguration configuration)
{
services.AddTcpService(config =>
{
config.SetListenIPHosts(10500)
config.SetListenIPHosts(int.Parse(configuration["TCP:Port"] ?? "10500"))
.ConfigureContainer(a => //容器的配置顺序应该在最前面
{
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用
@ -46,11 +42,11 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary>
/// 添加UdpService服务。
/// </summary>
public static IServiceCollection AddUdp(this IServiceCollection services)
public static IServiceCollection AddUdp(this IServiceCollection services, IConfiguration configuration)
{
services.AddUdpSession(config =>
{
config.SetBindIPHost(10500)
config.SetBindIPHost(int.Parse(configuration["UDP:Port"] ?? "10500"))
.ConfigureContainer(a => //容器的配置顺序应该在最前面
{
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用

View File

@ -7,6 +7,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<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" />
@ -18,4 +28,13 @@
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Plugins\JiShe.CollectBus.Protocol.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Plugins\JiShe.CollectBus.Protocol.Test.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -31,8 +31,8 @@ namespace JiShe.CollectBus.Console
var configuration = hostContext.Configuration;
services.ServiceRegister();
services.PluginServiceRegister();
services.AddTcp();
services.AddUdp();
services.AddTcp(configuration);
services.AddUdp(configuration);
services.AddStackExchangeRedisCache(options =>
{

View File

@ -1,4 +1,10 @@
{
"TCP": {
"Port": 10500
},
"UDP": {
"Port": 10500
},
"RedisCache": {
"ConnectionString": "123456@qwer@localhost:6379",
"InstanceName": "CollectBus"

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
using JiShe.CollectBus.Protocol.Contracts.Models;
using Microsoft.Extensions.Caching.Distributed;
namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
@ -15,7 +14,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
_cache = cache;
}
public abstract Models.Protocol Get();
public abstract ProtocolInfo Get();
public abstract void Received();

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JiShe.CollectBus.Protocol.Contracts.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class ProtocolNameAttribute: Attribute
{
public ProtocolNameAttribute(string name)
{
Name = name;
}
public string Name { get; set; }
}
}

View File

@ -4,7 +4,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Interfaces
{
public interface IProtocolPlugin
{
Models.Protocol Get();
ProtocolInfo Get();
void Load();

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JiShe.CollectBus.Protocol.Contracts.Models
namespace JiShe.CollectBus.Protocol.Contracts.Models
{
public interface IProtocol
public interface IProtocolInfo
{
/// <summary>
/// 协议名称

View File

@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JiShe.CollectBus.Protocol.Contracts.Models
namespace JiShe.CollectBus.Protocol.Contracts.Models
{
public class Protocol:IProtocol
public class ProtocolInfo : IProtocolInfo
{
public Protocol(string name, string baseProtocol, string type, string description, string regularExpression)
public ProtocolInfo(string name, string baseProtocol, string type, string description, string regularExpression)
{
Name = name;
Type = type;

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy $(TargetDir)JiShe.CollectBus.Protocol.Test.dll $(ProjectDir)..\JiShe.CollectBus.Console\Plugins\" />
</Target>
</Project>

View File

@ -0,0 +1,27 @@
using JiShe.CollectBus.Protocol.Contracts.Abstracts;
using JiShe.CollectBus.Protocol.Contracts.Attributes;
using JiShe.CollectBus.Protocol.Contracts.DependencyInjection;
using JiShe.CollectBus.Protocol.Contracts.Models;
using Microsoft.Extensions.Caching.Distributed;
namespace JiShe.CollectBus.Protocol.Test
{
[ProtocolName("StandardProtocol")]
public class TestProtocolPlugin(IDistributedCache cache) : BaseProtocolPlugin(cache), ISingletonDependency
{
public override ProtocolInfo Get()
{
return new ProtocolInfo("Test", "376.1", "TCP", "376.1协议", "DTSU1980");
}
public override void Received()
{
throw new NotImplementedException();
}
public override void Send()
{
throw new NotImplementedException();
}
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiShe.CollectBus.Protocol
{
public interface IStandardProtocolPlugin
{
}
}

View File

@ -7,8 +7,16 @@
<BaseOutputPath></BaseOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy $(TargetDir)JiShe.CollectBus.Protocol.dll $(ProjectDir)..\JiShe.CollectBus.Console\Plugins\" />
</Target>
</Project>

View File

@ -1,18 +1,17 @@
using JiShe.CollectBus.Protocol.Contracts.Abstracts;
using JiShe.CollectBus.Protocol.Contracts.Attributes;
using JiShe.CollectBus.Protocol.Contracts.DependencyInjection;
using JiShe.CollectBus.Protocol.Contracts.Models;
using Microsoft.Extensions.Caching.Distributed;
namespace JiShe.CollectBus.Protocol
{
public class StandardProtocolPlugin : BaseProtocolPlugin, IStandardProtocolPlugin,ISingletonDependency
[ProtocolName("StandardProtocol")]
public class StandardProtocolPlugin(IDistributedCache cache) : BaseProtocolPlugin(cache), ISingletonDependency
{
public StandardProtocolPlugin(IDistributedCache cache) : base(cache)
public override ProtocolInfo Get()
{
}
public override Contracts.Models.Protocol Get()
{
return new Contracts.Models.Protocol("Standard", "376.1", "TCP","376.1协议","DTS1980");
return new ProtocolInfo("Standard", "376.1", "TCP","376.1协议","DTS1980");
}
public void Load()

View File

@ -3,15 +3,21 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Core", "JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj", "{F1360C93-5B6B-4E65-9D81-1DA38740F32D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Core", "JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj", "{F1360C93-5B6B-4E65-9D81-1DA38740F32D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Console", "JiShe.CollectBus.Console\JiShe.CollectBus.Console.csproj", "{40C4F834-3080-451B-9510-6FE7BC4F801F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Console", "JiShe.CollectBus.Console\JiShe.CollectBus.Console.csproj", "{40C4F834-3080-451B-9510-6FE7BC4F801F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Protocol", "JiShe.CollectBus.Protocol\JiShe.CollectBus.Protocol.csproj", "{B2C476F1-AE32-419D-BDA2-291FCE639CF6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Protocol", "JiShe.CollectBus.Protocol\JiShe.CollectBus.Protocol.csproj", "{B2C476F1-AE32-419D-BDA2-291FCE639CF6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Protocol.Contracts", "JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj", "{4468B52D-3AAE-4918-B4D6-E6E8F000825D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Protocol.Contracts", "JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj", "{4468B52D-3AAE-4918-B4D6-E6E8F000825D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Common", "JiShe.CollectBus.Common\JiShe.CollectBus.Common.csproj", "{1D3A5A4E-B977-4E33-A1AF-62508110C3B7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Common", "JiShe.CollectBus.Common\JiShe.CollectBus.Common.csproj", "{1D3A5A4E-B977-4E33-A1AF-62508110C3B7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{3A04FB29-EA75-4499-BBF3-AF24C7D46A1D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Protocol.Test", "JiShe.CollectBus.Protocol.Test\JiShe.CollectBus.Protocol.Test.csproj", "{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,10 +45,22 @@ Global
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7}.Release|Any CPU.Build.0 = Release|Any CPU
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F1360C93-5B6B-4E65-9D81-1DA38740F32D} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{40C4F834-3080-451B-9510-6FE7BC4F801F} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{B2C476F1-AE32-419D-BDA2-291FCE639CF6} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{4468B52D-3AAE-4918-B4D6-E6E8F000825D} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED} = {3A04FB29-EA75-4499-BBF3-AF24C7D46A1D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {33261859-9CD1-4A43-B181-AB75C247D1CD}
EndGlobalSection