コード例 #1
0
ファイル: RinSerivceExtensions.cs プロジェクト: vip32/Rin
        public static void AddRin(this IServiceCollection services, Action <RinOptions> configure = null)
        {
            var options = new RinOptions();

            configure?.Invoke(options);

            services.AddHttpContextAccessor();

            var eventBus          = new MessageEventBus <RequestEventMessage>();
            var eventBusStoreBody = new MessageEventBus <StoreBodyEventMessage>();
            var transformerSet    = new BodyDataTransformerSet(
                new BodyDataTransformerPipeline(options.Inspector.RequestBodyDataTransformers),
                new BodyDataTransformerPipeline(options.Inspector.ResponseBodyDataTransformers)
                );

            // Other services
            services.AddSingleton <BodyDataTransformerSet>(transformerSet);
            services.AddSingleton <IRecordStorage>(options.RequestRecorder.StorageFactory);
            services.AddSingleton <IMessageEventBus <RequestEventMessage> >(eventBus);
            services.AddSingleton <IMessageEventBus <StoreBodyEventMessage> >(eventBusStoreBody);
            services.AddSingleton <RinOptions>(options);
            services.AddSingleton <RinChannel>();

            // IMessageSubscriber<RequestEventMessage> services
            services.AddSingleton <IMessageSubscriber <RequestEventMessage> >(x => new Rin.Hubs.RinCoreHub.MessageSubscriber(x.GetRequiredService <RinChannel>()));

            services.AddTransient <IResourceProvider, EmbeddedZipResourceProvider>();
        }
コード例 #2
0
        /// <summary>
        /// 获取登录结果
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="signInResult">登录结果</param>
        /// <param name="applicationCode">应用程序编码</param>
        private async Task <SignInWithTokenResult> GetLoginResultAsync(User user, SignInResult signInResult, string applicationCode)
        {
            if (signInResult.State == SignInState.Failed)
            {
                return new SignInWithTokenResult {
                           UserId = signInResult.UserId, State = signInResult.State, Message = signInResult.Message
                }
            }
            ;
            await MessageEventBus.PublishAsync(new UserLoginMessageEvent(new UserLoginMessage
            {
                UserId = user.Id,
                Name = user.Nickname,
                Ip = Web.IP,
                UserAgent = Web.Browser
            }));

            var result = await TokenBuilder.CreateAsync(user.GetClaims().ToDictionary(x => x.Type, x => x.Value));

            return(new SignInWithTokenResult
            {
                UserId = signInResult.UserId,
                State = signInResult.State,
                Message = signInResult.Message,
                Token = result
            });
        }
コード例 #3
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request">请求</param>
        public async Task <SignInWithTokenResult> SignInAsync(AdminLoginRequest request)
        {
            var user = await GetUserAsync(request.UserName);

            if (user == null)
            {
                return new SignInWithTokenResult {
                           Message = "用户名不存在", State = SignInState.Failed
                }
            }
            ;
            await AddClaimsToUserAsync(user, ApplicationCode.Admin);

            var result = await SignInManager.SignInAsync(user, request.Password);

            user.AddLoginLog(Web.IP, Web.Browser);
            await MessageEventBus.PublishAsync(new UserLoginMessageEvent(new UserLoginMessage
            {
                UserId = user.Id,
                Name = user.Nickname,
                Ip = Web.IP,
                UserAgent = Web.Browser
            }, false));

            await UnitOfWork.CommitAsync();

            return(await GetLoginResultAsync(user, result, ApplicationCode.Admin));
        }
コード例 #4
0
        public async Task <IActionResult> TestMessageAsync([FromBody] TestMessage request)
        {
            await MessageEventBus.PublishAsync(new TestMessageEvent1(request));

            await UnitOfWork.CommitAsync();

            return(Success());
        }
コード例 #5
0
        //[AntiDuplicateRequest]
        public async Task <IActionResult> TestMessageAsync([FromBody] TestMessage request)
        {
            Log.Info("测试日志消息Begin");
            await MessageEventBus.PublishAsync(new TestMessageEvent1(request, request.Send));

            if (request.NeedCommit)
            {
                await UnitOfWork.CommitAsync();
            }
            Log.Info("测试日志消息End");
            return(Success());
        }
