diff --git a/JiShe.CollectBus.Application/Extensions/ServiceCollections/ServiceCollectionExtensions.cs b/JiShe.CollectBus.Application/Extensions/ServiceCollections/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..db6fa96 --- /dev/null +++ b/JiShe.CollectBus.Application/Extensions/ServiceCollections/ServiceCollectionExtensions.cs @@ -0,0 +1,30 @@ +using Hangfire; +using Microsoft.Extensions.Configuration; +using System.Reflection; +using JiShe.CollectBus.Common.Jobs; + +// ReSharper disable once CheckNamespace +namespace Microsoft.Extensions.DependencyInjection +{ + public static class ServiceCollectionExtensions + { + /// + /// 添加TcpService服务。 + /// + public static IServiceCollection AddJobs(this IServiceCollection services, IConfiguration configuration) + { + var interfaceType = typeof(IBaseJob); + var assembly = Assembly.GetExecutingAssembly(); + + var types = assembly.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && type.GetInterfaces().Contains(interfaceType)) + { + RecurringJob.AddOrUpdate(type.Name, ()=>((type as IBaseJob)!).Execute(), Cron.Minutely, TimeZoneInfo.Utc); + } + } + return services; + } + } +} diff --git a/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj b/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj new file mode 100644 index 0000000..c4b0c04 --- /dev/null +++ b/JiShe.CollectBus.Application/JiShe.CollectBus.Application.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/JiShe.CollectBus.Application/JiSheCollectBusApplicationModule.cs b/JiShe.CollectBus.Application/JiSheCollectBusApplicationModule.cs new file mode 100644 index 0000000..d482caa --- /dev/null +++ b/JiShe.CollectBus.Application/JiSheCollectBusApplicationModule.cs @@ -0,0 +1,15 @@ +using JiShe.CollectBus.Common.Interfaces; +using JiShe.CollectBus.Common.Models; +using Microsoft.Extensions.DependencyInjection; + +namespace JiShe.CollectBus.Application +{ + public class JiSheCollectBusApplicationModule: IJiSheModule + { + public void ConfigureServices(ServiceContext context) + { + var configuration = context.Configuration; + context.Services.AddJobs(configuration); + } + } +} diff --git a/JiShe.CollectBus.Application/Jobs/TestJob.cs b/JiShe.CollectBus.Application/Jobs/TestJob.cs new file mode 100644 index 0000000..a923ade --- /dev/null +++ b/JiShe.CollectBus.Application/Jobs/TestJob.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JiShe.CollectBus.Common.Jobs; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace JiShe.CollectBus.Application.Jobs +{ + public class TestJob : IBaseJob + { + private readonly ILogger _logger; + private readonly IConfiguration _configuration; + + public TestJob(ILogger logger, IConfiguration configuration) + { + _logger = logger; + _configuration = configuration; + } + + public async Task Execute() + { + //to do something + await Task.CompletedTask; + } + } +} diff --git a/JiShe.CollectBus.Common/Attributes/DependsOnAttribute.cs b/JiShe.CollectBus.Common/Attributes/DependsOnAttribute.cs new file mode 100644 index 0000000..db05c99 --- /dev/null +++ b/JiShe.CollectBus.Common/Attributes/DependsOnAttribute.cs @@ -0,0 +1,24 @@ +using JiShe.CollectBus.Common.Interfaces; + +namespace JiShe.CollectBus.Common.Attributes +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)] + public class DependsOnAttribute : Attribute + { + // 依赖的模块 + public Type ModuleType { get; private init; } + public DependsOnAttribute(Type type) + { + ModuleType = type; + } + } + + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)] + public sealed class DependsOnAttribute : DependsOnAttribute + where TModule : IJiSheModule + { + public DependsOnAttribute() : base(typeof(TModule)) + { + } + } +} diff --git a/JiShe.CollectBus.Common/Extensions/DependencyInjections/DependencyInjectionExtensions.cs b/JiShe.CollectBus.Common/Extensions/DependencyInjections/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..becb781 --- /dev/null +++ b/JiShe.CollectBus.Common/Extensions/DependencyInjections/DependencyInjectionExtensions.cs @@ -0,0 +1,104 @@ +using JiShe.CollectBus.Common.Extensions.DependencyInjections; +using System.Reflection; +using JiShe.CollectBus.Common.Interfaces; +using Microsoft.Extensions.Hosting; +using System; +using Microsoft.Extensions.Configuration; +using Serilog; + +// ReSharper disable once CheckNamespace +namespace Microsoft.Extensions.DependencyInjection +{ + public static class DependencyInjectionExtensions + { + public static IServiceCollection ModuleRegister(this IServiceCollection services, IConfiguration configuration) + { + var assemblies = GetBinAssemblies(); + + foreach (var assembly in assemblies) + { + var allTypes = assembly.GetTypes(); + foreach (var type in allTypes) + { + if (typeof(IJiSheModule).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false }) + { + Log.Logger.Information($"正在加载模块{type.Name}..."); + var instance = Activator.CreateInstance(type); + _ = (type.GetMethod("ConfigureServices")?.Invoke(instance, [services, configuration])); + } + } + } + + return services; + } + + public static IServiceCollection ServiceRegister(this IServiceCollection services) + { + var assemblies = GetBinAssemblies(); + + foreach (var assembly in assemblies) + { + var allTypes = assembly.GetTypes(); + foreach (var type in allTypes) + { + if (type is not { IsClass: true, IsAbstract: false }) continue; + if (typeof(ISingletonDependency).IsAssignableFrom(type)) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency") && !p.FullName.Contains("IDisposable")); + foreach (var interfaceType in interfaceTypes) + { + Log.Logger.Information($"正在IOC注入ISingletonDependency {type.Name}..."); + services.AddSingleton(interfaceType, type); + } + } + + if (typeof(ITransientDependency).IsAssignableFrom(type)) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency") && !p.FullName.Contains("IDisposable")); + foreach (var interfaceType in interfaceTypes) + { + Log.Logger.Information($"正在IOC注入ITransientDependency {type.Name}..."); + services.AddTransient(interfaceType, type); + } + } + + if (typeof(IScopedDependency).IsAssignableFrom(type)) + { + var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency") && !p.FullName.Contains("IDisposable")); + foreach (var interfaceType in interfaceTypes) + { + Log.Logger.Information($"正在IOC注入IScopedDependency {type.Name}..."); + services.AddScoped(interfaceType, type); + } + } + } + } + + return services; + } + + private static IEnumerable GetBinAssemblies() + { + var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); + if (!directory.Exists) return []; + + var files = directory.GetFiles("JiShe.CollectBus.*.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.Common/Interfaces/IJiSheModule.cs b/JiShe.CollectBus.Common/Interfaces/IJiSheModule.cs index 62d3cdb..95ccad3 100644 --- a/JiShe.CollectBus.Common/Interfaces/IJiSheModule.cs +++ b/JiShe.CollectBus.Common/Interfaces/IJiSheModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Configuration; +using JiShe.CollectBus.Common.Models; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -6,6 +7,10 @@ namespace JiShe.CollectBus.Common.Interfaces { public interface IJiSheModule { - void ConfigureServices(IServiceCollection services,HostBuilderContext hostContext); + /// + /// 模块中的依赖注入 + /// + /// 模块服务上下文 + void ConfigureServices(ServiceContext context); } } diff --git a/JiShe.CollectBus.Common/JiShe.CollectBus.Common.csproj b/JiShe.CollectBus.Common/JiShe.CollectBus.Common.csproj index 09985a2..e22466b 100644 --- a/JiShe.CollectBus.Common/JiShe.CollectBus.Common.csproj +++ b/JiShe.CollectBus.Common/JiShe.CollectBus.Common.csproj @@ -11,6 +11,7 @@ + diff --git a/JiShe.CollectBus.Common/Jobs/IBaseJob.cs b/JiShe.CollectBus.Common/Jobs/IBaseJob.cs new file mode 100644 index 0000000..abe2cc5 --- /dev/null +++ b/JiShe.CollectBus.Common/Jobs/IBaseJob.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.CollectBus.Common.Jobs +{ + public interface IBaseJob + { + Task Execute(); + } +} diff --git a/JiShe.CollectBus.Common/Models/ServiceContext.cs b/JiShe.CollectBus.Common/Models/ServiceContext.cs new file mode 100644 index 0000000..7258cfc --- /dev/null +++ b/JiShe.CollectBus.Common/Models/ServiceContext.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace JiShe.CollectBus.Common.Models +{ + public class ServiceContext + { + internal ServiceContext(IServiceCollection serviceCollection, IConfiguration configuration) + { + Services = serviceCollection; + Configuration = configuration; + } + + /// + /// 依赖注入服务 + /// + public IServiceCollection Services { get; } + + /// + /// 配置 + /// + public IConfiguration Configuration { get; } + } +} diff --git a/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs b/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs index 84e5fa5..ef6741f 100644 --- a/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs +++ b/JiShe.CollectBus.Console/Extensions/DependencyInjections/DependencyInjectionExtensions.cs @@ -12,68 +12,6 @@ namespace Microsoft.Extensions.DependencyInjection { public static class DependencyInjectionExtensions { - public static void ModuleRegister(this IServiceCollection services, HostBuilderContext hostContext) - { - var assemblies = GetBinAssemblies(); - - foreach (var assembly in assemblies) - { - var allTypes = assembly.GetTypes(); - foreach (var type in allTypes) - { - if (typeof(IJiSheModule).IsAssignableFrom(type) && type is { IsClass: true, IsAbstract: false }) - { - Log.Logger.Information($"正在加载模块{type.Name}..."); - var instance = Activator.CreateInstance(type); - _ = (type.GetMethod("ConfigureServices")?.Invoke(instance, [services, hostContext])); - } - } - } - } - - public static void ServiceRegister(this IServiceCollection services) - { - var assemblies = GetBinAssemblies(); - - foreach (var assembly in assemblies) - { - var allTypes = assembly.GetTypes(); - foreach (var type in allTypes) - { - if (type is not { IsClass: true, IsAbstract: false }) continue; - if (typeof(ISingletonDependency).IsAssignableFrom(type)) - { - var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency") && !p.FullName.Contains("IDisposable")); - foreach (var interfaceType in interfaceTypes) - { - Log.Logger.Information($"正在IOC注入ISingletonDependency {type.Name}..."); - services.AddSingleton(interfaceType, type); - } - } - - if (typeof(ITransientDependency).IsAssignableFrom(type)) - { - var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency") && !p.FullName.Contains("IDisposable")); - foreach (var interfaceType in interfaceTypes) - { - Log.Logger.Information($"正在IOC注入ITransientDependency {type.Name}..."); - services.AddTransient(interfaceType, type); - } - } - - if (typeof(IScopedDependency).IsAssignableFrom(type)) - { - var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency") && !p.FullName.Contains("IDisposable")); - foreach (var interfaceType in interfaceTypes) - { - Log.Logger.Information($"正在IOC注入IScopedDependency {type.Name}..."); - services.AddScoped(interfaceType, type); - } - } - } - } - } - public static void PluginServiceRegister(this IServiceCollection services, string pluginPath = "") { if (pluginPath.IsNullOrWhiteSpace()) @@ -152,29 +90,5 @@ namespace Microsoft.Extensions.DependencyInjection return assemblies; } - - private static IEnumerable GetBinAssemblies() - { - var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); - if (!directory.Exists) return []; - - var files = directory.GetFiles("JiShe.CollectBus.*.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/Extensions/ServiceCollections/ServiceCollectionExtensions.cs b/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs index fc6a0fb..60231f8 100644 --- a/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs +++ b/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using JiShe.CollectBus.Core.Plugins; using JiShe.CollectBus.Core.Services; +using JiShe.CollectBus.Protocol.Contracts.Adapters; using Microsoft.Extensions.Configuration; using TouchSocket.Core; using TouchSocket.Sockets; @@ -17,6 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddTcpService(config => { config.SetListenIPHosts(int.Parse(configuration["TCP:ClientPort"] ?? "10500")) + //.SetTcpDataHandlingAdapter(()=>new StandardFixedHeaderDataHandlingAdapter()) .ConfigurePlugins(a => { a.Add(); diff --git a/JiShe.CollectBus.Console/Program.cs b/JiShe.CollectBus.Console/Program.cs index c9a59f2..2c3b33a 100644 --- a/JiShe.CollectBus.Console/Program.cs +++ b/JiShe.CollectBus.Console/Program.cs @@ -52,9 +52,9 @@ namespace JiShe.CollectBus.Console lc.ReadFrom.Configuration(configuration) .ReadFrom.Services(context); }); - services.ServiceRegister(); - services.PluginServiceRegister(); - services.ModuleRegister(hostContext); + services.ServiceRegister() + .ModuleRegister(configuration) + .PluginServiceRegister(); services.AddTcp(configuration); //services.AddUdp(configuration); services.AddStackExchangeRedisCache(options => diff --git a/JiShe.CollectBus.Console/appsettings.json b/JiShe.CollectBus.Console/appsettings.json index 509fd46..bb33117 100644 --- a/JiShe.CollectBus.Console/appsettings.json +++ b/JiShe.CollectBus.Console/appsettings.json @@ -27,8 +27,7 @@ }, "ConnectionStrings": { - "Default": "Data Source=192.168.111.248;Port=3306;Database=JiSheCollectBus;uid=root;pwd=123456abcD;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true", - "ClickHouse": "host=localhost;port=8123;user=default;password=;database=default" + "Default": "Data Source=192.168.111.248;Port=3306;Database=JiSheCollectBus;uid=root;pwd=123456abcD;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" }, "MongoSettings": { "Connection": "mongodb://backups_admin:jishe_mongodb_backups@118.190.144.92:27037", diff --git a/JiShe.CollectBus.Host/Extensions/ServiceCollections/ServiceCollectionExtensions.cs b/JiShe.CollectBus.Host/Extensions/ServiceCollections/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..486c5c8 --- /dev/null +++ b/JiShe.CollectBus.Host/Extensions/ServiceCollections/ServiceCollectionExtensions.cs @@ -0,0 +1,16 @@ +using System.Reflection; +using Hangfire; +using JiShe.CollectBus.Protocol.Contracts.Adapters; +using Microsoft.Extensions.Configuration; +using TouchSocket.Core; +using TouchSocket.Sockets; + +// ReSharper disable once CheckNamespace +namespace Microsoft.Extensions.DependencyInjection +{ + public static class ServiceCollectionExtensions + { + + + } +} diff --git a/JiShe.CollectBus.Host/JiShe.CollectBus.Host.csproj b/JiShe.CollectBus.Host/JiShe.CollectBus.Host.csproj index 3d6716f..3d18fb4 100644 --- a/JiShe.CollectBus.Host/JiShe.CollectBus.Host.csproj +++ b/JiShe.CollectBus.Host/JiShe.CollectBus.Host.csproj @@ -7,21 +7,26 @@ + + + + + + - - - - - - + + + + + diff --git a/JiShe.CollectBus.Host/Startup.cs b/JiShe.CollectBus.Host/Startup.cs index ea6d1e4..8cdfb2f 100644 --- a/JiShe.CollectBus.Host/Startup.cs +++ b/JiShe.CollectBus.Host/Startup.cs @@ -1,13 +1,39 @@ -namespace JiShe.CollectBus.Host +using Hangfire; +using Hangfire.Dashboard.BasicAuthorization; +using Hangfire.HttpJob; +using Hangfire.MySql; + +namespace JiShe.CollectBus.Host { public class Startup(IConfiguration configuration) { public void ConfigureServices(IServiceCollection services) { services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); + services.AddHangfire(config => + { + config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings(); + + config.UseStorage( + new MySqlStorage( + configuration.GetConnectionString("Default"), + new MySqlStorageOptions + { + QueuePollInterval = TimeSpan.FromSeconds(15), //- 作业队列轮询间隔。默认值为15秒。 + JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 作业到期检查间隔(管理过期记录)。默认值为1小时。 + CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。 + PrepareSchemaIfNecessary = true, //- 如果设置为true,则创建数据库表。默认是true。 + DashboardJobListLimit = 50000, //- 仪表板作业列表限制。默认值为50000。 + TransactionTimeout = TimeSpan.FromMinutes(1), //- 交易超时。默认为1分钟。 + })); + config.UseHangfireHttpJob(); + }); + services.AddHangfireServer(); + services.ServiceRegister().ModuleRegister(configuration); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -26,10 +52,37 @@ app.UseAuthorization(); - //app.UseEndpoints(endpoints => - //{ - // endpoints.MapRazorPages(); - //}); + var dashboardOptions = new DashboardOptions(); + + if (env.IsProduction()) + { + dashboardOptions = new DashboardOptions + { + Authorization = new[] + { + new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions + { + SslRedirect = false, + RequireSsl = false, + LoginCaseSensitive = false, + Users = + [ + new BasicAuthAuthorizationUser + { + Login = "admin", + PasswordClear = "lixiao1980" + } + ] + }) + }, + }; + } + app.UseHangfireDashboard("/hangfire", dashboardOptions); + + app.UseEndpoints(endpoint => + { + endpoint.MapControllers(); + }); } } diff --git a/JiShe.CollectBus.Host/WeatherForecast.cs b/JiShe.CollectBus.Host/WeatherForecast.cs deleted file mode 100644 index 137fed8..0000000 --- a/JiShe.CollectBus.Host/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace JiShe.CollectBus.Host -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} diff --git a/JiShe.CollectBus.Host/appsettings.json b/JiShe.CollectBus.Host/appsettings.json index 10f68b8..0c7713a 100644 --- a/JiShe.CollectBus.Host/appsettings.json +++ b/JiShe.CollectBus.Host/appsettings.json @@ -1,9 +1,51 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": "Information", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + }, + "WriteTo": [ + { "Name": "Console" }, + { + "Name": "File", + "Args": { + "path": "Logs/log-.txt", + "rollingInterval": "Day" + //"rollOnFileSizeLimit": true, + //"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + + "Properties": { + "Application": "CollectBus", + "Environment": "Development" } }, - "AllowedHosts": "*" -} + + "ConnectionStrings": { + "Default": "Data Source=192.168.111.248;Port=3306;Database=JiSheCollectBus;uid=root;pwd=123456abcD;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" + }, + "MongoSettings": { + "Connection": "mongodb://backups_admin:jishe_mongodb_backups@118.190.144.92:27037", + "DatabaseName": "JiSheCollectBus" + }, + "RedisCache": { + "ConnectionString": "123456@qwer@localhost:6379", + "InstanceName": "CollectBus" + }, + "MQ": { + "Host": "118.190.144.92", + "Port": "5672", + "VirtualHost": "/", + "UserName": "collectbus", + "Password": "123456", + "Queue": { + "Received": "Received_Command", + "Issued": "Issued_Command" + } + } +} \ No newline at end of file diff --git a/JiShe.CollectBus.MongoDB/JiSheCollectBusMongoDBModule.cs b/JiShe.CollectBus.MongoDB/JiSheCollectBusMongoDBModule.cs index 6f36ac7..335fc4d 100644 --- a/JiShe.CollectBus.MongoDB/JiSheCollectBusMongoDBModule.cs +++ b/JiShe.CollectBus.MongoDB/JiSheCollectBusMongoDBModule.cs @@ -1,17 +1,16 @@ using JiShe.CollectBus.Common.Interfaces; +using JiShe.CollectBus.Common.Models; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using MongoDB.Driver; namespace JiShe.CollectBus.MongoDB { public class JiSheCollectBusMongoDbModule: IJiSheModule { - public void ConfigureServices(IServiceCollection services, HostBuilderContext hostContext) + public void ConfigureServices(ServiceContext context) { - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(typeof(IMongoRepository<>), typeof(MongoBaseRepository<>)); + context.Services.AddSingleton(); + context.Services.AddSingleton(); + context.Services.AddSingleton(typeof(IMongoRepository<>), typeof(MongoBaseRepository<>)); } } } diff --git a/JiShe.CollectBus.Protocol.Contracts/Interfaces/CustomFixedHeaderRequestInfo.cs b/JiShe.CollectBus.Protocol.Contracts/Interfaces/CustomFixedHeaderRequestInfo.cs index 377322d..14e38e5 100644 --- a/JiShe.CollectBus.Protocol.Contracts/Interfaces/CustomFixedHeaderRequestInfo.cs +++ b/JiShe.CollectBus.Protocol.Contracts/Interfaces/CustomFixedHeaderRequestInfo.cs @@ -41,12 +41,15 @@ namespace JiShe.CollectBus.Protocol.Contracts.Interfaces public bool OnParsingHeader(ReadOnlySpan header) { - throw new NotImplementedException(); + //throw new NotImplementedException(); + return true; } public bool OnParsingBody(ReadOnlySpan body) { - throw new NotImplementedException(); + //throw new NotImplementedException(); + return true; + } public int BodyLength { get; } diff --git a/JiShe.CollectBus.RabbitMQ/JiSheCollectBusRabbitMqModule.cs b/JiShe.CollectBus.RabbitMQ/JiSheCollectBusRabbitMqModule.cs index a98e9c9..7fb927b 100644 --- a/JiShe.CollectBus.RabbitMQ/JiSheCollectBusRabbitMqModule.cs +++ b/JiShe.CollectBus.RabbitMQ/JiSheCollectBusRabbitMqModule.cs @@ -1,6 +1,8 @@ using JiShe.CollectBus.Common.Interfaces; +using JiShe.CollectBus.Common.Models; using JiShe.CollectBus.RabbitMQ.Consumers; using MassTransit; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Data; @@ -9,10 +11,10 @@ namespace JiShe.CollectBus.RabbitMQ { public class JiSheCollectBusRabbitMqModule: IJiSheModule { - public void ConfigureServices(IServiceCollection services, HostBuilderContext hostContext) + public void ConfigureServices(ServiceContext context) { - var configuration = hostContext.Configuration; - services.AddMassTransit(x => + var configuration = context.Configuration; + context.Services.AddMassTransit(x => { x.AddConsumer(cfg => { @@ -27,11 +29,11 @@ namespace JiShe.CollectBus.RabbitMQ x.AddConsumer(); x.AddConsumer(); - x.AddConfigureEndpointsCallback((context, name, cfg) => + x.AddConfigureEndpointsCallback((c, name, cfg) => { cfg.UseDelayedRedelivery(r => r.Intervals(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(30))); cfg.UseMessageRetry(r => r.Immediate(5)); - cfg.UseInMemoryOutbox(context); + cfg.UseInMemoryOutbox(c); //cfg.UseMessageRetry(r => //{ @@ -40,7 +42,7 @@ namespace JiShe.CollectBus.RabbitMQ //}); }); - x.UsingRabbitMq((context, cfg) => + x.UsingRabbitMq((c, cfg) => { cfg.Host(configuration["MQ:Host"], ushort.Parse(configuration["MQ:Port"] ?? string.Empty), configuration["MQ:VirtualHost"], h => { @@ -52,28 +54,28 @@ namespace JiShe.CollectBus.RabbitMQ cfg.ReceiveEndpoint(configuration["MQ:Queue:Received"] ?? string.Empty,configurator => { configurator.ConfigureConsumeTopology = false; - configurator.Consumer(context); + configurator.Consumer(c); configurator.Durable = true; }); // 登录 cfg.ReceiveEndpoint($"{configuration["MQ:Queue:Received"]}_Login", configurator => { configurator.ConfigureConsumeTopology = false; - configurator.Consumer(context); + configurator.Consumer(c); configurator.Durable = true; }); // 心跳 cfg.ReceiveEndpoint($"{configuration["MQ:Queue:Received"]}_Heartbeat", configurator => { configurator.ConfigureConsumeTopology = false; - configurator.Consumer(context); + configurator.Consumer(c); configurator.Durable = true; }); // 消息下发队列 cfg.ReceiveEndpoint(configuration["MQ:Queue:Issued"] ?? string.Empty, configurator => { configurator.ConfigureConsumeTopology = false; - configurator.Consumer(context); + configurator.Consumer(c); configurator.Durable = true; }); }); diff --git a/JiShe.CollectBus.sln b/JiShe.CollectBus.sln index 3f55287..cebdea4 100644 --- a/JiShe.CollectBus.sln +++ b/JiShe.CollectBus.sln @@ -31,7 +31,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Host", "Ji EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4.demo", "4.demo", "{43E8E19B-555A-4633-9926-B4F3C01D972A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Test", "JiShe.CollectBus.Test\JiShe.CollectBus.Test.csproj", "{6ED66F52-B4A0-403E-AE89-8E9A679C0885}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Test", "JiShe.CollectBus.Test\JiShe.CollectBus.Test.csproj", "{6ED66F52-B4A0-403E-AE89-8E9A679C0885}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.Application", "JiShe.CollectBus.Application\JiShe.CollectBus.Application.csproj", "{2760AC98-D2FA-4074-8396-FAE10BE20A3C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -83,6 +85,10 @@ Global {6ED66F52-B4A0-403E-AE89-8E9A679C0885}.Debug|Any CPU.Build.0 = Debug|Any CPU {6ED66F52-B4A0-403E-AE89-8E9A679C0885}.Release|Any CPU.ActiveCfg = Release|Any CPU {6ED66F52-B4A0-403E-AE89-8E9A679C0885}.Release|Any CPU.Build.0 = Release|Any CPU + {2760AC98-D2FA-4074-8396-FAE10BE20A3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2760AC98-D2FA-4074-8396-FAE10BE20A3C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2760AC98-D2FA-4074-8396-FAE10BE20A3C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2760AC98-D2FA-4074-8396-FAE10BE20A3C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -99,6 +105,7 @@ Global {223DBDB1-6CD3-4D4E-8795-42550BC0A871} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5} {FFA010F6-F33A-4705-8A42-B0FA3B3D2131} = {B68027BA-BD9D-4110-A383-708B87BC425D} {6ED66F52-B4A0-403E-AE89-8E9A679C0885} = {3A04FB29-EA75-4499-BBF3-AF24C7D46A1D} + {2760AC98-D2FA-4074-8396-FAE10BE20A3C} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {33261859-9CD1-4A43-B181-AB75C247D1CD}