2015-06-30 22 views
17

ho qualche file da caricare e alcuni dei file non è riuscito perché il post è asincrona e non sincrono ..Fai sincrono client http: attendere la risposta

che sto cercando di fare questa chiamata come chiamata sincronizzato ..

Voglio attendere la risposta.

Come posso effettuare questa chiamata come sincrona?

static async Task<JObect> Upload(string key, string url, string 
           sourceFile, string targetFormat) 
{ 
    using (HttpClientHandler handler = new HttpClientHandler { 
              Credentials = new NetworkCredential(key, "") 
             }) 
    using (HttpClient client = new HttpClient(handler)) 
    { 
     var request = new MultipartFormDataContent(); 
     request.Add(new StringContent(targetFormat), "target_format"); 
     request.Add(new StreamContent(File.OpenRead(sourceFile)), 
             "source_file", 
             new FileInfo(sourceFile).Name); 

     using (HttpResponseMessage response = await client.PostAsync(url, 
                  request).ConfigureAwait(false)) 

     using (HttpContent content = response.Content) 
     { 
      string data = await content.ReadAsStringAsync().ConfigureAwait(false); 
      return JsonObject.Parse(data); 
     } 
    } 
} 

Qualsiasi aiuto apprezzato!

+1

Potresti pubblicare ulteriori dettagli sull'errore? L'esecuzione asincrona non dovrebbe causare il fallimento del caricamento. Se proprio devi, puoi utilizzare Task.Wait e controllare il valore restituito per vedere se l'attività è stata completata. https://msdn.microsoft.com/en-us/library/dd235606(v=vs.110).aspx –

+1

Penso che non dovresti usare ConfigureAwait. Si prega di verificare senza ConfigureAwait. – Amit

+0

@rikkigibson continua dopo "attendi" sul thread non elaborato dal pool di thread (senza HttpContext/Cultura o il thread UI configurato correttamente) in genere farà il caso NRE nel chiamante del metodo (nessun codice di chiamata, né errore né framework sono mostrati da OP così difficile da individuare il problema). Come ha sottolineato Amit, "ConfigureAwait' è in genere il modo per far funzionare il codice in modo errato (nella speranza di risolvere altri problemi di sincronizzazione). Cioè qui è la discussione sull'utilizzo di ASP.Net di esso - http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code. –

risposta

1

che dovrebbe farlo:

static async Task<JObect> Upload(string key, string url, string 
          sourceFile, string targetFormat) 
{ 
    using (HttpClientHandler handler = new HttpClientHandler { 
              Credentials = new NetworkCredential(key, "") 
            }) 
    using (HttpClient client = new HttpClient(handler)) 
    { 
     var request = new MultipartFormDataContent(); 
     request.Add(new StringContent(targetFormat), "target_format"); 
     request.Add(new StreamContent(File.OpenRead(sourceFile)), 
            "source_file", 
            new FileInfo(sourceFile).Name); 

     using (HttpResponseMessage response = await client.PostAsync(url,request)) 

     using (HttpContent content = response.Content) 
     { 
      string data = await content.ReadAsStringAsync(); 
      return JsonObject.Parse(data); 
     } 
    } 
} 
+1

senza configureAwait (false), il caricamento non è terminato .. Quindi ho letto questo blog e l'ho aggiunto: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html –

38

cambiamento

await content.ReadAsStringAsync().ConfigureAwait(false) 

a

content.ReadAsStringAsync().Result 

il ReadAsStringAsync restituisce un oggetto Task. il '.Result' alla fine della riga dice al compilatore di restituire la stringa interna.

Problemi correlati