c# - Accessing Result property of a Task -


i trying retrieve id of album using c# facebook sdk. getting below error:

system.threading.tasks.task' not contain definition 'result' , no extension method 'result' accepting first argument of type 'system.threading.tasks.task' found

please see below code, error occurs on foreach line

try {     string wallalbumid = string.empty;     facebookclient client = new facebookclient(accesstoken);     client.gettaskasync(pageid + "/albums")         .continuewith(task =>             {                 if (!task.isfaulted)                 {                     foreach (dynamic album in task.result.data)                     {                         if (album["type"] == "wall")                         {                             wallalbumid = album["id"].tostring();                         }                     }                 }                 else                 {                     throw new dataretrievalexception("failed retrieve wall album id.", task.exception.innerexception);                 }             });     return wallalbumid; } 

for record, facebookclient.gettaskasync method returns task<object>

when compile code, 2 errors, first 1 one mentioned, , second 1 is:

'object' not contain definition 'data' , no extension method 'data' accepting first argument of type 'object' found

this second error actual error: task.result object, (i assume) want treat dynamic. because of error, compiler tries use overload of continuewith() uses task, not task<object>, why you're also getting first error.

to fix error, should cast task.result dynamic:

dynamic result = task.result; foreach (dynamic album in result.data) 

this compile fine, won't work, because set local variable after return enclosing method.

if you're using c# 5.0, should use await here, instead of continuewith():

try {     dynamic result = await client.gettaskasync(pageid + "/albums");     foreach (dynamic album in result.data)     {         if (album["type"] == "wall")         {             return (string)album["id"].tostring();         }     }     return string.empty; } catch (exception e) // should use specific exception here, i'm not sure {     throw new dataretrievalexception("failed retrieve wall album id.", e); } 

if can't use c# 5.0, whole method should return task<string> that's returned continuewith():

return client.gettaskasync(pageid + "/albums")       .continuewith(           task =>           {               if (!task.isfaulted)               {                   dynamic result = task.result;                   foreach (dynamic album in result.data)                   {                       if (album["type"] == "wall")                       {                           return (string)album["id"].tostring();                       }                   }                   return string.empty;               }               else               {                   throw new dataretrievalexception(                       "failed retrieve wall album id.", task.exception.innerexception);               }           }); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -