当前位置:编程学习 > C#/ASP.NET >>

C#异步报错。。

/// <summary>
        /// 日常日志记录
        /// </summary>
        /// <param name="LogModel"></param>
        public void AddLog(string logMsg, DateTime? opertionTime, DateTime? responseTime)
        {
            try
            {
                AsyncAddLog asy = new AsyncAddLog(AddLogdele);
                IAsyncResult ia = asy.BeginInvoke(logMsg, opertionTime, responseTime, new AsyncCallback(CallbackMethodAddLog), asy);
            }
            catch { }
        }

        private void AddLogdele(string logMsg, DateTime? opertionTime, DateTime? responseTime)
        {
            if (ConfigurationManager.AppSettings["LogSwitch"] != null && ConfigurationManager.AppSettings["LogSwitch"] == "on")
            {
                BasePage basePage = new BasePage();
                int userID = basePage.CurrentUser.UserID;
                string userName = basePage.CurrentUser.LoginName;
                string bussinessName = "123"; //base.CurrentModule.FULLNAME;
                DateTime recordTime = DateTime.Now;
                string com_Code = string.Empty;
                if (ConfigurationManager.AppSettings["Com_Code"] != null)
                {
                    com_Code = ConfigurationManager.AppSettings["Com_Code"].ToString();
                }
                HttpRequest Request = HttpContext.Current.Request;
                string client_IP = Request.ServerVariables.Get("Remote_Addr").ToString();
                DateTime opertionTimes = DateTime.Now;
                DateTime responseTimes = DateTime.Now;
                if (opertionTime != null)
                {
                    opertionTimes = DateTime.Now;
                }
                if (responseTime != null)
                {
                    responseTimes = Convert.ToDateTime(responseTime);
                }
                LogHelperController log = new LogHelperController();
                log.AddLog(userID, userName, bussinessName, opertionTimes, responseTimes, recordTime, com_Code, client_IP, logMsg);
            }
        }

        /// <summary>
        /// 异步执行完以后回收线程
        /// </summary>
        /// <param name="ar"></param>
        private void CallbackMethodAddLog(IAsyncResult ar)
        {
            AsyncAddLog asy = (AsyncAddLog)ar.AsyncState;
            asy.EndInvoke(ar);
        }

        public delegate void AsyncAddLog(string logMsg, DateTime? opertionTime, DateTime? responseTime);

我加上异步执行以后总是提示我:只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<httpModules> 节中包括 System.Web.SessionStateMod 或自定义会话状态模块。
但是如果我用如下方式,就不会报错:
public void AddLog(string logMsg, DateTime? opertionTime, DateTime? responseTime)
        {
            try
            {
                if (ConfigurationManager.AppSettings["LogSwitch"] != null && ConfigurationManager.AppSettings["LogSwitch"] == "on")
                {
                    BasePage basePage = new BasePage();
                    int userID = basePage.CurrentUser.UserID;
                    string userName = basePage.CurrentUser.LoginName;
                    string bussinessName = "123"; //base.CurrentModule.FULLNAME;
                    DateTime recordTime = DateTime.Now;
                    string com_Code = string.Empty;
                    if (ConfigurationManager.AppSettings["Com_Code"] != null)
                    {
                        com_Code = ConfigurationManager.AppSettings["Com_Code"].ToString();
                    }
                    HttpRequest Request = HttpContext.Current.Request;
                    string client_IP = Request.ServerVariables.Get("Remote_Addr").ToString();
                    DateTime opertionTimes = DateTime.Now;
                    DateTime responseTimes = DateTime.Now;
                    if (opertionTime != null)
                    {
                        opertionTimes = DateTime.Now;
                    }
                    if (responseTime != null)
                    {
                        responseTimes = Convert.ToDateTime(responseTime);
                    }
                    LogHelperController log = new LogHelperController();
                    log.AddLog(userID, userName, bussinessName, opertionTimes, responseTimes, recordTime, com_Code, client_IP, logMsg);
                }
            }
            catch { }
        }
--------------------编程问答-------------------- 你有没有实际测试出来、此时搞asp.net异步页面有什么意义呢? --------------------编程问答-------------------- 页面的cs里不行,就新建类库,里去记日志! --------------------编程问答--------------------
引用 2 楼 sammy_luo 的回复:
页面的cs里不行,就新建类库,里去记日志!

我就是建的类库啊,记日志的方法放到类库里面,然后在页面中调用类库中的方法。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,