-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Options do not follow .Net standard pattern #339
Comments
Also add |
Hi @TibbsTerry , I tried reproducing your test locally, here it is: [Fact]
public void _AAA()
{
var duration = TimeSpan.FromMinutes(50);
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { { "DefaultDurationMinutes", duration.TotalMinutes.ToString() } })
.Build();
var services = new ServiceCollection();
services.AddSingleton<IConfiguration>(configuration);
services.AddFusionCache();
services.AddOptions<FusionCacheOptions>()
.Configure<IConfiguration>((options, config) =>
{
var durationMinutes = long.Parse(config["DefaultDurationMinutes"]!);
options.DefaultEntryOptions.Duration = TimeSpan.FromMinutes(durationMinutes);
});
var serviceProvider = services.BuildServiceProvider();
var fusionCache = serviceProvider.GetRequiredService<IFusionCache>();
Assert.Equal(duration, fusionCache.DefaultEntryOptions.Duration); // SUCCESS
} I did not change anything, except for adapting it to use xUnit:
that's all, and here is the result: Can you give me more info? Thanks. |
@jodydonetti |
Ah, I think I know why that is! Can you add more details about the options validation? Thanks! |
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-9.0#options-validation How OptionsFactory builds the options: |
Hi @TibbsTerry thanks for the links, I'll try to see what I can do for v2. |
Hi @TibbsTerry , getting back to this. I read the docs, and made some tests with the v2 preview. I created this: public class MyOptionsValidation
: IValidateOptions<FusionCacheOptions>
{
public ValidateOptionsResult Validate(string name, FusionCacheOptions options)
{
if (name == "Foo" && options.CacheName == "/")
return ValidateOptionsResult.Fail("Oh no!");
return ValidateOptionsResult.Success;
}
} Then this: var builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(services =>
{
services
.AddOptions<FusionCacheOptions>("Foo")
.Configure(options =>
{
options.CacheName = "/";
})
.ValidateOnStart();
services.AddSingleton<IValidateOptions<FusionCacheOptions>, MyOptionsValidation>();
});
var app = builder.Build();
app.Run(); This was the result: It seems to me that v2 is aligned with all the standard patterns, so I'll close this when v2 will be released. If you find anything else please let me know. Thanks! |
Hi all, I still can't believe it but v2.0.0 is finally out 🎉 |
@jodydonetti I'm not too sure that you followed what I meant. Fusion cache should have its own built in Validator (that implements |
Hi @TibbsTerry , thanks for the clarification. |
Problem
Configuration of options do not follow the standard .Net options pattern
Options cannot be set via configuration and have to be 'hard-coded'
Solution
Confguration/Options should follow standards for .NET
ie should use
Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure
Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache
https://github.com/dotnet/runtime/blob/a5af0ab77caa5ed7a6844fc5f4f459e5edfe23d3/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCacheServiceCollectionExtensions.cs#L93
is probably a good example of how to do it.
or (less preferable solution)
change
FusionCacheBuilder.SetupOptionsAction
to
Action<IServiceProvider, FusionCacheOptions>? SetupOptionsAction { get; set; }
The text was updated successfully, but these errors were encountered: