Making a request to Gravatar in an asynchronous matter is very similar to calling the method in the synchronous way only that before we
call the method we need to set a callback first which will handle the retrieval of the response:

The callback handler method will receive the GravatarServiceResponse object as well as a state object passed to the calling method. The state object can also be null
if not needed.

var m_Email = "";
var m_Password = "my password";

var service = new GravatarService(m_Email, m_Password);

service.SetCallBack((res, state) =>
					if (!res.IsError) //no error, check results
						foreach (GravatarAddress adr in res.AddressesResponse) //addresses method will return a addresses list.
							Console.WriteLine("Address: '{0}', active image URL: '{1}'", adr.Name, adr.Image.Url);
					else //error occurred, show info
						Console.WriteLine("Error: {0}) {1}", res.ErrorCode, res.ErrorInfo);

The SetCallBack method returns the service instance allowing us to both set the callback and call the async method in one go.

Besides that, the handling of the response is done exactly the same as it would be for a synchronous call.

Last edited Jul 11, 2010 at 4:00 PM by yoavniran, version 1


No comments yet.