Using Official Mailjet API v3 .NET Wrapper from Surface controller.
I have been trying to integrate newsletter capabilities for a customer's web site, using Mailjet API.
Official Mailjet API v3 .NET Wrapper uses asynchronous functions in order to contact the service. I have a SurfaceController, that gets an Id and tries to retrieve a contact from the Mailjet service.
In order to use the retrieve method I used something like this:
public class PageNewsletterFormController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
string ContactID = Request.QueryString["id"];
ContactModel contact = new ContactModel(model.Content);
string ContactID = Request.QueryString["id"];
ContactModel contact = new ContactModel(model.Content);
JArray result = null;
MailjetClient client = new MailjetClient(WebConfigurationManager.AppSettings["apiKey"],
WebConfigurationManager.AppSettings["apiSecret"]) { Version = ApiVersion.V3_1 };
MailjetRequest request = new MailjetRequest
{
Resource = Contact.Resource
}
.Filter(Contact.ContactsList, WebConfigurationManager.AppSettings["contactslist"])
.Filter(Contact.ID, ContactID)
.Filter(Contact.Limit, "50");
MailjetResponse response = client.GetAsync(request).Result;
return CurrentTemplate(contact);
}
As soon as the code hits the GetAsync() method the page freezes.
I tried to do the same with something like this:
public class PageNewsletterFormController : RenderMvcController
{
public async Task<ActionResult> NewsletterForm(RenderModel model)
{
string ContactID = Request.QueryString["id"];
ContactModel contact = new ContactModel(model.Content);
JArray result = null;
MailjetClient client = new MailjetClient(WebConfigurationManager.AppSettings["apiKey"],
WebConfigurationManager.AppSettings["apiSecret"]) { Version = ApiVersion.V3_1 };
MailjetRequest request = new MailjetRequest
{
Resource = Contact.Resource
}
.Filter(Contact.ContactsList, WebConfigurationManager.AppSettings["contactslist"])
.Filter(Contact.ID, ContactID)
.Filter(Contact.Limit, "50");
MailjetResponse response = client.GetAsync(request).Result;
return CurrentTemplate(contact);
}
I guess you're encountering a deadlock issue here. Looking at the above sample code I can't find an await keyword within an async method.
Try using the await keyword e.g MailjetResponse response = await client.GetAsync(request);
(Not the best solution) If within a non async method like i would expect in a SurfaceController (ChildActionOnly) then try calling using this MailjetResponse response = Task.Run(async => await client.GetAsync(request)).GetAwaiter().GetResult();
Using Official Mailjet API v3 .NET Wrapper from Surface controller.
I have been trying to integrate newsletter capabilities for a customer's web site, using Mailjet API.
Official Mailjet API v3 .NET Wrapper uses asynchronous functions in order to contact the service. I have a SurfaceController, that gets an Id and tries to retrieve a contact from the Mailjet service.
In order to use the retrieve method I used something like this:
As soon as the code hits the GetAsync() method the page freezes.
I tried to do the same with something like this:
It has the same problem - it freezes.
Is there someone that can offer any pointers?
Thanks, George J.
Bump - Having the same problem here (in fact, we're both working on the same project and we're stuck on this). Any advice?
Hi,
You may have already tried some these...
I guess you're encountering a deadlock issue here. Looking at the above sample code I can't find an await keyword within an async method.
Try using the await keyword e.g MailjetResponse response = await client.GetAsync(request);
(Not the best solution) If within a non async method like i would expect in a SurfaceController (ChildActionOnly) then try calling using this MailjetResponse response = Task.Run(async => await client.GetAsync(request)).GetAwaiter().GetResult();
Hope it helps. Cheers
is working on a reply...