乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      對(duì)Asp.net WebApi中異步(async+await)接口實(shí)際使用及相關(guān)思考(示例給出了get,post,提交文件,異步接口等實(shí)踐).

       Coder編程 2021-03-19

      【很多初學(xué)者的疑問(wèn)】

        為何作為web api這樣的天然的并發(fā)應(yīng)用,還需要在controller的action上聲明使用async這些呢?

        <參考解答>

      在 web 服務(wù)器上,.NET Framework 維護(hù)用于處理 ASP.NET 請(qǐng)求的線程池。 當(dāng)請(qǐng)求到達(dá)時(shí),將調(diào)度池中的線程以處理該請(qǐng)求。 如果以同步方式處理請(qǐng)求,則處理請(qǐng)求的線程將在處理請(qǐng)求時(shí)處于繁忙狀態(tài),并且該線程無(wú)法處理其他請(qǐng)求。
      如果請(qǐng)求發(fā)出需要兩秒鐘時(shí)間才能完成的 web 服務(wù)調(diào)用,則該請(qǐng)求將需要兩秒鐘,無(wú)論是同步執(zhí)行還是異步執(zhí)行。 但是,在異步調(diào)用期間,線程在等待第一個(gè)請(qǐng)求完成時(shí)不會(huì)被阻止響應(yīng)其他請(qǐng)求。 因此,當(dāng)有多個(gè)并發(fā)請(qǐng)求調(diào)用長(zhǎng)時(shí)間運(yùn)行的操作時(shí),異步請(qǐng)求會(huì)阻止請(qǐng)求隊(duì)列和線程池的增長(zhǎng)。

      [注]總的來(lái)說(shuō),對(duì)單個(gè)客戶端請(qǐng)求來(lái)說(shuō),它感受到的速度,響應(yīng)時(shí)間并沒(méi)有因?yàn)槭褂卯惒蕉嵘?,但?duì)整個(gè)服務(wù)器來(lái)說(shuō),因?yàn)榫€程在異步場(chǎng)景下等待的同時(shí)還在服務(wù)其它的線程,因此線程數(shù)不會(huì)增長(zhǎng)太快,進(jìn)而不會(huì)輕易達(dá)到繁忙狀態(tài)。

      【給出一個(gè)自己寫(xiě)的分析代碼】

      using ConfigLab.Comp;
      using ConfigLab.Comp.HttpRequestTools.HttpClient;
      using ConfigLab.Comp.MetaData;
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.IO;
      using System.Linq;
      using System.Net;
      using System.Net.Http;
      using System.Text;
      using System.Threading.Tasks;
      using System.Web.Hosting;
      using System.Web.Http;
      
      namespace ConfigLab.WebApiProject.Controllers
      {
          /// <summary>
          /// 功能簡(jiǎn)介:web api中的異步接口體會(huì)
          /// 創(chuàng)建時(shí)間:2020-8-23
          /// 創(chuàng)建人:pcw
          /// 備注:如果不需要所有接口有一個(gè)默認(rèn)的 /api/*中的api這段,需要自行修改RouteConfig.cs中的路由設(shè)置
          /// </summary>
          public class CommonAPIController : ApiController
          {
              /// <summary>
              /// https://localhost:44305/CommonAPI/getTest1?userid=u001
              /// </summary>
              /// <param name="userid"></param>
              /// <returns></returns>
              public string getTest(string userid)
              {
                  return $"test1_result(from web api):userid={userid}";
              }
              /// <summary>
              /// https://localhost:44305/CommonAPI/getTest2
              /// 參數(shù):userid=u002&optype=add
              /// </summary>
              /// <param name="data"></param>
              /// <returns></returns>
              [HttpPost]
              public string postTest([FromBody] userActin data)
              {
                  return $"test2_result(from web api):userid={data.userid},optype={data.optype}";
              }
              /// <summary>
              /// 請(qǐng)求地址:https://localhost:44305/CommonAPI/postListByAsync
              /// 參數(shù):userid=u002&optype=add
              /// 返回:多個(gè)網(wǎng)站的返回值
              /// </summary>
              /// <param name="data"></param>
              /// <returns></returns>
              public async Task<List<ResponseResult>> postListByAsync([FromBody] userActin data)
              {
                  List<ResponseResult> listResult = new List<ResponseResult>();
                  HttpClientAssisterAsync httpAssist = new HttpClientAssisterAsync();//介紹(https://www.cnblogs.com/taohuadaozhu/p/13548266.html),Nuget 搜索安裝: ConfigLab.Comp即可使用
                  Task<ResponseResult> tsk_rrs = httpAssist.SendRequestByGet("http://www.baidu.com");
                  await tsk_rrs;
                  listResult.Add(tsk_rrs.Result);
                  tsk_rrs = httpAssist.SendRequestByGet("http://www.ifeng.com");
                  await tsk_rrs;
                  listResult.Add(tsk_rrs.Result);
                  return listResult;
              }
              /// <summary>
              /// 請(qǐng)求地址:https://localhost:44305/CommonAPI/SaveFile?projectId=p001&userid=u003
              /// 附加參數(shù):文件流
              /// </summary>
              /// <param name="projectId"></param>
              /// <param name="userid"></param>
              /// <returns></returns>
              public async Task<RunResult> SaveFile(string projectId, string userid)
              {
                  RunResult rrs = new RunResult();
                  if (!Request.Content.IsMimeMultipartContent())
                  {
                      //throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                      return new RunResult() { RunCode = -1, RunMsg = "HttpStatusCode.UnsupportedMediaType" };
                  }
                  //string root = Path.Combine(HostingEnvironment.MapPath(ConfigurationManager.AppSettings["FileStorePath"]), DateTime.Now.ToShortDateString(), projectId);
                  string root = Path.Combine(ConfigurationManager.AppSettings["FileStorePath"], DateTime.Now.ToShortDateString(), projectId);
                  if (!Directory.Exists(root)) Directory.CreateDirectory(root);
                  MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root);
                  try
                  {
                      await Request.Content.ReadAsMultipartAsync(provider); 
                      return new RunResult() { RunCode = 0, RunMsg = "" };
                  }
                  catch (Exception ex)
                  {
                      rrs.RunCode = -2;
                      rrs.RunMsg = $"獲取客戶端上傳的文件流失敗,ex.msg={ex.Message},ex.stacktrace={ex.StackTrace}";
                  }
                  return rrs;
              }
          }
          /// <summary>
          /// 測(cè)試用的post參數(shù)對(duì)象
          /// </summary>
          public class userActin
          {
              public string userid { get; set; }
              public string optype { get; set; }
      
          }
      }

       

          

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多