/// <summary> /// Configures the host builder to host an Orleans silo. /// </summary> /// <param name="hostBuilder">The host builder.</param> /// <param name="configureDelegate">The delegate used to configure the silo.</param> /// <returns>The host builder.</returns> /// <remarks> /// Calling this method multiple times on the same <see cref="IHostBuilder"/> instance will result in one silo being configured. /// However, the effects of <paramref name="configureDelegate"/> will be applied once for each call. /// </remarks> public static IHostBuilder UseOrleans( this IHostBuilder hostBuilder, Action <HostBuilderContext, ISiloBuilder> configureDelegate) { if (configureDelegate == null) { throw new ArgumentNullException(nameof(configureDelegate)); } const string siloBuilderKey = "SiloBuilder"; SiloBuilder siloBuilder; if (!hostBuilder.Properties.ContainsKey(siloBuilderKey)) { siloBuilder = new SiloBuilder(hostBuilder); hostBuilder.Properties.Add(siloBuilderKey, siloBuilder); hostBuilder.ConfigureServices((context, services) => { siloBuilder.Build(context, services); }); } else { siloBuilder = (SiloBuilder)hostBuilder.Properties[siloBuilderKey]; } siloBuilder.ConfigureSilo(configureDelegate); return(hostBuilder); }
/// <summary> /// Creates an returns a new silo. /// </summary> /// <param name="hostName">The silo name if it is not already specified in the configuration.</param> /// <param name="configuration">The configuration.</param> /// <returns>A new silo.</returns> public static ISiloHost CreateSiloHost(string hostName, IConfiguration configuration) { string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName; var hostBuilder = new HostBuilder(); var siloBuilder = new SiloBuilder(hostBuilder); var siloHostBuilder = new SiloHostBuilderAdaptor(hostBuilder, siloBuilder); // Add the silo builder to the host builder so that it is executed during configuration time. hostBuilder.Properties[nameof(SiloBuilder)] = siloBuilder; hostBuilder.ConfigureServices((context, services) => { siloBuilder.Build(context, services); }); siloBuilder .Configure <ClusterOptions>(configuration) .Configure <SiloOptions>(options => options.SiloName = siloName); hostBuilder.ConfigureHostConfiguration(cb => cb.AddConfiguration(configuration)); hostBuilder.Properties["Configuration"] = configuration; ConfigureAppServices(configuration, hostBuilder, siloBuilder, siloHostBuilder); hostBuilder.ConfigureServices((context, services) => { services.AddSingleton <TestHooksHostEnvironmentStatistics>(); services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>(); services.AddSingleton <TestHooksSystemTarget>(); ConfigureListeningPorts(context.Configuration, services); TryConfigureClusterMembership(context.Configuration, services); TryConfigureFileLogging(configuration, services, siloName); if (Debugger.IsAttached) { // Test is running inside debugger - Make timeout ~= infinite services.Configure <SiloMessagingOptions>(op => op.ResponseTimeout = TimeSpan.FromMilliseconds(1000000)); } }); var host = hostBuilder.Build(); var silo = host.Services.GetRequiredService <ISiloHost>(); InitializeTestHooksSystemTarget(silo); return(silo); }
/// <summary> /// Creates an returns a new silo. /// </summary> /// <param name="hostName">The silo name if it is not already specified in the configuration.</param> /// <param name="configurationSources">The configuration.</param> /// <returns>A new silo.</returns> public static ISiloHost CreateSiloHost(string hostName, IEnumerable <IConfigurationSource> configurationSources) { var configBuilder = new ConfigurationBuilder(); foreach (var source in configurationSources) { configBuilder.Add(source); } var configuration = configBuilder.Build(); string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName; var hostBuilder = new HostBuilder(); var siloBuilder = new SiloBuilder(hostBuilder); var siloHostBuilder = new SiloHostBuilderAdaptor(hostBuilder, siloBuilder); // Add the silo builder to the host builder so that it is executed during configuration time. hostBuilder.Properties[nameof(SiloBuilder)] = siloBuilder; hostBuilder.ConfigureServices((context, services) => { siloBuilder.Build(context, services); }); siloBuilder .Configure <ClusterOptions>(configuration) .Configure <SiloOptions>(options => options.SiloName = siloName); hostBuilder .ConfigureHostConfiguration(cb => { // TODO: Instead of passing the sources individually, just chain the pre-built configuration once we upgrade to Microsoft.Extensions.Configuration 2.1 foreach (var source in configBuilder.Sources) { cb.Add(source); } }); hostBuilder.Properties["Configuration"] = configuration; ConfigureAppServices(configuration, hostBuilder, siloBuilder, siloHostBuilder); hostBuilder.ConfigureServices((context, services) => { services.AddSingleton <TestHooksHostEnvironmentStatistics>(); services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>(); services.AddSingleton <TestHooksSystemTarget>(); ConfigureListeningPorts(context.Configuration, services); TryConfigureClusterMembership(context.Configuration, services); TryConfigureFileLogging(configuration, services, siloName); if (Debugger.IsAttached) { // Test is running inside debugger - Make timeout ~= infinite services.Configure <SiloMessagingOptions>(op => op.ResponseTimeout = TimeSpan.FromMilliseconds(1000000)); } }); siloBuilder.GetApplicationPartManager().ConfigureDefaults(); var host = hostBuilder.Build(); var silo = host.Services.GetRequiredService <ISiloHost>(); InitializeTestHooksSystemTarget(silo); return(silo); }