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
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。