You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.3 KiB
131 lines
4.3 KiB
|
4 months ago
|
using CoreAgent.Domain.Interfaces.Common;
|
||
|
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
|
||
|
|
namespace CoreAgent.Infrastructure.Services.Common;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 服务作用域管理器实现
|
||
|
|
/// 用于在单例服务中创建瞬时服务
|
||
|
|
/// </summary>
|
||
|
|
public class ServiceScopeManager : IServiceScopeManager
|
||
|
|
{
|
||
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
||
|
|
private readonly ILogger<ServiceScopeManager> _logger;
|
||
|
|
|
||
|
|
public ServiceScopeManager(
|
||
|
|
IServiceScopeFactory scopeFactory,
|
||
|
|
ILogger<ServiceScopeManager> logger)
|
||
|
|
{
|
||
|
|
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
|
||
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 在作用域中执行异步操作
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">返回类型</typeparam>
|
||
|
|
/// <param name="operation">要执行的操作</param>
|
||
|
|
/// <returns>操作结果</returns>
|
||
|
|
public async Task<T> ExecuteInScopeAsync<T>(Func<IServiceProvider, Task<T>> operation)
|
||
|
|
{
|
||
|
|
if (operation == null)
|
||
|
|
throw new ArgumentNullException(nameof(operation));
|
||
|
|
|
||
|
|
using var scope = _scopeFactory.CreateScope();
|
||
|
|
var serviceProvider = scope.ServiceProvider;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_logger.LogDebug("开始在执行作用域中执行异步操作");
|
||
|
|
var result = await operation(serviceProvider);
|
||
|
|
_logger.LogDebug("在执行作用域中执行异步操作完成");
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "在执行作用域中执行异步操作时发生异常");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 在作用域中执行异步操作(无返回值)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="operation">要执行的操作</param>
|
||
|
|
/// <returns>任务</returns>
|
||
|
|
public async Task ExecuteInScopeAsync(Func<IServiceProvider, Task> operation)
|
||
|
|
{
|
||
|
|
if (operation == null)
|
||
|
|
throw new ArgumentNullException(nameof(operation));
|
||
|
|
|
||
|
|
using var scope = _scopeFactory.CreateScope();
|
||
|
|
var serviceProvider = scope.ServiceProvider;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_logger.LogDebug("开始在执行作用域中执行异步操作");
|
||
|
|
await operation(serviceProvider);
|
||
|
|
_logger.LogDebug("在执行作用域中执行异步操作完成");
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "在执行作用域中执行异步操作时发生异常");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 在作用域中执行同步操作
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T">返回类型</typeparam>
|
||
|
|
/// <param name="operation">要执行的操作</param>
|
||
|
|
/// <returns>操作结果</returns>
|
||
|
|
public T ExecuteInScope<T>(Func<IServiceProvider, T> operation)
|
||
|
|
{
|
||
|
|
if (operation == null)
|
||
|
|
throw new ArgumentNullException(nameof(operation));
|
||
|
|
|
||
|
|
using var scope = _scopeFactory.CreateScope();
|
||
|
|
var serviceProvider = scope.ServiceProvider;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_logger.LogDebug("开始在执行作用域中执行同步操作");
|
||
|
|
var result = operation(serviceProvider);
|
||
|
|
_logger.LogDebug("在执行作用域中执行同步操作完成");
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "在执行作用域中执行同步操作时发生异常");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 在作用域中执行同步操作(无返回值)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="operation">要执行的操作</param>
|
||
|
|
public void ExecuteInScope(Action<IServiceProvider> operation)
|
||
|
|
{
|
||
|
|
if (operation == null)
|
||
|
|
throw new ArgumentNullException(nameof(operation));
|
||
|
|
|
||
|
|
using var scope = _scopeFactory.CreateScope();
|
||
|
|
var serviceProvider = scope.ServiceProvider;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_logger.LogDebug("开始在执行作用域中执行同步操作");
|
||
|
|
operation(serviceProvider);
|
||
|
|
_logger.LogDebug("在执行作用域中执行同步操作完成");
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "在执行作用域中执行同步操作时发生异常");
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|