Skip to content

Commit

Permalink
#317: Do not use SELECT for singletons.
Browse files Browse the repository at this point in the history
While the array produced only has about 13 elements, using LinQ select on it causes it to produce over 60 singletons.
  • Loading branch information
jf-06 committed Jun 18, 2024
1 parent bbbb581 commit be05708
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions src/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,39 @@ private static IEnumerable<IConfigurationProvider> GetSettingsProviders()
var assembly = Assembly.GetAssembly(typeof(BaseSettingsProvider));
var ns = typeof(BaseSettingsProvider).Namespace;

var singletons = assembly
var types = assembly
.GetTypes()
.Where(t => string.Equals(t.Namespace, ns, StringComparison.Ordinal) &&
t.BaseType.Name == typeof(BaseSettingsProvider).Name) // finicky
.Select(t =>
t.BaseType.Name == typeof(BaseSettingsProvider).Name)
.ToList(); // finicky

var singletons = new List<IConfigurationProvider>();

foreach (var t in types)
{
// Construct the singleton.
var constructor = t.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
// Construct the singleton.
var constructor = t.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
Console.Error.WriteLine("Provider {0} did not expose a public constructor!", t.FullName);
Console.Error.WriteLine("Provider {0} did not expose a public constructor!", t.FullName);

return null;
}
singletons.Add(null);

var singleton = constructor.Invoke(null);
if (singleton == null)
{
Console.Error.WriteLine("Provider {0} did not construct a singleton!", t.FullName);
continue;
}

var singleton = constructor.Invoke(null);
if (singleton == null || singleton is not IConfigurationProvider provider)
{
Console.Error.WriteLine("Provider {0} did not construct a singleton!", t.FullName);

return null;
}
singletons.Add(null);

return singleton;
});
continue;
}

singletons.Add(provider);
}

return singletons.Cast<IConfigurationProvider>();
}
Expand Down Expand Up @@ -383,10 +391,17 @@ private static void StartGrpcServer(IEnumerable<string> args, IServiceProvider s
options.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
listenOptions.UseHttps(globalSettings.GrpcServerCertificatePath, globalSettings.GrpcServerCertificatePassword, httpsOptions =>

try
{
listenOptions.UseHttps(globalSettings.GrpcServerCertificatePath, globalSettings.GrpcServerCertificatePassword, httpsOptions =>
{
httpsOptions.SslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12;
});
} catch (Exception ex)
{
httpsOptions.SslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12;
});
logger.Warning("Failed to configure gRPC with HTTPS because: {0}. Will resort to insecure host instead!", ex.Message);
}
});
});

Expand Down

0 comments on commit be05708

Please sign in to comment.