GetAwaiter和GetResult

这两个用法其实我也还是没有怎么搞懂,先记录下吧

 HttpContent content = new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
 var myhttpclient = _myhttpclientfactory.CreateClient();
 var message = Task.Run(() =>
 {
     return myhttpclient.PostAsync(url, content);
 });
 message.Wait();
 _Database.Add(new jsonlog
 {
         Time = Now.DateTime,
         Fk = Id,
         Json = JsonConvert.SerializeObject(o),
         Pk = System.Guid.NewGuid(),         
         Msg = $ "{OrderNum}"
 });
 _Database.SaveChanges();
 if(message.Result.IsSuccessStatusCode)
 {
     var rr = message.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
     _Database.OmsEcjsonlog.Add(new OmsEcjsonlog
     {
             CreateDateTime = Now.DateTime,
             Fk = Id,
             Json = rr,
             Pk = System.Guid.NewGuid(),            
             Msg = $ "{OrderNum}"
     });
     _Database.SaveChanges();
     var j = JObject.Parse(rr);   
     string flag = j["response"]["success"].ToString();
     string msg = j["response"]["msg"].ToString();
     if(flag.ToLower() == "true" || msg == "ok")
     {
         using(var tran = _OMSECDatabase.Database.BeginTransaction())
         {
             try
             {
                 tran.Commit();
                 i = 1;
             }
             catch(Exception ex)
             {
                 if(tran != null) tran.Rollback();
                 throw new Exception($ "SO Cancel Tran Failed,{ex.Message}");
             }
         }
     }
     else
     {}
 }

找了两个相关链接 A B 微软DOC

posted @ 2020-08-10 11:43  ProZkb  阅读(537)  评论(0编辑  收藏  举报