Reputation: 3060
I want to convert a very static DownloadString()
call to a DownloadStringAsync()
, but can't get the flow as I'd like.
Currently, I do the following in lots of places:
string apiurl = "http://api.gowalla.com/spots/" + spot;
var entries = JsonConvert.DeserializeObject<SpotInfo>(CallGowalla(apiurl));
return entries;
With the CallGowalla
looking like so:
private static string CallGowalla(string apiurl)
{
var wc = new WebClient();
wc.Headers["Accept"] = "application/json";
wc.Headers["ContentType"] = "application/json";
wc.Headers["Authorization"] = CreateAuthorizationString();
wc.Headers["-Gowalla-API-Key"] = ConfigurationManager.AppSettings["api"].ToString();
var raw = wc.DownloadString(apiurl);
return raw;
}
I'm getting hangups on the the calls, so I want to move the DownloadString
to DownloadStringAsync
and I'm not sure where to start without ripping apart all the calls to CallGowalla
throughout.
Any ideas?
Suggestions on architecture and refactoring welcome also...
Upvotes: 1
Views: 718
Reputation: 14579
You probably want to think about how you are calling the CallGowalla() method. It is probably easier to start calling this method in an async manner. Take a look at some of the examples on this Microsoft KB article. Your CallGowalla() method is equivalent to LongRunningMethod() in those samples.
Running methods asynchronously, perhaps using additional threads, can be problematic in some environments like ASP.NET. It is important to research issues dependent upon your environment. If you are trying to get higher throughput (more calls to Gowalla) then you might want to look into use of the ThreadPool to execute your method without managing threads. The problem with this is that you might exceed the Gowalla API limits very quickly. It would be good if you could provide more detail on why you need to make these calls asynchrously.
If you need a good overview on threading with C# there is a free book available.
Upvotes: 1