mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 13:33:28 +08:00
refactor(ioc): 为依赖注入容器注册方法添加线程安全锁
- 在RegisterSingleton方法中添加读写锁保护 - 在RegisterFactory方法中添加读写锁保护 - 在ExecuteServicesHook方法中添加读写锁保护 - 确保在冻结状态下抛出异常 - 添加日志记录单例注册操作 - 统一异常处理和资源清理逻辑
This commit is contained in:
parent
ae8ad29806
commit
a420a41a55
@ -104,10 +104,22 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
/// <typeparam name="TService">服务接口或基类类型</typeparam>
|
/// <typeparam name="TService">服务接口或基类类型</typeparam>
|
||||||
/// <typeparam name="TImpl">具体的实现类型</typeparam>
|
/// <typeparam name="TImpl">具体的实现类型</typeparam>
|
||||||
public void RegisterSingleton<TService, TImpl>()
|
public void RegisterSingleton<TService, TImpl>()
|
||||||
where TImpl : class, TService where TService : class
|
where TImpl : class, TService
|
||||||
|
where TService : class
|
||||||
{
|
{
|
||||||
|
_lock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ThrowIfFrozen();
|
||||||
Services.AddSingleton<TService, TImpl>();
|
Services.AddSingleton<TService, TImpl>();
|
||||||
|
_logger.Debug($"Singleton registered: {typeof(TService).Name}");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册多个实例到其所有接口和具体类型
|
/// 注册多个实例到其所有接口和具体类型
|
||||||
@ -234,11 +246,21 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型</typeparam>
|
/// <typeparam name="TService">服务类型</typeparam>
|
||||||
/// <param name="factory">创建服务实例的工厂委托函数,接收IServiceProvider参数</param>
|
/// <param name="factory">创建服务实例的工厂委托函数,接收IServiceProvider参数</param>
|
||||||
public void RegisterFactory<TService>(Func<IServiceProvider, TService> factory) where TService : class
|
public void RegisterFactory<TService>(
|
||||||
|
Func<IServiceProvider, TService> factory) where TService : class
|
||||||
|
{
|
||||||
|
_lock.EnterWriteLock();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ThrowIfFrozen();
|
ThrowIfFrozen();
|
||||||
Services.AddSingleton(factory);
|
Services.AddSingleton(factory);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册中介行为管道
|
/// 注册中介行为管道
|
||||||
@ -269,10 +291,19 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
|
|||||||
/// 配置服务
|
/// 配置服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configurator">服务配置委托</param>
|
/// <param name="configurator">服务配置委托</param>
|
||||||
public void ExecuteServicesHook(Action<IServiceCollection>? configurator = null)
|
public void ExecuteServicesHook(Action<IServiceCollection>? configurator)
|
||||||
{
|
{
|
||||||
|
_lock.EnterWriteLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ThrowIfFrozen();
|
||||||
configurator?.Invoke(Services);
|
configurator?.Invoke(Services);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitWriteLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user