Created
June 21, 2018 16:11
-
-
Save galvesribeiro/40dfdad9f2b574049172492617b7eef5 to your computer and use it in GitHub Desktop.
Orleans support to Microsoft.Extensions.Hosting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class OrleansExtensions | |
{ | |
private const string SiloBuilderKey = "OrleansSiloBuilderInstance"; | |
public static IHostBuilder AddOrleans(this IHostBuilder hostBuilder, Action<ISiloHostBuilder> configure) | |
{ | |
var siloHostBuilder = GetSiloBuilder(hostBuilder); | |
configure?.Invoke(siloHostBuilder); | |
siloHostBuilder.ConfigureDefaults(); | |
siloHostBuilder.GetApplicationPartManager().ConfigureDefaults(); | |
hostBuilder.ConfigureServices(services => | |
{ | |
services.AddOptions(); | |
services.AddLogging(); | |
services.AddHostedService<OrleansHostedService>(); | |
}); | |
return hostBuilder; | |
} | |
private static ISiloHostBuilder GetSiloBuilder(IHostBuilder hostBuilder) | |
{ | |
ISiloHostBuilder siloBuilder; | |
if (hostBuilder.Properties.TryGetValue(SiloBuilderKey, out var value)) | |
{ | |
siloBuilder = value as ISiloHostBuilder; | |
if (siloBuilder == null) throw new InvalidOperationException($"The SiloBuilder value is of the wrong type {value.GetType()}. It should be {nameof(ISiloHostBuilder)}"); | |
} | |
else | |
{ | |
hostBuilder.Properties[SiloBuilderKey] = siloBuilder = new SiloBuilder(hostBuilder); | |
} | |
return siloBuilder; | |
} | |
private class SiloBuilder : ISiloHostBuilder | |
{ | |
private readonly IHostBuilder hostBuilder; | |
public SiloBuilder(IHostBuilder hostBuilder) | |
{ | |
this.hostBuilder = hostBuilder; | |
} | |
public IDictionary<object, object> Properties => this.hostBuilder.Properties; | |
public ISiloHost Build() => throw new NotSupportedException(); | |
public ISiloHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate) | |
{ | |
this.hostBuilder.ConfigureHostConfiguration(configureDelegate); | |
return this; | |
} | |
public ISiloHostBuilder ConfigureAppConfiguration(Action<Orleans.Hosting.HostBuilderContext, IConfigurationBuilder> configureDelegate) | |
{ | |
this.hostBuilder.ConfigureAppConfiguration((ctx, cb) => configureDelegate(GetContext(ctx), cb)); | |
return this; | |
} | |
public ISiloHostBuilder ConfigureServices(Action<Orleans.Hosting.HostBuilderContext, IServiceCollection> configureDelegate) | |
{ | |
this.hostBuilder.ConfigureServices((ctx, serviceCollection) => configureDelegate(GetContext(ctx), serviceCollection)); | |
return this; | |
} | |
public ISiloHostBuilder UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory) | |
{ | |
this.hostBuilder.UseServiceProviderFactory(factory); | |
return this; | |
} | |
public ISiloHostBuilder ConfigureContainer<TContainerBuilder>(Action<Orleans.Hosting.HostBuilderContext, TContainerBuilder> configureDelegate) | |
{ | |
this.hostBuilder.ConfigureContainer<TContainerBuilder>((ctx, containerBuilder) => configureDelegate(GetContext(ctx), containerBuilder)); | |
return this; | |
} | |
public static Orleans.Hosting.HostBuilderContext GetContext(Microsoft.Extensions.Hosting.HostBuilderContext ctx) | |
{ | |
var siloContext = new Orleans.Hosting.HostBuilderContext(ctx.Properties) | |
{ | |
Configuration = ctx.Configuration, | |
HostingEnvironment = new HostingEnvironment(ctx.HostingEnvironment) | |
}; | |
return siloContext; | |
} | |
private class HostingEnvironment : Orleans.Hosting.IHostingEnvironment | |
{ | |
private readonly Microsoft.Extensions.Hosting.IHostingEnvironment env; | |
public HostingEnvironment(Microsoft.Extensions.Hosting.IHostingEnvironment env) | |
{ | |
this.env = env; | |
} | |
public string EnvironmentName | |
{ | |
get => this.env.EnvironmentName; | |
set => this.env.EnvironmentName = value; | |
} | |
public string ApplicationName | |
{ | |
get => this.env.ApplicationName; | |
set => this.env.ApplicationName = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class OrleansHostedService : IHostedService | |
{ | |
private readonly ISiloHost _siloHost; | |
private readonly ILogger<OrleansHostedService> _logger; | |
public OrleansHostedService(ILoggerFactory loggerFactory, ISiloHost siloHost) | |
{ | |
this._logger = loggerFactory.CreateLogger<OrleansHostedService>(); | |
this._siloHost = siloHost; | |
} | |
public Task StartAsync(CancellationToken cancellationToken) | |
{ | |
this._logger.LogInformation($"--------- Starting Orleans Hosted Silo ---------"); | |
return this._siloHost.StartAsync(cancellationToken); | |
} | |
public Task StopAsync(CancellationToken cancellationToken) | |
{ | |
this._logger.LogInformation($"--------- Stopping Orleans Hosted Silo ---------"); | |
return this._siloHost.StopAsync(cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment