我有一个调用外部Web服务的Web服务。外部服务在3-4秒后响应。
现在所有的调用都是同步的,但是使用异步调用是否有意义(下面的示例)?
它有助于提高性能(不保持线程被阻止)吗?是不是在GetData()的第一行上阻塞了线程?
谢谢。
public class MyService : WebService
{
[WebMethod]
public string GetData()
{
string response = ExecuteRequest(externalUrl, someContent).Result;
return response;
}
private async Task<string> ExecuteRequest(string url, string content)
{
var httpResponse = await new HttpClient().PostAsync(url, new StringContent(content));
string responseStr = await httpResponse.Content.ReadAsStringAsync();
return responseStr;
}
}
答案 0 :(得分:1)
回答你的问题:是的,使用异步调用确实有意义,但你的例子不是异步的。如果你想让它异步,你必须做这样的事情:
<div class="buttons">
<button class="button__form button__one">$10</button>
<button class="button__form button__two">$25</button>
<button class="button__form button__three">$50</button>
<button class="button__form button__four">$100</button>
<button class="button__form button__five">$250</button>
<button class="button__form button__six">$500</button>
</div><!-- /.buttons -->
<div class="bids__amounts">
<div class="bids__amount bids__current">
<p class="bids__note">The current bid is</p>
<h4 class="current__amount">tk-amount</h4>
</div>
<div class="bids__amount bids__new">
<p class="bids__note">Your bid will be</p>
<h4 class="new__amount">tk-amount</h4>
</div><!-- /.bids__amount -->
</div><!-- /.bids__amounts -->