From b95cc97f553b064e892ca3abe0bcc0e43272034d Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Sun, 4 Jan 2026 12:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=94=9F=E5=91=BD?= =?UTF-8?q?=E5=91=A8=E6=9C=9F=E6=9D=A1=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/UploadDownLoadController.cs | 80 +++++++++++++++---- .../IRaCIS.Core.Application.xml | 1 + 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs index b53f43913..7a2371bb3 100644 --- a/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs +++ b/IRaCIS.Core.API/Controllers/UploadDownLoadController.cs @@ -32,6 +32,7 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.WebUtilities; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Net.Http.Headers; @@ -940,17 +941,24 @@ namespace IRaCIS.Core.API.Controllers [HttpGet("download/GetPatientStudyBatchDownload")] - public async Task GetDownloadPatientStudyBatch([FromServices] IPatientService _patientService, [FromServices] IOSSService _oSSService, + public async Task GetDownloadPatientStudyBatch( [FromServices] IOSSService _oSSService, [FromServices] IHubContext _downLoadHub, + //[FromServices] IPatientService _patientService, + [FromServices] IServiceScopeFactory _serviceScopeFactory, [FromQuery] PatientImageDownloadCommand inCommand) { inCommand.SCPStudyIdList = inCommand.SCPStudyIdList.Where(t => t != Guid.Empty).ToList(); - var rusult = await _patientService.GetDownloadPatientStudyInfo(inCommand); - - var patientList = rusult.Data; - var downloadInfo = (SubejctVisitDownload)rusult.OtherData; - - + // 1. 获取患者研究数据(使用独立Scope) + var (patientList, downloadInfo) = await Task.Run(async () => + { + using (var scope = _serviceScopeFactory.CreateScope()) + { + var patientService = scope.ServiceProvider.GetRequiredService(); + var result = await patientService.GetDownloadPatientStudyInfo(inCommand); + return (result.Data, (SubejctVisitDownload)result.OtherData); + } + }); + long receivedSize = 0; long receivedCount = 0; long totalSize = downloadInfo.ImageSize; @@ -1172,7 +1180,15 @@ namespace IRaCIS.Core.API.Controllers // 正常完成 await NotifyProgressAsync(true); - await _patientService.DownloadImageSuccess(downloadInfo.Id); + // 3. 成功后调用Service,也使用独立Scope + await Task.Run(async () => + { + using (var scope = _serviceScopeFactory.CreateScope()) + { + var patientService = scope.ServiceProvider.GetRequiredService(); + await patientService.DownloadImageSuccess(downloadInfo.Id); + } + }); } catch (OperationCanceledException) { @@ -1867,17 +1883,42 @@ namespace IRaCIS.Core.API.Controllers [HttpGet("download/VisitImageDownload")] - public async Task VisitImageDownload([FromServices] IPatientService _patientService, [FromServices] IOSSService _oSSService, + public async Task VisitImageDownload( [FromServices] IOSSService _oSSService, [FromServices] IHubContext _downLoadHub, - [FromServices] IRepository _trialRepository, + //[FromServices] IRepository _trialRepository, + //[FromServices] IPatientService _patientService, + [FromServices] IServiceScopeFactory _serviceScopeFactory, // 注入ServiceScopeFactory [FromQuery] VisitImageDownloadCommand inCommand) { - var rusult = await _patientService.GetDownloadSubjectVisitStudyInfo(inCommand); - var visitList = rusult.Data; - var downloadInfo = (SubejctVisitDownload)rusult.OtherData; + // 1. 使用独立Scope获取Patient数据 + var (visitList, downloadInfo) = await Task.Run(async () => + { + using (var scope = _serviceScopeFactory.CreateScope()) + { + var patientService = scope.ServiceProvider.GetRequiredService(); + var result = await patientService.GetDownloadSubjectVisitStudyInfo(inCommand); + return (result.Data, (SubejctVisitDownload)result.OtherData); + } + }); - var trialInfo = _trialRepository.Where(t => t.Id == inCommand.TrialId).Select(t => new { t.TrialCode, t.ResearchProgramNo }).FirstOrDefault(); + var trialInfo = await Task.Run(async () => + { + using (var scope = _serviceScopeFactory.CreateScope()) + { + var trialRepository = scope.ServiceProvider.GetRequiredService>(); + return await trialRepository.Where(t => t.Id == inCommand.TrialId) + .Select(t => new { t.TrialCode, t.ResearchProgramNo }) + .FirstOrDefaultAsync(); + } + }); + + //var rusult = await _patientService.GetDownloadSubjectVisitStudyInfo(inCommand); + + //List visitList = rusult.Data; + //SubejctVisitDownload downloadInfo = (SubejctVisitDownload)rusult.OtherData; + + //var trialInfo = _trialRepository.Where(t => t.Id == inCommand.TrialId).Select(t => new { t.TrialCode, t.ResearchProgramNo }).FirstOrDefault(); var trialZipName = $"{trialInfo.TrialCode}_{trialInfo.ResearchProgramNo}_Image_"; @@ -2083,7 +2124,16 @@ namespace IRaCIS.Core.API.Controllers // 正常完成 await NotifyProgressAsync(true); - await _patientService.DownloadImageSuccess(downloadInfo.Id); + + // 3. 成功后调用Service,也使用独立Scope + await Task.Run(async () => + { + using (var scope = _serviceScopeFactory.CreateScope()) + { + var patientService = scope.ServiceProvider.GetRequiredService(); + await patientService.DownloadImageSuccess(downloadInfo.Id); + } + }); } catch (OperationCanceledException) { diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 24814e0e3..29cd78986 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -14053,6 +14053,7 @@ + 清理一致性分析任务