From c1c03a3f05f28fd8094e7f7c7b3a2b82d8862633 Mon Sep 17 00:00:00 2001
From: hang <87227557@qq.com>
Date: Sun, 13 Oct 2024 21:40:05 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9masstransit=20=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../IRaCIS.Core.Application.xml | 4 +-
.../NeedVerify/AddEntiryTriggerConsumer.cs | 26 --------
.../Consumer/NeedVerify/TestConsumer.cs | 59 ++++++++++++++++--
IRaCIS.Core.Application/TestService.cs | 62 +------------------
4 files changed, 58 insertions(+), 93 deletions(-)
delete mode 100644 IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddEntiryTriggerConsumer.cs
diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
index dd3e6535f..915e46add 100644
--- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
+++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
@@ -12808,7 +12808,7 @@
-
+
meditor send 的时候,请求流会先到消费者,返回后才会执行后续代码
publish 请求流不会先到消费者,发布后,直接执行后续代码
@@ -12816,7 +12816,7 @@
-
+
meditor send 的时候,请求流会先到消费者,返回后才会执行后续代码
publish 请求流不会先到消费者,发布后,直接执行后续代码
diff --git a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddEntiryTriggerConsumer.cs b/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddEntiryTriggerConsumer.cs
deleted file mode 100644
index 899759d74..000000000
--- a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/AddEntiryTriggerConsumer.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-using AutoMapper;
-using IP2Region.Net.Abstractions;
-using IRaCIS.Core.Domain;
-using IRaCIS.Core.Domain.Models;
-using MassTransit;
-
-namespace IRaCIS.Core.Application.MassTransit.Consumer;
-
-
-//国家|区域|省份|城市|ISP 缺省的地域信息默认是0
-//0|0|0|内网IP|内网IP
-// 中国|0|湖北省|武汉市|电信
-public class AddUserLogTriggerConsumer(ISearcher _searcher) : IConsumer
-{
- public async Task Consume(ConsumeContext context)
- {
- var userLog = context.Message.UserLog;
-
- var ipinfo = _searcher.Search(userLog.IP);
-
- userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
-
- }
-}
diff --git a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/TestConsumer.cs b/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/TestConsumer.cs
index 133078b51..c54ad7e31 100644
--- a/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/TestConsumer.cs
+++ b/IRaCIS.Core.Application/MassTransit/Consumer/NeedVerify/TestConsumer.cs
@@ -1,6 +1,12 @@
-using MassTransit;
+using IRaCIS.Core.Domain;
+using MassTransit;
+using MassTransit.Mediator;
+using Medallion.Threading;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -8,24 +14,69 @@ using static IRaCIS.Core.Application.Service.TestService;
namespace IRaCIS.Core.Application.MassTransit.Consumer;
+
+public class MasstransiTestCommand
+{
+ public string value { get; set; }
+}
+
///
/// meditor send 的时候,请求流会先到消费者,返回后才会执行后续代码
/// publish 请求流不会先到消费者,发布后,直接执行后续代码
///
///
///
-public class MasstransitHangfireTestConsumer(IRepository _userRepository) : IConsumer
+public class MasstransitTestConsumer(IRepository _userRepository) : IConsumer
{
- public async Task Consume(ConsumeContext context)
+ public async Task Consume(ConsumeContext context)
{
Console.WriteLine(_userRepository._dbContext.GetHashCode());
Console.WriteLine("Now is " + DateTime.Now.ToString());
Console.WriteLine($"MassTransit.Consumer :{context.Message.value}");
-
await context.RespondAsync(ResponseOutput.Ok());
}
}
+[ApiExplorerSettings(GroupName = "Institution")]
+public class TestMasstransitService : BaseService
+{
+
+
+ public async Task TestMasstransitRequest([FromServices] IMessageScheduler _scheduler,
+ [FromServices] IRepository _testLengthRepository,
+ [FromServices] IRequestClient _requestClient,
+ [FromServices] IScopedClientFactory _clientFactory,
+ [FromServices] IScopedMediator _mediatorScoped,
+ [FromServices] IMediator _mediator)
+ {
+ var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
+
+ Console.WriteLine(_testLengthRepository._dbContext.GetHashCode());
+
+ //IScopedMediator 上下文一致, IMediator上下文不一致
+
+ //通过命令不获取结果 进入消费者后再返回 数据库上下文 不同
+ await _mediator.Send(new MasstransiTestCommand { value = "message at " + DateTime.Now.ToString() });
+
+ //通过命令获取结果 进入消费者后再返回 数据库上下文 相同
+ var dd = await _mediatorScoped.CreateRequest(new MasstransiTestCommand() { value = "message at " + DateTime.Now.ToString() })
+ .GetResponse();
+
+ //发布后,不会立即进入消费者,消费者是另外的线程执行
+ await _mediatorScoped.Publish(new MasstransiTestCommand { value = "message at " + DateTime.Now.ToString() });
+
+
+ //await _scheduler.SchedulePublish(DateTime.Now.AddSeconds(10), new MasstransitHangfireTest() { value = "message at " + DateTime.Now.ToString() });
+
+ return ResponseOutput.Ok();
+ }
+}
+
+
+
+
+
+
diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs
index ea508a0b4..d0d5e4cde 100644
--- a/IRaCIS.Core.Application/TestService.cs
+++ b/IRaCIS.Core.Application/TestService.cs
@@ -126,67 +126,7 @@ namespace IRaCIS.Core.Application.Service
}
- //My project is a monolithic project,And the efcore context repository is scoped registered.
- public async Task TestMasstransitMeditor(
- [FromServices] IScopedMediator _mediatorScoped,
- [FromServices] IMediator _mediator,
- [FromServices] IRepository _testLengthRepository
- )
- {
-
- var dbContext = _testLengthRepository._dbContext;
-
-
- var dbContext2 = _trialRepository._dbContext;
-
- if (ReferenceEquals(dbContext, dbContext2))
- {
- Console.WriteLine("两个 DbContext 是同一个实例");
-
- Console.WriteLine(dbContext.GetHashCode());
- Console.WriteLine(dbContext2.GetHashCode());
- }
-
-
- // add 1 recored
- await _testLengthRepository.AddAsync(new TestLength() { Name = "xxxx" });
-
- // The consumer method will inject the repository and add 3 pieces of data, but the savechanges method of the repository will not be called
- await _mediatorScoped.Publish(new AddSubjectTriggerCommand { SubjectId = Guid.Empty });
-
- await _mediator.Send(new AddSubjectTriggerCommand2 { SubjectId = Guid.Empty });
-
- // this will save 1 record not 4 record ,Why is the dbcontext different? Can it be in the same transaction?
- await _testLengthRepository.SaveChangesAsync();
-
- return ResponseOutput.Ok();
- }
-
- public class MasstransitHangfireTest
- {
- public string value { get; set; }
- }
-
- public async Task TestMasstransitHangfireOrRequest([FromServices] IMessageScheduler _scheduler,
- [FromServices] IRepository _testLengthRepository,
- [FromServices] IRequestClient _requestClient)
- {
- var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
-
- var aa = CultureInfo.CurrentCulture.Name;
-
- CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US);
-
- var bb = CultureInfo.CurrentCulture.Name;
- var isEn_US2 = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
-
- //await _scheduler.SchedulePublish(DateTime.Now.AddSeconds(10), new MasstransitHangfireTest() { value = "message at " + DateTime.Now.ToString() });
-
- Console.WriteLine(_testLengthRepository._dbContext.GetHashCode());
- await _requestClient.GetResponse(new MasstransitHangfireTest() { value = "message at " + DateTime.Now.ToString() });
-
- return ResponseOutput.Ok();
- }
+
public async Task TestJson()
{