Code like the following.. where there are many async methods queued up.... end up with the global static application context... because the context gets rolled back in the method below...
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
var tweet = TweetAsync.PublishTweet(message);
if (tweet == null)
{
logger.Error(string.Format("Twitter tweet of message for user id {0} failed, exception description: {1}, status code: {2}, details: {3}.",
accessToken, ExceptionHandler.GetLastException().TwitterDescription,
ExceptionHandler.GetLastException().StatusCode,
ExceptionHandler.GetLastException().TwitterExceptionInfos.First().Message ));
// just continue with notification processing and log errors about Twitter
}
});
public void ExecuteOperationWithCredentials(IOAuthCredentials credentials, Action operation)
{
var initialCredentials = CurrentThreadCredentials;
CurrentThreadCredentials = credentials;
operation();
__CurrentThreadCredentials = initialCredentials;__
}