From 2c78d143255efd6c4c99af277d8564b0c0b0886b Mon Sep 17 00:00:00 2001 From: mtai0524 Date: Fri, 22 Nov 2024 19:11:31 +0700 Subject: [PATCH] fix: scoped dbContext --- .../Notaion.Application/Services/ChatService.cs | 2 +- .../Notaion.Infrastructure/DependencyInjection.cs | 2 +- .../Repositories/UnitOfWork.cs | 1 - .../Notaion/Controllers/ChatController.cs | 15 +++++++++------ .../site8642-WebDeploy1.pubxml.user | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/NotaionWebApp/Notaion.Application/Services/ChatService.cs b/NotaionWebApp/Notaion.Application/Services/ChatService.cs index b9a0dfa..e597552 100644 --- a/NotaionWebApp/Notaion.Application/Services/ChatService.cs +++ b/NotaionWebApp/Notaion.Application/Services/ChatService.cs @@ -72,7 +72,7 @@ public async Task HideChatAllAsync() */ public async Task> GetChatsAsync() { - var chats = await _unitOfWork.ChatRepository.GetAllAsync(); + var chats = await _unitOfWork.ChatRepository.GetAsync(x => x.Hide == false); return _mapper.Map>(chats); } diff --git a/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs b/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs index b1f5768..593a278 100644 --- a/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs +++ b/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs @@ -27,7 +27,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi { options.UseSqlServer(connectionString); options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); - }, ServiceLifetime.Singleton, ServiceLifetime.Transient); + }, ServiceLifetime.Scoped); // chat services.AddScoped(); diff --git a/NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs b/NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs index 0691063..cc961e0 100644 --- a/NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs +++ b/NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs @@ -68,7 +68,6 @@ public async Task CommitTransactionAsync() } } - public void Dispose() { Dispose(true); diff --git a/NotaionWebApp/Notaion/Controllers/ChatController.cs b/NotaionWebApp/Notaion/Controllers/ChatController.cs index f21d35b..bd8c0be 100644 --- a/NotaionWebApp/Notaion/Controllers/ChatController.cs +++ b/NotaionWebApp/Notaion/Controllers/ChatController.cs @@ -11,6 +11,7 @@ using Notaion.Application.Services; using Notaion.Application.Interfaces.Services; using Microsoft.AspNetCore.Authorization; +using System; namespace Notaion.Controllers { @@ -28,12 +29,12 @@ public ChatController(ApplicationDbContext context, IHubContext hubCont this.chatService = chatService; } - [HttpGet("test-genaric-repo")] - public async Task GetChatWithGenaricRepo() - { - var chats = await chatService.GetChatsAsync(); - return Ok(chats); - } + //[HttpGet("test-genaric-repo")] + //public async Task GetChatWithGenaricRepo() + //{ + // var chats = await chatService.GetChatsAsync(); + // return Ok(chats); + //} //[Authorize] [HttpGet("get-chats")] @@ -60,6 +61,8 @@ public async Task AddChat([FromBody] CreateChatDto chatDto) { var createdChat = await this.chatService.CreateChatAsync(chatDto); + await _hubContext.Clients.All.SendAsync("ReceiveMessage", createdChat.UserName, createdChat.Content); + return Ok(createdChat); } catch (Exception ex) diff --git a/NotaionWebApp/Notaion/Properties/PublishProfiles/site8642-WebDeploy1.pubxml.user b/NotaionWebApp/Notaion/Properties/PublishProfiles/site8642-WebDeploy1.pubxml.user index cab8ff9..8cfc042 100644 --- a/NotaionWebApp/Notaion/Properties/PublishProfiles/site8642-WebDeploy1.pubxml.user +++ b/NotaionWebApp/Notaion/Properties/PublishProfiles/site8642-WebDeploy1.pubxml.user @@ -7,7 +7,7 @@ by editing this MSBuild file. In order to learn more about this please visit htt AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA45ACkv6Ev0Gy7utx8HyVkgAAAAACAAAAAAAQZgAAAAEAACAAAABSm0dADuKEoSXI2PM3/C+djjsZNaNJvaNktK2zzOh0ZQAAAAAOgAAAAAIAACAAAAAaubSe329ikteIzbdUehSGkeEFWcMirIFoj/tBJlG3PCAAAAAaOwJII3B0tuPaPOzYnGrfQAgIQnX5FDaCdhlVr8BQDEAAAADxwckrnekLCzuMBtAKkP1cCWMOulRqdqFNLxcPxe9HXD/Y0JACDx7eUDw/7grdO/SQQKIkbvxPSZZdbFnGYkiS - True|2024-11-18T01:56:26.6539288Z||;False|2024-11-18T08:54:38.8724697+07:00||;True|2024-11-17T23:02:34.2326203+07:00||;True|2024-11-17T22:58:09.9977191+07:00||;True|2024-11-14T11:37:13.0792751+07:00||;True|2024-10-28T19:07:16.1054653+07:00||;True|2024-10-08T17:12:03.4740526+07:00||; + True|2024-11-22T12:11:24.9616986Z||;True|2024-11-22T19:01:42.6927776+07:00||;True|2024-11-22T18:40:57.6369725+07:00||;True|2024-11-22T18:29:27.3414695+07:00||;True|2024-11-18T08:56:26.6539288+07:00||;False|2024-11-18T08:54:38.8724697+07:00||;True|2024-11-17T23:02:34.2326203+07:00||;True|2024-11-17T22:58:09.9977191+07:00||;True|2024-11-14T11:37:13.0792751+07:00||;True|2024-10-28T19:07:16.1054653+07:00||;True|2024-10-08T17:12:03.4740526+07:00||; \ No newline at end of file