修改代码

This commit is contained in:
Dai Mr 2024-10-26 22:27:19 +08:00
parent 16bd93cc11
commit 0b1532d4af
9 changed files with 39 additions and 23 deletions

View File

@ -23,8 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ISingletonDependency"));
foreach (var interfaceType in interfaceTypes)
{
var serviceDescriptor = new ServiceDescriptor(interfaceType, interfaceType.Name, type, ServiceLifetime.Singleton);
services.Add(serviceDescriptor);
services.AddSingleton(interfaceType, type);
}
}
}
@ -36,8 +35,7 @@ namespace Microsoft.Extensions.DependencyInjection
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("ITransientDependency"));
foreach (var interfaceType in interfaceTypes)
{
var serviceDescriptor = new ServiceDescriptor(interfaceType, interfaceType.Name, type, ServiceLifetime.Transient);
services.Add(serviceDescriptor);
services.AddTransient(interfaceType, type);
}
}
}
@ -49,8 +47,7 @@ namespace Microsoft.Extensions.DependencyInjection
var interfaceTypes = type.GetInterfaces().Where(p => p.FullName != null && !p.FullName.Contains("IScopedDependency"));
foreach (var interfaceType in interfaceTypes)
{
var serviceDescriptor = new ServiceDescriptor(interfaceType, interfaceType.Name, type, ServiceLifetime.Scoped);
services.Add(serviceDescriptor);
services.AddScoped(interfaceType, type);
}
}
}

View File

@ -7,6 +7,8 @@
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>

View File

@ -1,12 +1,24 @@
using JiShe.CollectBus.RabbitMQ.Models;
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
using JiShe.CollectBus.RabbitMQ.Models;
using JiShe.CollectBus.RabbitMQ.Senders;
using Microsoft.Extensions.DependencyInjection;
using System;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins
{
public partial class TcpServiceReceivedPlugin(IMqSender sender) : PluginBase
public partial class TcpServiceReceivedPlugin : PluginBase
{
private readonly IServiceProvider _serviceProvider;
private readonly IMqSender _mqSender;
public TcpServiceReceivedPlugin(IServiceProvider serviceProvider, IMqSender mqSender)
{
_serviceProvider = serviceProvider;
_mqSender = mqSender;
}
[GeneratorPlugin(typeof(ITcpReceivedPlugin))]
public async Task OnTcpReceived(ITcpSessionClient client, ReceivedDataEventArgs e)
{
@ -16,7 +28,7 @@ namespace JiShe.CollectBus.Core.Plugins
//const string protocolType = "StandardProtocol";
//var protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
//var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
//var protocolPluginInfo = await protocolPlugin.GetAsync();
//client.Logger.Info($"{protocolPluginInfo.Name},{protocolPluginInfo.RegularExpression}");
////从客户端收到信息
@ -25,8 +37,7 @@ namespace JiShe.CollectBus.Core.Plugins
//await protocolPlugin.ReceivedAsync(client,e);
await sender.SendToReportAsync(new ReportDto
await _mqSender.SendToReportAsync(new ReportDto
{
ClientIP = client.IP,
ClientId = client.Id,

View File

@ -3,7 +3,7 @@ using MassTransit;
namespace JiShe.CollectBus.RabbitMQ.Consumers
{
internal class ReportConsumer : IConsumer<ReportDto>
public class ReportConsumer : IConsumer<ReportDto>
{
public async Task Consume(ConsumeContext<ReportDto> context)
{

View File

@ -45,6 +45,7 @@ namespace Microsoft.Extensions.DependencyInjection
s.ExchangeType = ExchangeType.Direct;
});
});
//cfg.UseRawJsonSerializer();
});
});
services.AddMassTransitHostedService();

View File

@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace JiShe.CollectBus.RabbitMQ.Models

View File

@ -7,7 +7,7 @@ using JiShe.CollectBus.Common.Extensions.DependencyInjections;
namespace JiShe.CollectBus.RabbitMQ.Senders
{
public interface IMqSender : ISingletonDependency
public interface IMqSender
{
Task SendToPushAsync<T>(T message, CancellationToken cancellationToken = default) where T : class;
Task SendToPushAsync(object message, CancellationToken cancellationToken = default);

View File

@ -4,7 +4,7 @@ using Microsoft.Extensions.Configuration;
namespace JiShe.CollectBus.RabbitMQ.Senders
{
public class MqSender : IMqSender
public class MqSender : IMqSender, ISingletonDependency
{
private readonly ISendEndpointProvider _sendEndpointProvider;
private readonly IConfiguration _configuration;
@ -17,26 +17,29 @@ namespace JiShe.CollectBus.RabbitMQ.Senders
public async Task SendToPushAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
{
var queueName = _configuration["MQ:Queue:Push"];
await SendAsync(queueName, message, cancellationToken);
var queueKey = _configuration["MQ:Queue:Push"];
await SendAsync(queueKey, message, cancellationToken);
}
public async Task SendToPushAsync(object message, CancellationToken cancellationToken = default)
{
var queueName = _configuration["MQ:Queue:Push"];
await SendAsync(queueName, message, cancellationToken);
var queueKey = _configuration["MQ:Queue:Push"];
await SendAsync(queueKey, message, cancellationToken);
}
public async Task SendToReportAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
{
var queueName = _configuration["MQ:Queue:Report"];
await SendAsync(queueName, message, cancellationToken);
var queueKey = _configuration["MQ:Queue:Report"];
//await SendAsync(queueName, message, cancellationToken);
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{queueKey}"));
await endpoint.Send(message,typeof(T),cancellationToken);
}
public async Task SendToReportAsync(object message, CancellationToken cancellationToken = default)
{
var queueName = _configuration["MQ:Queue:Report"];
await SendAsync(queueName, message, cancellationToken);
var queueKey = _configuration["MQ:Queue:Report"];
await SendAsync(queueKey, message, cancellationToken);
}