我有一個(gè)API,負(fù)責(zé)在數(shù)據(jù)庫中插入短信詳細(xì)信息。它通過對(duì)存儲(chǔ)庫進(jìn)行同步調(diào)用來實(shí)現(xiàn),我認(rèn)為可以異步實(shí)現(xiàn)該存儲(chǔ)庫,我該如何實(shí)現(xiàn)呢?還是最好的方式來處理這種情況。代碼片段示例受到高度贊賞,因?yàn)槲胰栽诶^續(xù)圍繞.NET進(jìn)行包裝。api:public IHttpActionResult SendSMSNotification([FromBody] SMSNotification smsNotification) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _service.SendSMS(smsNotification); return Ok(); }服務(wù):internal void SendSMS(SMSNotification smsNotification) { _repository.Notify(_mapperService.GetSMSNotification(smsNotification)); }映射器:public SMSNotification GetSMSNotification(SMSNotification message) { return AutoMapper.Mapper.Map<SMSNotification>(message); }回購:public virtual bool Notify(SMSNotification request) { using (var sql = _sqlMapper.CreateCommand('Database', 'Stored proc')) { sql.AddParam("@fMessage", request.Message); //.............. //.............. more params var retvalParamOutput = sql.AddOutputParam("@fRetVal", System.Data.SqlDbType.Int); sql.Execute(); return retvalParamOutput.GetSafeValue<int>() == 1; } }sql這里是一個(gè)自定義的東西,它具有以下方法: public static int Execute(this IDataCommand @this); [AsyncStateMachine(typeof(<ExecuteAsync>d__1))] public static Task<int> ExecuteAsync(this IDataCommand @this);
將常規(guī)方法轉(zhuǎn)換為異步方法
繁華開滿天機(jī)
2021-05-11 17:44:21