2013-08-08 2 views
3

Mac OS X의 Mono (3.2.1)에서이 간단한 테스트를 실행하면 콘솔에 응답을 인쇄하지 않고 대신 Shutting down finalizer thread timed out.
이 코드에 문제가 있습니까? 아니면 내 모노 오작동?Mono에서 간단한 HttpClient 테스트가 실패 함

using System; 
using System.Net.Http; 

namespace VendTest 
{ 
    class MainClass 
    { 
     public static void Main(string[] args) 
     { 
      Client client = new Client(); 
      client.HttpClientCall(); 
     } 
    } 

    public class Client 
    { 
     HttpClient client; 

     public Client() 
     { 
      client = new HttpClient(); 
     } 

     public async void HttpClientCall() 
     { 
      HttpClient httpClient = new HttpClient(); 
      HttpResponseMessage response = await httpClient.GetAsync("http://vendhq.com"); 
      string responseAsString = await response.Content.ReadAsStringAsync(); 
      Console.WriteLine(responseAsString); 
     } 
    } 
} 

답변

7

async void 메쏘드를 거의 사용하지 않아야합니다. Main()HttpClientCall()가 실제로 완료되기 전에 종료됩니다. Main()을 종료하면 전체 응용 프로그램이 종료되므로 아무 것도 인쇄되지 않습니다.

Main()에 대해 방법을 async TaskWait()으로 변경해야합니다. (종종 교착 상태로 이어질 수 awaitWait() 혼합하지만, 콘솔 응용 프로그램에 적합한 솔루션 입니다.)

class MainClass 
{ 
    public static void Main() 
    { 
     new Client().HttpClientCallAsync().Wait(); 
    } 
} 

public class Client 
{ 
    HttpClient client = new HttpClient(); 

    public async Task HttpClientCallAsync() 
    { 
     HttpClient httpClient = new HttpClient(); 
     HttpResponseMessage response = await httpClient.GetAsync("http://vendhq.com"); 
     string responseAsString = await response.Content.ReadAsStringAsync(); 
     Console.WriteLine(responseAsString); 
    } 
} 
+0

지식이 애매한 부분에 대한 감사합니다! 지금 그것은 작동합니다 :) –

관련 문제