【很多初學(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)求。 [注]總的來(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; } } }
|
|
來(lái)自: Coder編程 > 《待分類(lèi)》