コード例 #6
0
        static async Task CommonPublish()
        {
            var eventBus    = new MessageEventBus();
            var tokenSource = new CancellationTokenSource();
            var i           = 0;

            while (!tokenSource.IsCancellationRequested)
            {
                await eventBus.PublishAsync("test", new { Time = DateTime.Now, Index = i }, "");

                i++;
            }
        }
コード例 #7
0
        public async Task <IActionResult> TestMessageAsync([FromBody] TestMessage request)
        {
            Logger.LogInformation($"测试Logger消息{nameof(ILogger<TestController>)} - 0: {request.Id}");
            OtherLog
            .Message($"测试Logger消息{nameof(ILog<TestController>)} - 0: {request.Id}")
            .LogInformation();
            Log.Info("测试日志消息Begin");
            await MessageEventBus.PublishAsync(new TestMessageEvent1(request, request.Send));

            if (request.NeedCommit)
            {
                await UnitOfWork.CommitAsync();
            }
            Log.Info("测试日志消息End");
            return(Success());
        }
コード例 #8
0
        public async Task TestMessage1Async(TestMessage message, [FromCap] CapHeader header)
        {
            if (message.ThrowException)
            {
                throw new NotImplementedException("主动触发,暂未生效");
            }
            Log.Info($"测试一波CAP消息 - 0: {message.Id}");
            Logger.LogDebug($"测试一波CAP消息 - 1: {message.Id}");
            var log = Bing.Logs.Log.GetLog(nameof(TestMessageEventHandler));

            log.Debug($"测试一波CAP消息 - 1 - 1: {message.Id}");
            Debug.WriteLine(message.Id);
            await MessageEventBus.PublishAsync(new TestMessageEvent2(message, message.Send));

            if (message.NeedCommit)
            {
                await UnitOfWork.CommitAsync();
            }
        }
コード例 #9
0
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="request">创建请求参数</param>
        public async Task <Guid> CreateAsync(CreateRoleRequest request)
        {
            var role = ToEntity(request);

            role.Type = "Role";
            await RoleManager.CreateAsync(role);

            await MessageEventBus.PublishAsync(new CreateRoleMessageEvent(
                                                   new CreateRoleMessage()
            {
                Code = $"event:{role.Code}",
                Name = $"event:{role.Name}",
                Type = $"event:{role.Type}"
            }));

            await UnitOfWork.CommitAsync();

            return(role.Id);
        }
コード例 #10
0
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="request">创建请求参数</param>
        public async Task <Guid> CreateAsync(CreateRoleRequest request)
        {
            var role = ToEntity(request);

            role.Type = "Role";
            await RoleManager.CreateAsync(role);

            await MessageEventBus.PublishAsync(new CreateRoleMessageEvent(
                                                   new CreateRoleMessage()
            {
                Code = $"event:{role.Code}",
                Name = $"event:{role.Name}",
                Type = $"event:{role.Type}"
            }));

            await SqlExecutor.ExecuteSqlAsync("insert into Systems.Test(Id, Name) Values(@Id, @Name)",
                                              new { Id = Guid.NewGuid(), Name = "隔壁老王" });

            await UnitOfWork.CommitAsync();

            return(role.Id);
        }
コード例 #11
0
 /// <summary>
 /// 等待提交时发送
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="args"></param>
 public void SendMessage <T>(T args) where T : MessageEventArgs, new()
 {
     MessageEventBus.AddWaitCommit <T>(args);
 }
コード例 #12
0
 /// <summary>
 /// 立即发送
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="args"></param>
 public void SendMessageImmediately <T>(T args) where T : MessageEventArgs, new()
 {
     MessageEventBus.Add <T>(args);
 }
コード例 #13
0
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="args"></param>
 protected virtual void SendMessage <T>(T args) where T : MessageEventArgs, new()
 {
     MessageEventBus.Add <T>(args);
 }