如何实现返回Task< decimal>的函数. [英] How to implement function returning Task<decimal>

查看:109
本文介绍了如何实现返回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ý

解决方案

没有太多其他东西了.

您只需要使用async关键字来声明您的方法是异步的(这使您可以在内部使用"await"关键字来调用其他异步方法):

公共异步任务<十进制> GetCurrencyRate()
{
   //您的代码在这里
} 

在方法内部,您仅使用已有的代码,即可在其中等待其他异步方法调用.

真正要记住的唯一事情是,您必须使用一路异步"功能.如前所述 此处.

该文章还描述了"ConfigureAwait"的使用,这对记住很有用.从本质上讲,.net异步/等待代码默认情况下是在同一线程上(或始终在某些上下文中)等待任何调用之后继续执行代码.在GUI代码中,这 是很重要的,但是如果您要编写后端非GUI代码,则这是一个低效率的问题,因此,您通常希望在可能的所有地方都使用ConfigureAwait(false).

如本例所示(摘自上面链接中的文章):

异步任务MyMethodAsync()
{
  //这里的代码在原始上下文中运行.
  等待Task.Delay(1000);
  //这里的代码在原始上下文中运行.
  等待Task.Delay(1000). ConfigureAwait(
    ContinueOnCapturedContext:false);
  //这里的代码没有原始代码
  //上下文(在这种情况下,在线程池上).
} 


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

解决方案

There isn't much else to it.

You just have to use the async keyword to declare that your method is asynchronous (this enables you to use the 'await' keyword inside to call other async methods):

public async Task<decimal> GetCurrencyRate()
{
   // your code goes here
}

Inside the method you just use the code you already have, where you await other async method calls.

The only thing to remember really is that you have to use "async all the way" as described here.

That article also describes the use of "ConfigureAwait", which is useful to remember. Essentially, the .net async/await code defaults to continuing your code after any awaited calls on the same thread (or some context anyway). In GUI code, this is important, but if you are writing back-end non-GUI code this is a point of inefficiency, so you generally want to use ConfigureAwait(false) everywhere that you can.

As in this example (copied from the article in the above link):

async Task MyMethodAsync()
{
  // Code here runs in the original context.
  await Task.Delay(1000);
  // Code here runs in the original context.
  await Task.Delay(1000).ConfigureAwait(
    continueOnCapturedContext: false);
  // Code here runs without the original
  // context (in this case, on the thread pool).
}


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