c# - How to await a byte[] -
i have mvc 4 app. 1 of things send email calling service using webclient so:
var serializer = new javascriptserializer(); string requestdata = serializer.serialize(new { eventid = 1, subscriberid = studentid, tolist = loginmodel.emailaddress, templateparamvals = strstudentdetails, }); using (var client = new webclient()) { client.headers[httprequestheader.contenttype] = "application/json"; var result = await client.uploaddata(uri, encoding.utf8.getbytes(requestdata)); } i wanted make use of uploaddataasync got error:
an asynchronous operation cannot started @ time. asynchronous operations may started within asynchronous handler or module or during events in page lifecycle.
so thought of creating wrapper function & making asynchronous while still using uploaddataasync.
private async task<bool> sendemailasync(long studentid, loginmodel loginmodel) { try { string uri = configurationmanager.appsettings["communicationmanagerurl"] + "postuserevent"; var serializer = new javascriptserializer(); string requestdata = serializer.serialize(new { eventid = 1, subscriberid = studentid, tolist = loginmodel.emailaddress, templateparamvals = strstudentdetails, }); using (var client = new webclient()) { client.headers[httprequestheader.contenttype] = "application/json"; var result = await client.uploaddata(uri, encoding.utf8.getbytes(requestdata)); } } catch (exception ex) { // log exceptions ... } } this doesn't build , gives error:
cannot await 'byte[]'
any ideas how solve this?
you can not await synchrone function, have call [..]async one:
var result = await client.uploaddataasync(uri, encoding.utf8.getbytes(requestdata)); the function call returns byte[], of course not awaitable. uploaddataasync returns task<byte[]> instead, 1 want use:
http://msdn.microsoft.com/de-de/library/ms144225(v=vs.110).aspx
edit: method seems return void, kind of awaitable, too.. in terms of fire , forget.
Comments
Post a Comment