public async Task <SignalRDto> FindConnectionByUserId(Guid userId)
        {
            if (userId == null)
            {
                return(null);
            }
            SignalRDto item = null;

            try
            {
                using (var db = new SqlConnection(connectionString))
                {
                    if (db.State != ConnectionState.Open)
                    {
                        await db.OpenAsync();
                    }

                    item = await db.QueryFirstOrDefaultAsync <SignalRDto>("Select * From [dbo].[SignalRConnection] Where [UserId] = @userId", new { userId });

                    _logger.Debug($"SignalR: FindConnectionByUserId; Result: {JsonConvert.SerializeObject(item)}");
                }
            }
            catch (Exception e)
            {
                _logger.Error(e, $"SignalR: FindConnectionByUserId; User: {userId}, Message: {e.Message}.");
            }

            return(item);
        }
        public async Task <int> InsertAsync(SignalRDto entity)
        {
            if (entity == null)
            {
                return(0);
            }

            var result = 0;

            try
            {
                using (var db = new SqlConnection(connectionString))
                {
                    if (db.State != ConnectionState.Open)
                    {
                        await db.OpenAsync();
                    }

                    result = await db.ExecuteAsync($@"INSERT INTO [dbo].[SignalRConnection]
                                                   ([SignalRConnectionId]
                                                   ,[ConnectionID]
                                                   ,[Connected]
                                                   ,[TimeStart]
                                                   ,[TimeEnd]
                                                   ,[UserId]
                                                   ,[StatusConnectionID])
                                             VALUES
                                                   (newid()
                                                   ,@{nameof(entity.ConnectionID)}
                                                   ,@{nameof(entity.Connected)}
                                                   ,@{nameof(entity.TimeStart)}
                                                   ,@{nameof(entity.TimeEnd)}
                                                   ,@{nameof(entity.UserId)}
                                                   ,@{nameof(entity.StatusConnectionID)})", entity);

                    _logger.Debug($"SignalR: InsertAsync; Result: {result}, Entity: {JsonConvert.SerializeObject(entity)}");
                }
            }
            catch (Exception e)
            {
                _logger.Error(e, $"SignalR: InsertAsync; Entity: {JsonConvert.SerializeObject(entity)}, Message: {e.Message}.");
            }

            return(result);
        }
        public async Task SignalRUserConnected(string connectionid, string statusconnectionid, Guid userId)
        {
            var result      = 0;
            var signalRconn = await _signalRRepository.FindConnectionByUserId(userId);

            _logger.Debug($"SignalR: SignalRUserConnected; Start: {connectionid}, {statusconnectionid}, {userId}, Connection: {signalRconn}");
            if (signalRconn == null)
            {
                signalRconn = new SignalRDto
                {
                    SignalRConnectionId = Guid.NewGuid(),
                    ConnectionID        = connectionid,
                    Connected           = true,
                    TimeStart           = DateTime.Now,
                    TimeEnd             = null,
                    UserId             = userId,
                    StatusConnectionID = statusconnectionid
                };

                result = await _signalRRepository.InsertAsync(signalRconn);
            }
            else
            {
                if (!string.IsNullOrEmpty(connectionid) && string.IsNullOrEmpty(statusconnectionid))
                {
                    signalRconn.ConnectionID = connectionid;
                    result = await _signalRRepository.UpdateAsync(signalRconn);
                }
                else
                {
                    signalRconn.StatusConnectionID = statusconnectionid;
                    result = await _signalRRepository.UpdateAsync(signalRconn);
                }
            }
            _logger.Debug($"SignalR: SignalRUserConnected; Result: {result}, Connection: {signalRconn}");
        }
Beispiel #4
0
        private void UpdateProgress(int percentage, string msg, object[] args)
        {
            if (percentage > this.highestPercentageSoFar)
            {
                this.highestPercentageSoFar = percentage;
                this.ignoredCounter++;
                if (this.ignoredCounter > IgnoreThreshold || percentage >= 100)
                {
                    this.ignoredCounter = 0;
                    var message = string.Format(msg, args);

                    var signalRDto = new SignalRDto { message = message, percentage = percentage };

                    this.signalRProgressHub.Invoke<string>("UpdateProgress", signalRDto).ContinueWith(
                        task =>
                            {
                                if (task.IsFaulted)
                                {
                                    this.output.WriteLine(
                                        "There was an error calling send: {0}", task.Exception.GetBaseException());
                                }
                                else
                                {
                                    this.output.WriteLine(task.Result);
                                }
                            });
                }
            }
        }