Skip to content

Commit

Permalink
fix(configuration): fix broken configuration for .net8 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlautier authored Feb 14, 2024
1 parent 11ae4cc commit 5915eec
Showing 1 changed file with 52 additions and 51 deletions.
103 changes: 52 additions & 51 deletions Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
#nullable enable
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
Expand All @@ -9,8 +9,7 @@
using Orleans.Persistence.Redis.Config;
using Orleans.Persistence.Redis.Core;
using Orleans.Persistence.Redis.Serialization;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;
using Orleans.Serialization;
using Orleans.Storage;
using JsonSerializer = Orleans.Persistence.Redis.Serialization.JsonSerializer;
Expand All @@ -21,10 +20,8 @@ namespace Orleans.Hosting;

public static class RedisSiloBuilderExtensions
{
public static RedisStorageOptionsBuilder AddRedisGrainStorage(
this ISiloBuilder builder,
string name
) => new RedisStorageOptionsBuilder(builder, name);
public static RedisStorageOptionsBuilder AddRedisGrainStorage(this ISiloBuilder builder, string name)
=> new(builder, name);

public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault(
this ISiloBuilder builder
Expand All @@ -33,21 +30,59 @@ this ISiloBuilder builder
internal static IServiceCollection AddRedisGrainStorage(
this IServiceCollection services,
string name,
Action<OptionsBuilder<RedisStorageOptions>> configureOptions = null
Action<OptionsBuilder<RedisStorageOptions>>? configureOptions = null
)
{
configureOptions?.Invoke(services.AddOptions<RedisStorageOptions>(name));
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
services.AddKeyedSingleton(name, CreateStateStore);
services.ConfigureNamedOptionForLogging<RedisStorageOptions>(name);
services.TryAddSingleton(sp =>
sp.GetKeyedService<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));

// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
return services
.AddKeyedSingleton(name, CreateDbConnection)
.AddKeyedSingleton(name, CreateRedisStorage)
.AddKeyedSingleton(name, (provider, n)
=> (ILifecycleParticipant<ISiloLifecycle>)provider.GetRequiredKeyedService<IGrainStorage>(n));
.AddKeyedSingleton<IGrainStateStore>(name, (sp, k) =>
{
var key = (string)k;

Check warning on line 43 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Converting null literal or possible null value to non-nullable type.
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
var serializer = sp.GetRequiredKeyedService<ISerializer>(key);
var humanReadableSerializer = sp.GetKeyedService<IHumanReadableSerializer>(key);
var options = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = sp.GetRequiredService<ILogger<GrainStateStore>>();

return ActivatorUtilities.CreateInstance<GrainStateStore>(
sp,
key,

Check warning on line 52 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Possible null reference argument for parameter 'parameters' in 'GrainStateStore ActivatorUtilities.CreateInstance<GrainStateStore>(IServiceProvider provider, params object[] parameters)'.
connection,
options.Get(key),
serializer,
humanReadableSerializer,

Check warning on line 56 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Possible null reference argument for parameter 'parameters' in 'GrainStateStore ActivatorUtilities.CreateInstance<GrainStateStore>(IServiceProvider provider, params object[] parameters)'.
logger,
sp
);
}
)
.AddKeyedSingleton(name, (sp, k) =>
{
var key = (string)k;

Check warning on line 64 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Converting null literal or possible null value to non-nullable type.
var optionsSnapshot = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = sp.GetRequiredService<ILogger<DbConnection>>();
return ActivatorUtilities.CreateInstance<DbConnection>(sp, optionsSnapshot.Get(key), logger);
}
)
.AddKeyedSingleton<IGrainStorage>(name, (sp, k) =>
{
var key = (string)k;

Check warning on line 72 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Converting null literal or possible null value to non-nullable type.
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);

Check warning on line 75 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Possible null reference argument for parameter 'parameters' in 'RedisGrainStorage ActivatorUtilities.CreateInstance<RedisGrainStorage>(IServiceProvider provider, params object[] parameters)'.
}
)
.AddGrainStorage(name, (sp, key) =>
{
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);
}
)
;
}

internal static ISiloBuilder AddRedisDefaultSerializer(this ISiloBuilder builder, string name)
Expand Down Expand Up @@ -99,40 +134,6 @@ Func<IServiceProvider, object[]> cfg
services.AddKeyedSingleton<IHumanReadableSerializer>(name, (provider, n)
=> ActivatorUtilities.CreateInstance<TSerializer>(provider, cfg?.Invoke(provider)))

Check warning on line 135 in Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / Package / build

Possible null reference argument for parameter 'parameters' in 'TSerializer ActivatorUtilities.CreateInstance<TSerializer>(IServiceProvider provider, params object[] parameters)'.
);

private static IGrainStorage CreateRedisStorage(IServiceProvider services, string name)
{
var store = services.GetRequiredKeyedService<IGrainStateStore>(name);
var connection = services.GetRequiredKeyedService<DbConnection>(name);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(services, name, store, connection);
}

private static IGrainStateStore CreateStateStore(IServiceProvider provider, string name)
{
var connection = provider.GetRequiredKeyedService<DbConnection>(name);
var serializer = provider.GetRequiredKeyedService<ISerializer>(name);
var humanReadableSerializer = provider.GetKeyedServices<IHumanReadableSerializer>(name);
var options = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = provider.GetRequiredService<ILogger<GrainStateStore>>();

return ActivatorUtilities.CreateInstance<GrainStateStore>(
provider,
name,
connection,
options.Get(name),
serializer,
humanReadableSerializer,
logger,
provider
);
}

private static DbConnection CreateDbConnection(IServiceProvider provider, string name)
{
var optionsSnapshot = provider.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = provider.GetRequiredService<ILogger<DbConnection>>();
return ActivatorUtilities.CreateInstance<DbConnection>(provider, optionsSnapshot.Get(name), logger);
}
}

public static class RedisDefaultJsonSerializerSettings
Expand Down

0 comments on commit 5915eec

Please sign in to comment.