山东雷驰
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

57 lines
2.3 KiB

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Hosting;
using Orleans.Statistics;
using Serilog;
using System;
namespace Kean.Infrastructure.Orleans
{
/// <summary>
/// ServiceCollection 扩展方法
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// 向服务描述中追加 Orleans 配置
/// </summary>
/// <param name="services">服务描述符</param>
/// <param name="setupAction">配置项</param>
/// <returns>服务描述符</returns>
public static IServiceCollection AddOrleans(this IServiceCollection services, Action<OrleansOptions> setupAction)
{
var orleansOptions = new OrleansOptions();
setupAction(orleansOptions);
var siloHost = new SiloHostBuilder()
.ConfigureServices(orleansOptions.ConfigureDelegate)
.ConfigureLogging((hostBuilderContext, loggingBuilder) =>
{
loggingBuilder.AddSerilog();
loggingBuilder.AddConfiguration(hostBuilderContext.Configuration);
})
.UseLocalhostClustering(
siloPort: orleansOptions.SiloPort,
gatewayPort: orleansOptions.GatewayPort,
serviceId: orleansOptions.ServiceId,
clusterId: orleansOptions.ClusterId
)
.UseRedisClustering(redisClusteringOptions =>
{
redisClusteringOptions.ConnectionString = orleansOptions.RedisClustering.ConnectionString;
redisClusteringOptions.Database = orleansOptions.RedisClustering.Database;
})
.UseDashboard(dashboardOptions => dashboardOptions.HostSelf = false)
.UsePerfCounterEnvironmentStatistics()
.Build();
var client = siloHost.Services.GetRequiredService<IClusterClient>();
return services
.AddHostedService(serviceProvider => new HostedService(siloHost))
.AddServicesForSelfHostedDashboard()
.AddDashboardEmbeddedFiles()
.AddSingleton<IGrainFactory>(client)
.AddSingleton(client);
}
}
}