/// <summary> /// 发布事件 /// </summary> /// <typeparam name="TSender"></typeparam> /// <typeparam name="TEventArgs"></typeparam> /// <param name="sender"></param> /// <param name="eventArgs"></param> /// <returns></returns> public async Task <bool> PublishAsync <TSender, TEventArgs>(TSender sender, TEventArgs eventArgs) where TEventArgs : CommonEventArgs { string eventKey = GetEventKey <TSender, TEventArgs>(); if (!handlers.ContainsKey(eventKey)) { return(false); } var handlerTypes = handlers[eventKey]; foreach (Type handlerType in handlerTypes) { var handler = ServiceLocator.GetService <IEventHandler <TSender, TEventArgs> >(handlerType); if (null != handler) { await handler.HandleAsync(sender, eventArgs).ContinueWith(task => { task.Exception?.Handle(exception => { LoggerLocator.GetLogger <LocalEventBus>().LogError(exception, "执行触发操作事件时发生异常"); return(true); }); }).ConfigureAwait(false); } } return(true); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.TryAddTransient <IHttpContextAccessor, HttpContextAccessor>(); services.AddCors(); services.AddSecurity(Configuration); services.AddDbContext <CarRentalUnitOfWork>(optionsBuilder => { optionsBuilder .UseMySQL(Configuration.GetConnectionString("CarRentalDataSource")); }, ServiceLifetime.Scoped); // registers repositories services.AddTransient <ReadModel.IAvailableCarRepository, Infrastructure.Repositories.AvailableCarRepository>(); services.AddTransient <IAvailableCarRepository, AvailableCarRepository>(); services.AddTransient <IUserRepository, UserRepository>(); services.AddTransient <IBookingRepository, BookingRepository>(); //register application services services.AddTransient <IAvailableCarAppService, AvailableCarAppService>(); services.AddTransient <IAvailableCarService, AvailableCarService>(); services.AddMediatR(typeof(CreateBookingCommand).Assembly); services.AddAutoMapper(); services.AddMvc() .AddFluentValidation(fv => { fv.ImplicitlyValidateChildProperties = true; fv.RegisterValidatorsFromAssemblyContaining <AddressModel>(); fv.RegisterValidatorsFromAssemblyContaining <CreateBookingCommand>(); }); //sigletos TypeAdapterLocator.SetCurrent(new AutoMapperTypeAdapterFactory()); EntityValidatorLocator.SetCurrent(new DataAnnotationsEntityValidatorFactory()); LoggerLocator.SetCurrent(new Log4NetLoggerFactory()); }
/// <summary> /// 订阅事件 /// </summary> /// <typeparam name="TEventArgs"></typeparam> /// <typeparam name="TEventHandler"></typeparam> public void Subscribe <TSender, TEventArgs, TEventHandler>() where TEventArgs : CommonEventArgs where TEventHandler : IEventHandler <TSender, TEventArgs> { AddSubscriptionAsync <TSender, TEventArgs, TEventHandler>().Wait(); var channelName = GetChannelName <TSender, TEventArgs>(typeof(TEventHandler).FullName); redisClient.Db0.SubscribeAsync <RedisEventMessgae <TSender, TEventArgs> >(channelName, async(message) => { var handler = ServiceLocator.GetService <TEventHandler>(); if (handler != null && message != null) { await handler.HandleAsync(message.Sender, message.EventArgs).ContinueWith(task => { task.Exception?.Handle(exception => { LoggerLocator.GetLogger <RedisEventBus>().LogError(exception, "执行触发操作事件时发生异常"); return(true); }); }).ConfigureAwait(false); } }); }
private ILogger GetLog() { return(LoggerLocator.CreateLog(GetType())); }
private static void RegisterSingletons() { TypeAdapterLocator.SetCurrent(new AutoMapperTypeAdapterFactory()); EntityValidatorLocator.SetCurrent(new DataAnnotationsEntityValidatorFactory()); LoggerLocator.SetCurrent(new Log4NetLoggerFactory()); }