c# - Task Was Canceled -


i trying download pdf database api call.. pdf getting downloaded 1 row getting task canceled exception

public async system.threading.tasks.task<actionresult> record(string empno) {     using (var client = new httpclient())     {          client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/pdf"));         var response = client.getasync("url?empno=" + empno).result;          if (response.issuccessstatuscode)         {             var files = directory.getfiles(server.mappath(@"~/emppdf/"));             foreach (var file in files)             {                 try                 {                     system.io.file.delete(file);                 }                 catch                 { }             }             var bytes = await response.content.readasbytearrayasync();               using (stream writer = system.io.file.create(system.web.httpcontext.current.server.mappath(@"~emppdf/" + empno + ".pdf")))             {                 writer.write(bytes, 0, bytes.length);                 writer.flush();                 responsepdfpath = @"/emppdf/" + empno + ".pdf"; //+ response.content.headers.contentdisposition.filename;             }              viewbag.pathtopdf = responsepdfpath;             return view();         }     } } 

i getting task cancelled exception code.

deadlock because of mixing of blocking calls (.result) , async-await code.

 var response = client.getasync("url?empno=" + empno).result; 

that line should awaited

 var response = await client.getasync("url?empno=" + empno); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -