c#抛出异常请求多次执行

时间:2017-02-03 12:03:52

标签: c# .net interceptor castle-dynamicproxy

当抛出异常时,控制进入catch块,然后再次进入控制器操作,请求来自此处,并重新执行ajax调用3到4次,我已检查了ajax调用它只发送了一次请求。内容是调用的函数,它可能因为拦截器而发生

 public ActionResult Login(LoginModel model)
        {
            string message = Resources.LoginInformationIncorrect;
            bool success = false;
            AuthenticationStatus status;
            var hashPass = EncodePassword(model.Password);
           status = _formsAuthProvider.SignIn(model.Username, hashPass);

       }


        public AuthenticationStatus SignIn(string username, string password)
        {
            var status = AuthenticationStatus.Failed;
            AuthenticationResultBo result = null;

            result = AuthenticateUser(username, password);
        }

        public AuthenticationResultBo AuthenticateUser(string username, string password)
        {
            var status = AuthenticationStatus.Failed;
            var failedLoginAttempt = 0;
            // authenticate user
            var credential = GetUserCredentials(username, password);
       }

        public UserBo GetUserCredentials(string username, string password)
        {

            using (var cmd = DbUtility.CreateSpCommand("[GetUserCredentialsForLogin]"))
            {

                cmd.Parameters.Add("@username", SqlDbType.NVarChar, 20).Value = username ?? SqlString.Null;
                cmd.Parameters.Add("@password", SqlDbType.NVarChar, 32).Value = password ?? SqlString.Null;

                var result = new UserBo();
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                        result = Mapper.Map<IDataReader, IEnumerable<UserBo>>(reader).FirstOrDefault();
                    reader.Close();
                }
                //throwing exception manually
                //throw new System.InvalidOperationException("Fake exception");
                return result;

            }
        }





        public class TryCatchLoggingInterceptor : IInterceptor
        {
            public void Intercept(IInvocation invocation)
            {
                try
                {
                    LogManager.Info("Call:" + invocation.TargetType.Name + "::" + invocation.Method.Name);
                    var startTime = DateTime.Now;
                    invocation.Proceed();
                    var endTime = DateTime.Now;
                    LogManager.Info("CallEnd:" + invocation.TargetType.Name + "::" + invocation.Method.Name + " Execution Time(Milliseconds): " + (endTime - startTime).Milliseconds);
                }
                catch (Exception ex)
                {                
                    //HttpContext.Current.Request.Abort();
                    var builder = new StringBuilder();
                    var dataSource = ConfigurationManager.ConnectionStrings["dbMain"].ToString().Split(';')[0];

                    builder.AppendLine();
                    builder.AppendLine(string.Concat(Enumerable.Repeat(">", 50)));
                    builder.AppendLine("Server Time:-" + DateTime.Now);
                    //builder.AppendLine("Requested Url:" + HttpContext.Current.Request.Url);
                    builder.AppendLine(dataSource);
                    builder.AppendLine("Error at " + invocation.TargetType.Name + "::" + invocation.Method.Name + "(" + JsonConvert.SerializeObject(invocation.Arguments, Formatting.Indented) + ")");
                    builder.AppendLine(string.Concat(Enumerable.Repeat("-", 50)));
                    builder.AppendLine(JsonConvert.SerializeObject(ex, Formatting.Indented).Replace("\\n", System.Environment.NewLine));
                    builder.AppendLine(string.Concat(Enumerable.Repeat("<", 50)));

                    LogManager.Error(builder.ToString());
                    LogManager.SendMail(ex, builder.ToString());

                    throw;
                }           
            }
        }

0 个答案:

没有答案