zoukankan      html  css  js  c++  java
  • .net mvc webapi使用异步请求处理并发

    model.cs中

     #region 根据学年获取老师在的班级
        public class TeacherClassForYearPara
        {
            public string YearCode { set; get; }
            public string UserCode { set; get; }
    
        }
        public class TeacherClassForYearRet: BasPostMsg
        {
            public string ClassCode { set; get; }
            public string ClassName { set; get; }
    
        }
        #endregion

    repository.cs中

            public async Task<TeacherClassForYearRet> TeacherClassForYear(TeacherClassForYearPara para)
            {
                TeacherClassForYearRet ret = new TeacherClassForYearRet();
                try
                {
    
                    await Task.Run(() =>
                    {
                        if (string.IsNullOrEmpty(para.UserCode))
                            throw new Exception("UserCode不能为空");
                        if (string.IsNullOrEmpty(para.YearCode))
                            throw new Exception("YearCode不能为空");
    
                        DbCommand cmd = db.GetStoredProcCommond("TeacherClassForYear_P");
                        db.AddInParameter(cmd, "@UserCode", DbType.Int32, para.UserCode);
                        db.AddInParameter(cmd, "@YearCode", DbType.String, para.YearCode);
                        DataTable dt = db.ExecuteDataTable(cmd);
                        if (dt.Rows.Count > 0)
                        {
                            ret.ClassCode = dt.Rows[0]["ClassCode"].ToString();
                            ret.ClassName = dt.Rows[0]["ClassName"].ToString();
    
                        }
                        ret.Status = "1";
                        ret.Msg = "请求成功!";
                    }
                );
    
                }
                catch (Exception e)
                {
                    ret.Status = "0";
                    ret.Msg = e.Message;
                }
                return ret;
            }
    

      controller.cs中

     #region 根据学年获取老师在的班级
            [HttpPost]
            public async Task<IHttpActionResult> TeacherClassForYear([FromBody]TeacherClassForYearPara para)
                        => Ok(await repo.TeacherClassForYear(para));
            #endregion
    

      

  • 相关阅读:
    Android 如何处理崩溃的异常
    体验下Xcode5与ios7
    IOS 改变导航栏返回按钮的标题
    android之HttpURLConnection
    android异步加载图片
    android 从服务器上获取APK下载安装
    android AsyncTask异步下载并更新进度条
    android 四种堆状态
    Windows Azure 的开源 DNA
    mysql 只给更新表的某个字段的授权
  • 原文地址:https://www.cnblogs.com/jiamengyang/p/9600042.html
Copyright © 2011-2022 走看看