From 2e2b11de2d7adce47dd3b867a45d4d048fe0cf26 Mon Sep 17 00:00:00 2001 From: Russell Hammett Date: Mon, 12 Oct 2020 10:44:41 -0400 Subject: [PATCH] Update to latest orleans version - introduces "local" environment - Removes "Startup" class as to avoid some confusion introduced by it's "same name" in reference to web project Startup classes - Updates SiloHost to make use of new `UseOrleans` `IHostBuilder` extension method - Updates Client and Silohost to utilize configuration within application bootstrapping, similar to https://blog.kritner.com/2020/03/13/how-to-rely-on-configuration-during-application-bootstrapping/ --- ...eans.GettingStarted.GrainInterfaces.csproj | 4 +- ...itner.Orleans.GettingStarted.Grains.csproj | 4 +- .../IClientBuilderExtensions.cs | 8 +- ...ritner.OrleansGettingStarted.Client.csproj | 3 +- .../Program.cs | 26 +++--- .../Helpers/ConsoleAppConfigurator.cs | 53 +++++------ ...ritner.OrleansGettingStarted.Common.csproj | 2 +- .../Startup.cs | 28 ------ ...xtensions.cs => ISiloBuilderExtensions.cs} | 16 ++-- ...tner.OrleansGettingStarted.SiloHost.csproj | 7 +- .../Program.cs | 93 ++++++++----------- src/_appsettings/appsettings.local.json | 7 ++ 12 files changed, 96 insertions(+), 155 deletions(-) delete mode 100644 src/Kritner.OrleansGettingStarted.Common/Startup.cs rename src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/{ISiloHostBuilderExtensions.cs => ISiloBuilderExtensions.cs} (78%) create mode 100644 src/_appsettings/appsettings.local.json diff --git a/src/Kritner.Orleans.GettingStarted.GrainInterfaces/Kritner.Orleans.GettingStarted.GrainInterfaces.csproj b/src/Kritner.Orleans.GettingStarted.GrainInterfaces/Kritner.Orleans.GettingStarted.GrainInterfaces.csproj index 5da86e1..3ebd990 100644 --- a/src/Kritner.Orleans.GettingStarted.GrainInterfaces/Kritner.Orleans.GettingStarted.GrainInterfaces.csproj +++ b/src/Kritner.Orleans.GettingStarted.GrainInterfaces/Kritner.Orleans.GettingStarted.GrainInterfaces.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/src/Kritner.Orleans.GettingStarted.Grains/Kritner.Orleans.GettingStarted.Grains.csproj b/src/Kritner.Orleans.GettingStarted.Grains/Kritner.Orleans.GettingStarted.Grains.csproj index 99f762d..52aba56 100644 --- a/src/Kritner.Orleans.GettingStarted.Grains/Kritner.Orleans.GettingStarted.Grains.csproj +++ b/src/Kritner.Orleans.GettingStarted.Grains/Kritner.Orleans.GettingStarted.Grains.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/src/Kritner.OrleansGettingStarted.Client/ExtensionMethods/IClientBuilderExtensions.cs b/src/Kritner.OrleansGettingStarted.Client/ExtensionMethods/IClientBuilderExtensions.cs index b3fc764..3f4a9cd 100644 --- a/src/Kritner.OrleansGettingStarted.Client/ExtensionMethods/IClientBuilderExtensions.cs +++ b/src/Kritner.OrleansGettingStarted.Client/ExtensionMethods/IClientBuilderExtensions.cs @@ -15,11 +15,11 @@ public static class IClientBuilderExtensions /// the Orleans environment. /// /// The client builder. - /// The Orleans configuration options. + /// The Orleans configuration. /// The environment. public static IClientBuilder ConfigureClustering( this IClientBuilder builder, - IOptions orleansConfigOptions, + OrleansConfig orleansConfig, string environmentName ) { @@ -27,10 +27,6 @@ string environmentName { throw new ArgumentNullException(nameof(builder)); } - if (orleansConfigOptions.Value == default(OrleansConfig)) - { - throw new ArgumentException(nameof(orleansConfigOptions)); - } builder.UseLocalhostClustering(); diff --git a/src/Kritner.OrleansGettingStarted.Client/Kritner.OrleansGettingStarted.Client.csproj b/src/Kritner.OrleansGettingStarted.Client/Kritner.OrleansGettingStarted.Client.csproj index a6f32dc..7f0e613 100644 --- a/src/Kritner.OrleansGettingStarted.Client/Kritner.OrleansGettingStarted.Client.csproj +++ b/src/Kritner.OrleansGettingStarted.Client/Kritner.OrleansGettingStarted.Client.csproj @@ -8,7 +8,7 @@ - + @@ -18,6 +18,7 @@ + diff --git a/src/Kritner.OrleansGettingStarted.Client/Program.cs b/src/Kritner.OrleansGettingStarted.Client/Program.cs index c5d1337..6148d10 100644 --- a/src/Kritner.OrleansGettingStarted.Client/Program.cs +++ b/src/Kritner.OrleansGettingStarted.Client/Program.cs @@ -11,31 +11,29 @@ using System.Threading.Tasks; using Kritner.OrleansGettingStarted.Common; using Kritner.OrleansGettingStarted.Client.ExtensionMethods; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; namespace Kritner.OrleansGettingStarted.Client { public class Program { - private static Startup Startup; - private static IServiceProvider ServiceProvider; const int initializeAttemptsBeforeFailing = 5; private static int attempt = 0; - static int Main(string[] args) + static async Task Main(string[] args) { - Startup = ConsoleAppConfigurator.BootstrapApp(); - var serviceCollection = new ServiceCollection(); - Startup.ConfigureServices(serviceCollection); - ServiceProvider = serviceCollection.BuildServiceProvider(); - - return RunMainAsync().Result; + var (env, configurationRoot, orleansConfig) = + ConsoleAppConfigurator.BootstrapConfigurationRoot(); + + return await CreateHostBuilder(args, env, configurationRoot, orleansConfig); } - private static async Task RunMainAsync() + private static async Task CreateHostBuilder(string[] args, string env, IConfigurationRoot configurationRoot, OrleansConfig orleansConfig) { try { - using (var client = await StartClientWithRetries()) + await using (var client = await StartClientWithRetries(env, orleansConfig)) { await new OrleansExamples(new OrleansFunctionProvider()) .ChooseFunction(client); @@ -51,14 +49,14 @@ private static async Task RunMainAsync() } } - private static async Task StartClientWithRetries() + private static async Task StartClientWithRetries(string env, OrleansConfig orleansConfig) { attempt = 0; IClusterClient client; client = new ClientBuilder() .ConfigureClustering( - ServiceProvider.GetService>(), - Startup.HostingEnvironment.EnvironmentName + orleansConfig, + env ) .Configure(options => { diff --git a/src/Kritner.OrleansGettingStarted.Common/Helpers/ConsoleAppConfigurator.cs b/src/Kritner.OrleansGettingStarted.Common/Helpers/ConsoleAppConfigurator.cs index 6e17e94..32f3fcd 100644 --- a/src/Kritner.OrleansGettingStarted.Common/Helpers/ConsoleAppConfigurator.cs +++ b/src/Kritner.OrleansGettingStarted.Common/Helpers/ConsoleAppConfigurator.cs @@ -5,51 +5,40 @@ using Microsoft.Extensions.Hosting.Internal; using System; using System.IO; +using Kritner.OrleansGettingStarted.Common.Config; +using Microsoft.Extensions.Options; namespace Kritner.OrleansGettingStarted.Common.Helpers { public static class ConsoleAppConfigurator { - public static Startup BootstrapApp() + public static (string env, IConfigurationRoot configurationRoot, OrleansConfig orleansConfig) BootstrapConfigurationRoot() { - var environment = GetEnvironment(); - var hostingEnvironment = GetHostingEnvironment(environment); - var configurationBuilder = CreateConfigurationBuilder(environment); + var env = GetEnvironmentName(); + var tempConfigBuilder = new ConfigurationBuilder(); - return new Startup(hostingEnvironment, configurationBuilder); - } + tempConfigBuilder + .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: false) + .AddJsonFile($"appsettings.{env}.json", optional: false, reloadOnChange: false); - private static string GetEnvironment() - { - var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); - if (string.IsNullOrEmpty(environmentName)) - { - return "dev"; - } + var configurationRoot = tempConfigBuilder.Build(); - return environmentName; + var serviceCollection = new ServiceCollection(); + serviceCollection.Configure(configurationRoot.GetSection(nameof(OrleansConfig))); + var serviceProvider = serviceCollection.BuildServiceProvider(); + var orleansConfig = serviceProvider.GetService>().Value; + return (env, configurationRoot, orleansConfig); } - - private static IHostingEnvironment GetHostingEnvironment(string environmentName) + + public static string GetEnvironmentName() { - return new HostingEnvironment + var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); + if (string.IsNullOrWhiteSpace(env)) { - EnvironmentName = environmentName, - ApplicationName = AppDomain.CurrentDomain.FriendlyName, - ContentRootPath = AppDomain.CurrentDomain.BaseDirectory, - ContentRootFileProvider = new PhysicalFileProvider(AppDomain.CurrentDomain.BaseDirectory) - }; - } - - private static IConfigurationBuilder CreateConfigurationBuilder(string environmentName) - { - var config = new ConfigurationBuilder() - .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); + throw new Exception("ASPNETCORE_ENVIRONMENT env variable not set."); + } - return config; + return env; } } } diff --git a/src/Kritner.OrleansGettingStarted.Common/Kritner.OrleansGettingStarted.Common.csproj b/src/Kritner.OrleansGettingStarted.Common/Kritner.OrleansGettingStarted.Common.csproj index d635b12..fef1c06 100644 --- a/src/Kritner.OrleansGettingStarted.Common/Kritner.OrleansGettingStarted.Common.csproj +++ b/src/Kritner.OrleansGettingStarted.Common/Kritner.OrleansGettingStarted.Common.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/Kritner.OrleansGettingStarted.Common/Startup.cs b/src/Kritner.OrleansGettingStarted.Common/Startup.cs deleted file mode 100644 index f51995c..0000000 --- a/src/Kritner.OrleansGettingStarted.Common/Startup.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Kritner.OrleansGettingStarted.Common.Config; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Kritner.OrleansGettingStarted.Common -{ - public class Startup - { - public IHostingEnvironment HostingEnvironment { get; } - public IConfiguration Configuration { get; } - - public Startup( - IHostingEnvironment hostingEnvironment, - IConfigurationBuilder configurationBuilder - ) - { - HostingEnvironment = hostingEnvironment; - Configuration = configurationBuilder.Build(); - } - - public void ConfigureServices(IServiceCollection serviceCollection) - { - serviceCollection.AddOptions(); - serviceCollection.Configure(Configuration.GetSection(nameof(OrleansConfig))); - } - } -} diff --git a/src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloHostBuilderExtensions.cs b/src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloBuilderExtensions.cs similarity index 78% rename from src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloHostBuilderExtensions.cs rename to src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloBuilderExtensions.cs index ebfd0fa..dfc8f39 100644 --- a/src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloHostBuilderExtensions.cs +++ b/src/Kritner.OrleansGettingStarted.SiloHost/ExtensionMethods/ISiloBuilderExtensions.cs @@ -7,18 +7,18 @@ namespace Kritner.OrleansGettingStarted.SiloHost.ExtensionMethods { - public static class ISiloHostBuilderExtensions + public static class ISiloBuilderExtensions { /// /// Configures clustering for the Orleans Silo Host based on /// the Orleans environment. /// - /// The silo host builder. - /// The Orleans configuration options. + /// The silo builder. + /// The Orleans configuration. /// The environment. - public static ISiloHostBuilder ConfigureClustering( - this ISiloHostBuilder builder, - IOptions orleansConfigOptions, + public static ISiloBuilder ConfigureClustering( + this ISiloBuilder builder, + OrleansConfig orleansConfig, string environmentName ) { @@ -26,10 +26,6 @@ string environmentName { throw new ArgumentNullException(nameof(builder)); } - if (orleansConfigOptions.Value == default(OrleansConfig)) - { - throw new ArgumentException(nameof(orleansConfigOptions)); - } builder.UseLocalhostClustering(); builder.Configure(options => options.AdvertisedIPAddress = IPAddress.Loopback); diff --git a/src/Kritner.OrleansGettingStarted.SiloHost/Kritner.OrleansGettingStarted.SiloHost.csproj b/src/Kritner.OrleansGettingStarted.SiloHost/Kritner.OrleansGettingStarted.SiloHost.csproj index 9fd78d4..dd1df34 100644 --- a/src/Kritner.OrleansGettingStarted.SiloHost/Kritner.OrleansGettingStarted.SiloHost.csproj +++ b/src/Kritner.OrleansGettingStarted.SiloHost/Kritner.OrleansGettingStarted.SiloHost.csproj @@ -7,9 +7,9 @@ - - - + + + @@ -20,6 +20,7 @@ + diff --git a/src/Kritner.OrleansGettingStarted.SiloHost/Program.cs b/src/Kritner.OrleansGettingStarted.SiloHost/Program.cs index 88a45de..9a559ee 100644 --- a/src/Kritner.OrleansGettingStarted.SiloHost/Program.cs +++ b/src/Kritner.OrleansGettingStarted.SiloHost/Program.cs @@ -1,6 +1,4 @@ -using Kritner.OrleansGettingStarted.Common; -using Kritner.OrleansGettingStarted.Common.Config; -using Kritner.OrleansGettingStarted.Common.Helpers; +using Kritner.OrleansGettingStarted.Common.Config; using Kritner.Orleans.GettingStarted.Grains; using Kritner.OrleansGettingStarted.SiloHost.ExtensionMethods; using Kritner.OrleansGettingStarted.SiloHost.Helpers; @@ -13,70 +11,53 @@ using Orleans.Statistics; using System; using System.Threading.Tasks; +using Kritner.OrleansGettingStarted.Common.Helpers; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; namespace Kritner.OrleansGettingStarted.SiloHost { public class Program { - private static Startup Startup; - private static IServiceProvider ServiceProvider; - - public static int Main(string[] args) - { - Startup = ConsoleAppConfigurator.BootstrapApp(); - var serviceCollection = new ServiceCollection(); - Startup.ConfigureServices(serviceCollection); - ServiceProvider = serviceCollection.BuildServiceProvider(); - - return RunMainAsync().Result; - } - - private static async Task RunMainAsync() + public static async Task Main(string[] args) { - try - { - var host = await StartSilo(); - Console.WriteLine("Press Enter to terminate..."); - Console.ReadLine(); - - await host.StopAsync(); - - return 0; - } - catch (Exception ex) - { - Console.WriteLine(ex); - return 1; - } + var (env, configurationRoot, orleansConfig) = + ConsoleAppConfigurator.BootstrapConfigurationRoot(); + + await CreateHostBuilder(args, env, configurationRoot, orleansConfig).Build().RunAsync(); } - - private static async Task StartSilo() + + private static IHostBuilder CreateHostBuilder(string[] args, string env, IConfigurationRoot configurationRoot, OrleansConfig orleansConfig) { - // define the cluster configuration - var builder = new SiloHostBuilder() - .ConfigureClustering( - ServiceProvider.GetService>(), - Startup.HostingEnvironment.EnvironmentName - ) - .Configure(options => + return Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((context, builder) => { - options.ClusterId = "dev"; - options.ServiceId = "HelloWorldApp"; + context.HostingEnvironment.EnvironmentName = env; + builder.AddConfiguration(configurationRoot); }) - .AddMemoryGrainStorage(Constants.OrleansMemoryProvider) - .ConfigureApplicationParts(parts => + .UseOrleans(siloBuilder => { - parts.AddApplicationPart(typeof(IGrainMarker).Assembly).WithReferences(); - }) - .ConfigureServices(DependencyInjectionHelper.IocContainerRegistration) - .UsePerfCounterEnvironmentStatistics() - .UseDashboard(options => { }) - .UseInMemoryReminderService() - .ConfigureLogging(logging => logging.AddConsole()); - - var host = builder.Build(); - await host.StartAsync(); - return host; + siloBuilder + .ConfigureClustering( + orleansConfig, + env + ) + .Configure(options => + { + options.ClusterId = "dev"; + options.ServiceId = "HelloWorldApp"; + }) + .AddMemoryGrainStorage(Constants.OrleansMemoryProvider) + .ConfigureApplicationParts(parts => + { + parts.AddApplicationPart(typeof(IGrainMarker).Assembly).WithReferences(); + }) + .ConfigureServices(DependencyInjectionHelper.IocContainerRegistration) + .UsePerfCounterEnvironmentStatistics() + .UseDashboard(options => { }) + .UseInMemoryReminderService() + .ConfigureLogging(logging => logging.AddConsole()); + }); } } } diff --git a/src/_appsettings/appsettings.local.json b/src/_appsettings/appsettings.local.json new file mode 100644 index 0000000..19cec60 --- /dev/null +++ b/src/_appsettings/appsettings.local.json @@ -0,0 +1,7 @@ +{ + "OrleansConfig" : { + "NodeIpAddresses" : ["0.0.0.0"], + "GatewayPort" : -1, + "SiloPort" : -1 + } +} \ No newline at end of file