本文介绍了如何实现返回Task< decimal>的函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是异步/等待概念的新手,我想问一下如何实现此功能...

我想要功能:

公共任务<十进制> GetCurrencyRate(){} 

此功能将调用REST Web服务,如下所示:

 

var baseAddress =新的Uri("https://rest-ws.xxx/"); 使用(var httpClient = new HttpClient {BaseAddress = baseAddress}) { 使用(var response = await httpClient.GetAsync("currencies"))) { 字符串responseData =等待response.Content.ReadAsStringAsync();

//从json解析并返回十进制 } }

GetCurrencyRate()中还有另外2个异步调用.

如何实现我的GetCurrencyRate()的主体,以便正确地异步调用内部代码,并考虑线程等因素对其进行优化.

谢谢

捷克共和国的MirekVanický

解决方案


Hi all,

I am new to async/await concept and I would like to ask how to implement this function...

I want to have function:

public Task<decimal> GetCurrencyRate(){}

This function will call REST web service something like this:

var baseAddress = new Uri("https://rest-ws.xxx/"); using (var httpClient = new HttpClient { BaseAddress = baseAddress }) { using (var response = await httpClient.GetAsync("currencies")) { string responseData = await response.Content.ReadAsStringAsync();

// parsing from json and returning decimal } }

There are 2 other async calls inside GetCurrencyRate().

How to implement body of my GetCurrencyRate() so that code inside is correctly asynchronously called, optimized considering threads etc.

Thank You,

Mirek Vanický, Czech Republic

解决方案


这篇关于如何实现返回Task&lt; decimal&gt;的函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 19:49