public static void UseEFCoreEventStorage(this ShriekOptionBuilder builder, Action <DbContextOptionsBuilder> optionsAction = null) { builder.Services.AddDbContext <EventStorageSQLContext>(optionsAction); builder.Services.AddScoped <IEventStorageRepository, EventStorageSQLRepository>(); builder.Services.AddScoped <IMementoRepository, EventStorageSQLRepository>(); builder.Services.AddScoped <IEventStorage, DefalutEventStorage>(); }
public static void UseRabbitMqCommandBus(this ShriekOptionBuilder builder, Action <RabbitMqOptions> optionAction) { var option = new CommandBusRabbitMqOptions(); optionAction(option); AddRabbitMq(builder, option); builder.Services.AddTransient <ICommandBus, RabbitMqCommandBus>(); }
private static void AddRabbitMq(ShriekOptionBuilder builder, RabbitMqOptions option) { var factory = new ConnectionFactory() { HostName = option.HostName, UserName = option.UserName, Password = option.Password }; //创建连接 var connection = factory.CreateConnection(); //创建通道 var channel = connection.CreateModel(); //声明一个队列 (durable=true 持久化消息) channel.QueueDeclare(option.QueueName, true, false, false, null); if (!string.IsNullOrEmpty(option.ExchangeName)) { channel.ExchangeDeclare(option.ExchangeName, option.ExchangeType, false, false, null); //将队列绑定到交换机 channel.QueueBind(option.QueueName, option.ExchangeName, option.RouteKey, null); } option.Channel = channel; //事件基本消费者 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件 consumer.Received += (sender, args) => { var json = Encoding.UTF8.GetString(args.Body); var o = JObject.Parse(json); dynamic message = o.ToObject(Type.GetType(o[nameof(Message.MessageType)].Value <string>())); try { option.MessagePublisher.Send(message); //确认该消息已被消费 channel.BasicAck(args.DeliveryTag, false); } catch { // ignored } }; //启动消费者 设置为手动应答消息 channel.BasicConsume(option.QueueName, false, consumer); builder.Services.AddSingleton(option.GetType(), x => option); }
public static void UseLiteDbEventStorage(this ShriekOptionBuilder builder, Action <LiteDBOptions> optionAction) { var options = new LiteDBOptions(); optionAction(options); builder.Services.AddScoped(x => options); builder.Services.AddScoped <EventStorageLiteDatabase>(); builder.Services.AddScoped <IEventStorageRepository, EventStorageRepository>(); builder.Services.AddScoped <IMementoRepository, EventStorageRepository>(); builder.Services.AddScoped <IEventStorage, SqlEventStorage>(); }
public static void UseDapperEventStorage(this ShriekOptionBuilder builder, Action <DapperOptions> optionsAction) { var options = new DapperOptions(); optionsAction?.Invoke(options); builder.Services.AddScoped(x => options); builder.Services.AddScoped <IEventStorageRepository, EventRepository>(); builder.Services.AddScoped <IMementoRepository, MementoRepository>(); builder.Services.AddScoped <IEventStorage, SqlEventStorage>(); }
public static void UseMongoDBEventStorage(this ShriekOptionBuilder builder, Action <MongoDBOptions> optionsAction) { var options = new MongoDBOptions(); optionsAction(options); builder.Services.AddScoped(x => new MongoDatabase(options)); builder.Services.AddScoped <IEventStorageRepository, EventStorageRepository>(); builder.Services.AddScoped <IMementoRepository, EventStorageRepository>(); builder.Services.AddScoped <IEventStorage, SqlEventStorage>(); }
public static void UseInfluxDbEventStorage(this ShriekOptionBuilder builder, Action <InfluxDBOptions> optionAction) { var options = new InfluxDBOptions(); optionAction(options); builder.Services.AddScoped(x => options); builder.Services.AddScoped <InfluxDbContext>(); builder.Services.AddScoped <IEventStorageRepository, EventStorageRepository>(); builder.Services.AddScoped <IMementoRepository, MementoRepository>(); builder.Services.AddScoped <IEventStorage, DefalutEventStorage>(); }
public static void UseEventStorageRedisCache(this ShriekOptionBuilder builder, Action <RedisEventStorageOptions> optionAction) { var option = new RedisEventStorageOptions(); optionAction(option); if (builder.Services.All(x => x.ServiceType != typeof(IDistributedCache))) { builder.Services.AddSingleton <ICacheService>(x => new RedisCacheService(new RedisCache(new RedisCacheOptions { Configuration = option.Configuration, InstanceName = option.InstanceName }))); } else { builder.Services.AddSingleton <ICacheService, RedisCacheService>(); } builder.Services.AddSingleton <IEventStorage, RedisEventStorage>(); }
/// <summary> /// 使用WebApi动态代理客户端 /// </summary> /// <param name="builder"></param> /// <param name="optionAction">配置</param> public static void UseWebApiProxy(this ShriekOptionBuilder builder, Action <WebApiProxyOptions> optionAction) { builder.Services.AddWebApiProxy(optionAction); }
public static void AddRabbitMQ(this ShriekOptionBuilder builder, Action <RabbitMqOptions> optionAction) { var option = new RabbitMqOptions(); optionAction(option); var factory = new ConnectionFactory() { HostName = option.HostName, UserName = option.UserName, Password = option.Password }; //创建连接 var connection = factory.CreateConnection(); //创建通道 var channel = connection.CreateModel(); //声明一个队列 (durable=true 持久化消息) channel.QueueDeclare(option.QueueName, true, false, false, null); if (!string.IsNullOrEmpty(option.ExchangeName)) { channel.ExchangeDeclare(option.ExchangeName, option.ExchangeType, false, false, null); //将队列绑定到交换机 channel.QueueBind(option.QueueName, option.ExchangeName, option.RouteKey, null); } option.Channel = channel; //事件基本消费者 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件 consumer.Received += (sender, args) => { var msgPackJson = Encoding.UTF8.GetString(args.Body); var msgPack = JsonConvert.DeserializeObject <MessagePack>(msgPackJson); var o = JObject.Parse(msgPack.Data); var messageType = Type.GetType(msgPack.MessageType); dynamic message = o.ToObject(messageType); try { var subscribers = option.ServiceProvider.GetServices(typeof(IMessageSubscriber <>).MakeGenericType(messageType)); foreach (var sub in subscribers) { ((dynamic)sub).Execute((dynamic)message); } //确认该消息已被消费 channel.BasicAck(args.DeliveryTag, false); } catch { // ignored } }; //启动消费者 设置为手动应答消息 channel.BasicConsume(option.QueueName, false, consumer); builder.Services.AddSingleton(option.GetType(), x => option); }
public static void UseRabbitMqCommandBus(this ShriekOptionBuilder builder, Action <RabbitMqOptions> optionAction) { builder.AddRabbitMQ(optionAction); builder.Services.AddTransient <ICommandBus, RabbitMqCommandBus>(); }