等待呼叫失败,但.Result成功

时间:2019-02-12 06:25:53

标签: .net .net-core async-await console-application

我偶然发现了一个奇怪的问题...

这是一个简单的代码,仅用await调用API

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>

    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  <body>
    <div class="container">
      <table id="example" class="display nowrap" border='1'>
        <thead>
          <tr>
            <th colspan="2">teacher 1</th>
            <th>teacher 2</th>
            <th>teacher 3</th>
            <th colspan="3">teacher 2</th>
            <th>teacher 5</th>
            <th>teacher 6</th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td>Tiger Nixon2</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>Edinburgh61</td>
            <td>Edinburgh62</td>
            <td>Edinburgh63</td>
            <td>2011/04/25</td>
            <td>Edinburgh3,120</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>

当我调试时以及当我越过public async Task<IEnumerable<DTO>> GetAll(int Id, string APIPath) { var client = new HttpClient(); var response = await client.GetAsync($"{APIPath}/Contract/GetAll?Id={Id}"); var responseResult = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject<IEnumerable<DTO>>(responseResult.ToString()); } 行时。应用程序无任何异常崩溃,调试会话结束。

但是当我将await更改为await时,调试会话将继续并保持正常运行,甚至从API返回响应。

我的猜测是,由于崩溃,以某种方式未创建新线程并且调试会话已完成。任何想法,为什么?

更新 这是一个.Net Core控制台应用程序

UPDATE2 不确定它有什么帮助,但是这是主线...

.Result

0 个答案:

没有答案
相关问题