mahesh kumar B M

mahesh kumar B M

  • NA
  • 44
  • 1.1m

equivalent code without using async await in c# 4.0

Oct 17 2014 12:30 PM
here is the below code that is used to get the list of templates from sendgrid using async and await feature. but due to some limitations i cant use these feature in my application which is in .net 3.5/4 and i use vs 2010. i just need the equivalent code which will run at background without using async and await.... im grateful if somebody gives equivalent code without suggesting me to use vs 2012 or using any nugget package....


private static async Task GetAsync(string getString)
{

TReturn data =default(TReturn);
using (var client = new HttpClient())
{

client.BaseAddress = new Uri("https://api.sendgrid.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
Encoding.ASCII.GetBytes(String.Format("{0}:{1}", Credentials.UserName, Credentials.Password))));
HttpResponseMessage response = await client.GetAsync(getString);
if (response.IsSuccessStatusCode)
{

string t = await response.Content.ReadAsStringAsync();
data =JsonConvert.DeserializeObject(t);
}

}

return data;
}

private static async Task<TemplateVersion> GetCurrentTemplate(Template template)
{

return await GetAsync<TemplateVersion>(String.Format(

"v3/templates/{0}/versions/{1}", template.id,
template.Versions.Single(s1 => s1.active.Equals(

"1")).id));
}

private static async Task<TemplatesCollection> GetTemplates()
{

return await GetAsync<TemplatesCollection>("v3/templates");
}

private static void Main()
{

List<Template> templates = GetTemplates().Result.Templates;

Template myTemplate = templates.Single(s1 => s1.name.Equals(

"Renew"));
SendEmailWebApi(myTemplate,

" Reminder", "n AutoMailer");
}

private static void SendEmailWebApi(Template template, string subject, string doNotReplyDescription)
{

// this method will send mail using sendgrid properties by adding from ,to ,subject and attachments
}

Answers (1)