Sono abbastanza sicuro che sto creando un deadlock nella mia applicazione e non sono sicuro di come risolvere il problema. Ho alcune parti mobili e sono nuovo a async
e await
quindi per favore abbiate pazienza con me.Async Deadlock?
Ho un cliente che consente di caricare un file come segue:
public static async Task<string> UploadToService(HttpPostedFile file, string authCode, int id)
{
var memoryStream = new MemoryStream();
file.InputStream.CopyTo(memoryStream);
var requestContent = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(memoryStream.ToArray());
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
requestContent.Add(fileContent, "file", file.FileName);
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(BaseUrl);
httpClient.DefaultRequestHeaders.Accept.Clear();
var message =
await
httpClient.PostAsync(
string.Format("Upload?authCode={0}&id={1}", authCode, id),
requestContent);
return await message.Content.ReadAsStringAsync();
}
}
Il pezzo che riceve il file:
[HttpPost]
public Task<HttpResponseMessage> Upload(string authCode, int id)
{
var request = Request;
var provider = new CustomMultipartFormDataStreamProvider(root);
var task =
request.Content.ReadAsMultipartAsync(provider)
.ContinueWith(o =>
{
// ...
// Save file
// ...
return new HttpResponseMessage()
{
Content = new StringContent("File uploaded successfully"),
StatusCode = HttpStatusCode.OK
};
});
return task;
}
Tutto è iniziato con il seguente:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var file = HttpContext.Current.Request.Files[0];
var response = UploadToService(file, hiddenAuthCode.Value, int.Parse(hiddenId.Value));
}
}
Tutto sembra funzionare, tranne che lo PostAsync
non riconosce mai che task
è stato restituito. Vedo che lo stato del task await ... PostAsync ...
è WaitingForActivation ma non sono completamente sicuro di cosa significhi (ricorda, sono un n00b per questa roba). Il mio file viene salvato nella posizione corretta ma l'applicazione non riconosce mai la risposta dal mio servizio.
Se qualcuno potesse indicarmi la giusta direzione, sarebbe molto apprezzato.
Dopo averlo modificato in "RegisterAsyncTask', ho ottenuto il tipo di argomento System.Threading.Task.Task non è assegnabile al tipo di parametro System.Web.UI.PageAsyncTask'. Ho provato a cambiare il mio tipo di reso, ma questo ha causato altri errori. Qualche idea? –
Joe
@Joe, un altro tweak: 'new PageAsyncTask (() => UploadToService (file, ...))', l'argomento è un 'Func'. –
Noseratio
Grazie! Dopo aver apportato le modifiche, non più deadlock. – Joe