Beispiel #1
0
        public void Execute(IJobExecutionContext context)
        {
            var sw = Stopwatch.StartNew();
            IPersistanceTransaction transaction = null;
            HttpResponseMessage     result      = null;

            try
            {
                using (var conn = RepositoryManager.GetNewConnection())
                {
                    transaction = conn.BeginTransaction();

                    ProcessBusiness(context);

                    Log.Info("Commit transaction!");
                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                Log.Error("Unhandled error", ex);

                if (transaction != null)
                {
                    Log.Warn("Rollback transaction!");
                    transaction.Rollback();
                }
            }

            Log.InfoFormat("Finish processing request, cost {0} milliseconds.", sw.ElapsedMilliseconds);
        }
Beispiel #2
0
        public static TResponse Process <TRequest, TResponse>(TRequest request, IOperation <TRequest, TResponse> processor)
            where TResponse : ResponseBase, new()
            where TRequest : RequestBase, new()
        {
            IPersistanceTransaction transaction = null;
            TResponse result = new TResponse();
            var       sw     = Stopwatch.StartNew();

            // 1. get session
            using (var conn = RepositoryManager.GetNewConnection())
                using (transaction = conn.BeginTransaction())
                {
                    try
                    {
                        // 2. authentication
                        Log.Info("Check user credential.");
                        var authenticaitionResultCode = NinjectBinder.Get <IAuthenticaitionService>().Authenticate(request.Username, request.Password);
                        if (authenticaitionResultCode != AuthenticaitionResultCode.Ok)
                        {
                            result.ResultType = ResultTypes.AuthenticationError;
                            result.Messages   = new String[] { "User password is incorrect" };
                            return(result);
                        }

                        // business
                        result = processor.Process(request);

                        // transaction commit
                        Log.Info("Commit transaction!");
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        result.ResultType = ResultTypes.UnknownError;

                        // transaction rollback
                        if (transaction != null)
                        {
                            Log.Warn("Rollback transaction!");
                            transaction.Rollback();
                        }
                    }
                    finally
                    {
                        Log.InfoFormat("Finish processing {0}, cost {1} milliseconds", processor.ToString(), sw.ElapsedMilliseconds);

                        // release session
                    }
                }

            return(result);
        }
Beispiel #3
0
        public static HttpResponseMessage Process <T>(T t, OperationCodes operationCode, Func <HttpResponseMessage> fun, ApiController controller)
        {
            Log.InfoFormat("{0} {1}", controller.Request.Method, controller.Request.RequestUri);
            var sw = Stopwatch.StartNew();
            IPersistanceTransaction transaction = null;
            HttpResponseMessage     result      = null;

            try
            {
                using (var conn = RepositoryManager.GetNewConnection())
                {
                    transaction = conn.BeginTransaction();

                    var identify = controller.User.Identity as BasicAuthenticationIdentity;
                    if (identify != null && !Authencate(identify))
                    {
                        throw new AuthenticationException("Authorization has been denied for this request.");
                    }

                    result = fun();

                    LogOperation(operationCode, controller);

                    Log.Info("Commit transaction!");
                    transaction.Commit();
                }
            }
            catch (AuthenticationException ex)
            {
                Log.Error("AuthenticationException error", ex);
                result = controller.Request.CreateErrorResponse(System.Net.HttpStatusCode.Unauthorized, ex.Message, ex);
            }
            catch (Exception ex)
            {
                Log.Error("Unhandled error", ex);

                if (transaction != null)
                {
                    Log.Warn("Rollback transaction!");
                    transaction.Rollback();
                }

                result = controller.Request.CreateErrorResponse(System.Net.HttpStatusCode.InternalServerError, "Internal Error", ex);
            }

            Log.InfoFormat("Response: {0}", result);
            Log.InfoFormat("Finish processing request, cost {0} milliseconds.", sw.ElapsedMilliseconds);
            return(result);
        }
Beispiel #4
0
        public static TResult Process <T, TResult>(T tParameters, Func <TResult> fun)
            where TResult : ResponseBase, new()
        {
            var sw = Stopwatch.StartNew();
            IPersistanceTransaction transaction = null;

            try
            {
                using (var conn = RepositoryManager.GetNewConnection())
                {
                    transaction = conn.BeginTransaction();

                    var result = fun();

                    Log.Info("Commit transaction!");
                    transaction.Commit();

                    return(result);
                }
            }
            catch (Exception ex)
            {
                Log.Error("Unhandled error", ex);

                if (transaction != null)
                {
                    Log.Warn("Rollback transaction!");
                    transaction.Rollback();
                }

                return(new TResult()
                {
                    ResultType = ResultTypes.UnknownError, Messages = new[] { "Internal error" }
                });
            }
            finally
            {
                Log.InfoFormat("Finish processing request, cost {0} milliseconds.", sw.ElapsedMilliseconds);
            }
        }