コード例 #1
0
        /// <summary>
        /// Opens a session for an user (creates a new user or connects an existing user).
        /// </summary>
        /// <param name="userName">The user name.</param>
        /// <returns>The <see cref="ConnectionResult"/>.</returns>
        public ConnectionResult OpenSession(string userName)
        {
            return(this.ManageException(
                       action: () =>
            {
                if (this.store.UserExist(userName: userName))
                {
                    User user = this.store.LoadUser(userName: userName);
                    if (user.State != Model.UserState.Offline)
                    {
                        throw FaultExceptionHelper.From(error: ErrorType.AlreadyConnectedUser, exception: null);
                    }
                }

                this.currentUserId = this.store.AddOrUpdateUser(user: new User {
                    UserName = userName
                });
                this.store.UpdateUserState(userId: this.currentUserId, state: Model.UserState.Online);
                this.notificationManager.NotifyUserStateChange(userId: this.currentUserId, state: Model.UserState.Online);
                return new ConnectionResult {
                    UserId = this.currentUserId
                };
            },
                       description: "OpenSession"));
        }
コード例 #2
0
        /// <summary>
        /// Updates the profile of an <see cref="UserContract"/>.
        /// </summary>
        /// <param name="user">The <see cref="UserContract"/>.</param>
        public void UpdateMyProfile(UserContract user)
        {
            this.ManageException(
                action: () =>
            {
                if (user == null)
                {
                    throw new InvalidOperationException(message: "user can't be null");
                }

                if (user.UserId <= 0)
                {
                    throw new InvalidOperationException(message: "unknown user");
                }

                if (user.UserId != this.currentUserId)
                {
                    throw new InvalidOperationException(message: "not allowed to update profile");
                }

                if (this.store.UserExist(userName: user.UserName))
                {
                    var knownUser = this.store.LoadUser(userName: user.UserName);
                    if (knownUser.UserId != user.UserId)
                    {
                        throw FaultExceptionHelper.From(error: ErrorType.AlreadyConnectedUser, exception: null);
                    }
                }

                this.store.AddOrUpdateUser(user: user.FromContract());
                this.notificationManager.NotifyUserChange(userId: user.UserId);
            },
                description: "UpdateMyProfile");
        }
コード例 #3
0
        private TResult ManageException <TResult>(Func <TResult> action, string description) where TResult : class
        {
            if (action == null)
            {
                throw new ArgumentNullException(paramName: nameof(action));
            }

            if (description == null)
            {
                throw new ArgumentNullException(paramName: nameof(description));
            }

            long trackInfo = this.sessionId.GetHashCode();

            this.logger.Format(
                severity: SeverityLevel.Info,
                trackInfo: trackInfo,
                logMessageFormat: "Calling client service for method {0} ...",
                args: description);

            try
            {
                TResult result = action();

                this.logger.Format(
                    severity: SeverityLevel.Info,
                    trackInfo: trackInfo,
                    logMessageFormat: "Call client service for method {0} complete ...",
                    args: description);

                return(result);
            }
            catch (FaultException <ChatServiceError> exception)
            {
                this.logger.WriteException(
                    exception: exception,
                    trackInfo: trackInfo,
                    severity: SeverityLevel.Error,
                    logMessage: "Exception thrown to client from service method");
                throw;
            }
            catch (FaultException exception)
            {
                this.logger.WriteException(
                    exception: exception,
                    trackInfo: trackInfo,
                    severity: SeverityLevel.Error,
                    logMessage: "Exception without details thrown to client from service method");

                throw FaultExceptionHelper.From(exception: exception);
            }
            catch (ArgumentOutOfRangeException exception)
            {
                this.logger.WriteException(
                    exception: exception,
                    trackInfo: this.sessionId.GetHashCode(),
                    severity: SeverityLevel.Error,
                    logMessage: "Unknown ID Exception exception thrown to client from method, converting it to fault");

                throw FaultExceptionHelper.From(error: ErrorType.InvalidArgument, exception: exception);
            }
            catch (InvalidOperationException exception)
            {
                this.logger.WriteException(
                    exception: exception,
                    trackInfo: this.sessionId.GetHashCode(),
                    severity: SeverityLevel.Failure,
                    logMessage: "Exception thrown to client from service method, converting it to fault");

                throw FaultExceptionHelper.From(exception: exception);
            }
            catch (Exception exception)
            {
                this.logger.WriteException(
                    exception: exception,
                    trackInfo: this.sessionId.GetHashCode(),
                    severity: SeverityLevel.Failure,
                    logMessage: "Unhandled exception catched from service method, converting it to fault");

                throw FaultExceptionHelper.From(exception: exception);
            }
        }