public void Interceptor(IInvocationContext inputContext)
        {
            //获得当前HttpContext
            var tempHttpContext = GolbalAutofacContainer.GetCurrentHttpContext();

            if (null == inputContext)
            {
                inputContext.Proceed();
            }

            var useloger = LogManager.GetLogger(string.Empty, inputContext.InvocationTarget.GetType());

            if (null == useloger)
            {
                inputContext.Proceed();
            }

            var tempIp = tempHttpContext.Connection.LocalIpAddress;

            //执行前后日志与异常日志
            try
            {
                var tempString = string.Format("IP:{0} 调用 类:{1} 方法:{2}", tempIp == null ? "?" : tempIp.ToString(), inputContext.Method.Name, inputContext.TargetType.Name);
                useloger.Log(LogLevel.Info, tempString);
                inputContext.Proceed();
                tempString = string.Format("IP:{0} 调用 类:{1} 方法:{2} 成功", tempIp == null ? "?" : tempIp.ToString(), inputContext.Method.Name, inputContext.TargetType.Name);
                useloger.Log(LogLevel.Info, tempString);
            }
            catch (Exception ex)
            {
                var tempString = string.Format("IP:{0} 调用 类:{1} 方法:{2} 出现异常{3}", tempIp == null ? "?" : tempIp.ToString(), inputContext.TargetType.Name, inputContext.Method.Name, ex.Message);
                useloger.Log(LogLevel.Error, tempString);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// 获得当前的持久操作接口
        /// </summary>
        /// <returns></returns>
        private static IRespositoryStringKey <MongoDBContext, CacheEntity> GetCacheRespository()
        {
            var tempContext = GolbalAutofacContainer.GetCurrentHttpContext();

            if (null == tempContext)
            {
                return(null);
            }

            var tempCacheRepository = tempContext.RequestServices.GetService(typeof(IRespositoryStringKey <MongoDBContext, CacheEntity>)) as IRespositoryStringKey <MongoDBContext, CacheEntity>;

            if (null == tempCacheRepository)
            {
                return(null);
            }

            return(tempCacheRepository);
        }
        public void Interceptor(IInvocationContext inputContext)
        {
            //获得当前HttpContext
            var tempHttpContext = GolbalAutofacContainer.GetCurrentHttpContext();

            //判断是否可解析
            if (null == tempHttpContext || null == useResloveType)
            {
                inputContext.Proceed();
            }


            DbContext useDbContext = null;

            IEFTransactionTagService useTagService = null;


            //尝试解析服务若失败则直接执行
            try
            {
                useDbContext  = tempHttpContext.RequestServices.GetService(useResloveType) as DbContext;
                useTagService = tempHttpContext.RequestServices.GetService(typeof(IEFTransactionTagService)) as IEFTransactionTagService;
            }
            catch (Exception)
            {
                inputContext.Proceed();
                //执行完返回
                return;
            }

            //若获取会话失败
            if (null == useDbContext)
            {
                inputContext.Proceed();
                //执行完返回
                return;
            }

            //是否使用过事务标签
            bool ifNeedTansaction = true;

            if (null != useTagService)
            {
                ifNeedTansaction = !useTagService.IfContextHasBeenStartTraction(useResloveType);
            }

            IDbContextTransaction tempTransaction = null;

            //尝试打开事务并提交
            try
            {
                //若需打开事务
                if (ifNeedTansaction)
                {
                    tempTransaction = useDbContext.Database.BeginTransaction();

                    if (null != useTagService)
                    {
                        useTagService.SetContextUseTraction(useResloveType);
                    }
                }

                inputContext.Proceed();
                AsyncMethod(useDbContext).Wait();

                //若需打开事务
                if (ifNeedTansaction)
                {
                    tempTransaction.Commit();
                }
            }
            //异常回滚
            catch (Exception)
            {
                if (null != tempTransaction && ifNeedTansaction)
                {
                    tempTransaction.Rollback();
                }

                //若返回值是bool类型的
                if (inputContext.Method.ReturnType == m_useBoolType)
                {
                    inputContext.ReturnValue = false;
                }
                //若返回值是Object类型的
                else if (m_useObjectType.IsAssignableFrom(inputContext.Method.ReturnType))
                {
                    inputContext.ReturnValue = null;
                }
            }
        }