mirror of https://github.com/abpframework/abp.git
10 changed files with 65 additions and 9 deletions
@ -0,0 +1,22 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Options; |
||||
|
|
||||
|
namespace Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
public static class ServiceCollectionOptionsExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// You should only use this method to register options if you need to continue using the ServiceProvider to get other options in your Options configuration method.
|
||||
|
/// Otherwise, please use the default AddOptions method for better performance.
|
||||
|
/// </summary>
|
||||
|
/// <param name="services"></param>
|
||||
|
/// <typeparam name="TOptions"></typeparam>
|
||||
|
/// <returns></returns>
|
||||
|
public static OptionsBuilder<TOptions> AddAbpOptions<TOptions>(this IServiceCollection services) |
||||
|
where TOptions : class |
||||
|
{ |
||||
|
services.TryAddSingleton<IOptions<TOptions>, AbpUnnamedOptionsManager<TOptions>>(); |
||||
|
return services.AddOptions<TOptions>(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
namespace Volo.Abp.Options; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// This Options manager is similar to Microsoft UnnamedOptionsManager but without the locking mechanism.
|
||||
|
/// Prevent deadlocks when accessing options in multiple threads.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TOptions"></typeparam>
|
||||
|
public class AbpUnnamedOptionsManager<TOptions> : IOptions<TOptions> |
||||
|
where TOptions : class |
||||
|
{ |
||||
|
private readonly IOptionsFactory<TOptions> _factory; |
||||
|
private volatile TOptions? _value; |
||||
|
|
||||
|
public AbpUnnamedOptionsManager(IOptionsFactory<TOptions> factory) |
||||
|
{ |
||||
|
_factory = factory; |
||||
|
} |
||||
|
|
||||
|
public TOptions Value |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
if (_value is TOptions value) |
||||
|
{ |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
_value = _factory.Create(Microsoft.Extensions.Options.Options.DefaultName); |
||||
|
return _value; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue