首页
论坛
新职位
搜索论坛
什么是新的
新职位
新的个人资料帖子
最新活动
会员
目前的访客
新的个人资料帖子
搜索个人资料帖子
VB.NET社区
登录
寄存器
什么是新的
搜索
搜索
仅搜索标题
通过:
新职位
搜索论坛
Menu
Log in
Register
安装应用
安装
首页
论坛
ASP.NET
网页服务
为什么不将错误写入数据库?
您正在使用过期的浏览器。它可能无法正确显示此网站或其他网站。
您应该升级或使用
替代浏览器
.
回复主题
信息
<blockquote data-quote="raysefo" data-source="post: 11781" data-attributes="member: 11575"><p>Thank you [USER=11331]@Sheepings[/USER]. </p><p></p><p>Yes, I am using http client in order to call 3rd party rest API. Here is how I am calling 3rd party rest API in my code:</p><p>[CODE=csharp]namespace BusinessService.Utility</p><p>{</p><p> public class Utilities</p><p> {</p><p> private static readonly HttpClient _httpClient = new HttpClient();</p><p></p><p> //some code here</p><p> </p><p> public static async Task<HttpResponseMessage> CallRazer(GameRequest gameRequest, string url)</p><p> {</p><p> try</p><p> {</p><p> FormUrlEncodedContent content = null;</p><p></p><p> if (url == "Product/")</p><p> {</p><p> try</p><p> {</p><p> //Transform GameRequest into ProductDTO</p><p> var config =</p><p> new MapperConfiguration(cfg => { cfg.CreateMap<GameRequest, ProductRequestDto>(); });</p><p> var iMapper = config.CreateMapper();</p><p> var productRequest = iMapper.Map<GameRequest, ProductRequestDto>(gameRequest);</p><p></p><p> //Convert request</p><p> var keyValues = productRequest.ToKeyValue();</p><p> content = new FormUrlEncodedContent(keyValues);</p><p></p><p> //Call 3rd party API</p><p> var response = await _httpClient.PostAsync("//test.com/" + url, content);</p><p> return response;</p><p> }</p><p> catch (Exception e)</p><p> {</p><p> Console.WriteLine(e);</p><p> throw;</p><p> }</p><p> finally</p><p> {</p><p> content.Dispose();</p><p> }</p><p> }</p><p> else</p><p> {</p><p> try</p><p> {</p><p> //Convert request</p><p> var keyValues = gameRequest.ToKeyValue();</p><p> content = new FormUrlEncodedContent(keyValues);</p><p></p><p> //Call 3rd party API</p><p> var response = await _httpClient.PostAsync("//test.com/" + url, content);</p><p> return response;</p><p> }</p><p> catch (Exception e)</p><p> {</p><p> Console.WriteLine(e);</p><p> throw;</p><p> }</p><p> finally</p><p> {</p><p> content.Dispose();</p><p> }</p><p> }</p><p> }</p><p> catch (Exception e)</p><p> {</p><p> Console.WriteLine(e);</p><p> throw;</p><p> }</p><p> }</p><p></p><p> </p><p> }</p><p></p><p> public static class ObjectExtensions</p><p> {</p><p> public static IDictionary<string, string> ToKeyValue(this object metaToken)</p><p> {</p><p> if (metaToken == null)</p><p> {</p><p> return null;</p><p> }</p><p></p><p> JToken token = metaToken as JToken;</p><p> if (token == null)</p><p> {</p><p> return ToKeyValue(JObject.FromObject(metaToken));</p><p> }</p><p></p><p> if (token.HasValues)</p><p> {</p><p> var contentData = new Dictionary<string, string>();</p><p> </p><p> foreach (var child in token.Children().ToList())</p><p> {</p><p> var childContent = child.ToKeyValue();</p><p> if (childContent != null)</p><p> {</p><p> contentData = contentData.Concat(childContent)</p><p> .ToDictionary(k => k.Key, v => v.Value);</p><p> }</p><p> }</p><p></p><p> return contentData;</p><p> }</p><p></p><p> var jValue = token as JValue;</p><p> if (jValue?.Value == null)</p><p> {</p><p> return null;</p><p> }</p><p></p><p> var value = jValue?.Type == JTokenType.Date</p><p> ? jValue?.ToString("o", CultureInfo.InvariantCulture)</p><p> : jValue?.ToString(CultureInfo.InvariantCulture);</p><p></p><p> return new Dictionary<string, string> {{token.Path, value}};</p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p><p></p><p>And here is how I call CallRazer:</p><p>[CODE=csharp]namespace BusinessService</p><p>{</p><p> public class GameServices : IGameServices, IDisposable</p><p> {</p><p> private readonly UnitOfWork _unitOfWork;</p><p></p><p> public GameServices(UnitOfWork unitOfWork)</p><p> {</p><p> _unitOfWork = unitOfWork;</p><p> }</p><p></p><p> public async Task<HttpResponseMessage> GamePurchase(RequestDto requestDto)</p><p> {</p><p> HttpResponseMessage response = null;</p><p> using (_unitOfWork)</p><p> {</p><p> response = await CallRazerService(requestDto);</p><p> return response;</p><p> }</p><p> }</p><p></p><p> private async Task<HttpResponseMessage> CallRazerService(RequestDto requestDto)</p><p> {</p><p> HttpResponseMessage response = null;</p><p></p><p> //Transform DTO into GameRequest for calling Razer Initiate</p><p> var config = new MapperConfiguration(cfg =></p><p> {</p><p> cfg.CreateMap<RequestDto, GameRequest>();</p><p> cfg.CreateMap<GameRequest, GameConfirmRequest>();</p><p> cfg.CreateMap<GameConfirmResponse, GameConfirmResponseDto>();</p><p> cfg.CreateMap<Coupon, CouponDto>();</p><p> cfg.CreateMap<GameRequest, GameRequestDto>();</p><p> });</p><p> var iMapper = config.CreateMapper();</p><p> var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);</p><p></p><p> //Unique reference ID</p><p> gameRequest.referenceId = Guid.NewGuid();</p><p></p><p> var gameRequestDto = iMapper.Map<GameRequest, GameRequestDto>(gameRequest);</p><p></p><p> //Create signature</p><p> gameRequest = Utilities.CreateSignature(gameRequestDto, RequestType.Initiate);</p><p></p><p> //Set service</p><p> gameRequest.service = "RAZER";</p><p></p><p> //Add initiation request into database</p><p> _unitOfWork.GameRepository.Insert(gameRequest);</p><p></p><p> #region Call Razer initiate/confirm</p><p></p><p> //Call Razer for initiation</p><p> response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");</p><p></p><p> //Read response</p><p> var htmlResponse = await response.Content.ReadAsStringAsync();</p><p> var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);</p><p></p><p> //Adding initiation response into database</p><p> _unitOfWork.GameResponseRepository.Insert(gameResponse);</p><p></p><p> if (gameResponse.initiationResultCode == "00")</p><p> {</p><p> gameRequestDto = iMapper.Map<GameRequest, GameRequestDto>(gameRequest);</p><p> gameRequestDto.validatedToken = gameResponse.validatedToken;</p><p> //Create signature</p><p> var gameConfirmRequest = Utilities.CreateSignature(gameRequestDto, RequestType.Confirm);</p><p></p><p> //Transform DTO into GameRequest for calling Razer Initiate</p><p> var gameConfirmRequests = iMapper.Map<GameRequest, GameConfirmRequest>(gameConfirmRequest);</p><p></p><p> //Add confirm request into database</p><p> _unitOfWork.GameConfirmRequestRepository.Insert(gameConfirmRequests);</p><p></p><p> //Call Razer for confirm</p><p> response = await Utilities.CallRazer(gameConfirmRequest, "purchaseconfirmation");</p><p></p><p> //Read response</p><p> htmlResponse = await response.Content.ReadAsStringAsync();</p><p> var gameConfirmResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(htmlResponse);</p><p></p><p> //Set service</p><p> gameConfirmResponse.service = "RAZER";</p><p> //Add confirm response into database</p><p> _unitOfWork.GameConfirmResponseRepository.Insert(gameConfirmResponse);</p><p> }</p><p></p><p> #endregion</p><p></p><p> await _unitOfWork.SaveAsync();</p><p></p><p> return response;</p><p> }</p><p></p><p> </p><p> public void Dispose()</p><p> {</p><p> _unitOfWork.Dispose();</p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p></blockquote><p></p>
Insert quotes…
验证
发表回复
首页
论坛
ASP.NET
网页服务
为什么不将错误写入数据库?
本网站使用Cookie来帮助个性化内容,调整您的体验并在注册时保持登录状态。
继续使用本网站,即表示您同意我们使用cookie。
接受
了解更多…
最佳
底部