In this line appears error: "Cannot access a disposed object".
db._fbcontacts.InsertOnSubmit(new FacebookContactsList { Name = FBName });
What can a reason of it?
There is the method where I want to write name of user's account from facebook in localdatabase
public void LoadUserInfo()
{
using (FacebookDataContext db = new FacebookDataContext(DBConnectionstring))
{
FacebookClient fb = new FacebookClient(App.AccessToken);
fb.GetCompleted += (o, e) =>
{
var result = (IDictionary<string, object>)e.GetResultData();
var FBName = String.Format("{0} {1}", (string)result["first_name"], (string)result["last_name"]);
db._fbcontacts.InsertOnSubmit(new FacebookContactsList { Name = FBName });
db.SubmitChanges();
};
fb.GetTaskAsync("me");
}
}
Your callback method is being called after the FacebookDataContext
has been disposed.
This is the code that executes immediately
using (FacebookDataContext db = new FacebookDataContext(DBConnectionstring))
{
FacebookClient fb = new FacebookClient(App.AccessToken);
// no callback method is being executed here!
fb.GetTaskAsync("me");
}
then, sometime later when GetTaskAsync
completes, the following is executed:
var result = (IDictionary<string, object>)e.GetResultData();
/*blah blah snip*/
db._fbcontacts.InsertOnSubmit(new FacebookContactsList { Name = FBName });
db.SubmitChanges();
But you can see from the first code block that, upon exiting the scope of the using
statement, db has already been disposed.
Instead, you'll need to pull db out of the using block and dispose of it manually...
public void LoadUserInfo()
{
FacebookDataContext db = new FacebookDataContext(DBConnectionstring);
FacebookClient fb = new FacebookClient(App.AccessToken);
fb.GetCompleted += (o, e) =>
{
try
{
/*blah fricken blah snipped */
db.SubmitChanges();
}finally{
if(db != null) db.Dispose();
}
};
fb.GetTaskAsync("me");
}
Or you can pull the instantiation of the context inside of the event handler.
public void LoadUserInfo()
{
FacebookClient fb = new FacebookClient(App.AccessToken);
fb.GetCompleted += (o, e) =>
{
/*yadda mfing blah */
using (FacebookDataContext db = new FacebookDataContext(DBConnectionstring))
{
db._fbcontacts.InsertOnSubmit(new FacebookContactsList { Name = FBName });
db.SubmitChanges();
}
};
fb.GetTaskAsync("me");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句