• .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
    

      

  • 相关阅读:
    测试驱动开发的意义何在
    Web自动化测试模式page object的小利器:gizmo
    在NANT使用Nunit2标签运行Nunit测试
    小试牛刀 Ruby on Rails
    敏捷回顾会议的思考
    ThoughtWorks技术校园行第二波 课程资料 CleanCode&DirtyCode
    从git merge 和 git rebase想到……
    Ruby中的深浅拷贝
    NUnit Extension小介绍
    如何写好的测试呢?
  • 原文地址:https://www.cnblogs.com/jiamengyang/p/9600042.html
Copyright © 2020-2023  润新知