I am using this library as wrapper for Mailchimp API v3.0. This library has all methods as async.
But I have to call that methods from my synchronous methods and to read result.
The first I have to say that I CANNOT make my methods as async. So my methods must stay synchronous and they will call async methods from this library.
So I read a lot of documentation about deadlock and avoiding that.
So here is one example of the method from the library:
internal class MemberLogic : BaseLogic, IMemberLogic
{
public async Task<Member> AddOrUpdateAsync(string listId, Member member)
{
using (var client = this.CreateMailClient($"{BaseUrl}/"))
{
var response =
await
client.PutAsJsonAsync($"{listId}/members/{this.Hash(member.EmailAddress.ToLower())}", member, null).ConfigureAwait(false);
await response.EnsureSuccessMailChimpAsync().ConfigureAwait(false);
return await response.Content.ReadAsAsync<Member>().ConfigureAwait(false);
}
}
}
Not I am calling this method from my method like:
public bool ListSubscribe(string email, string listId, string apiKey)
{
try
{
var api = new MailChimpManager(apiKey);
var profile = new Member();
profile.EmailAddress = emailAddress;
Member member = api.Members.AddOrUpdateAsync(listId, profile).Result;
}
catch(Exception ex)
{
throw;
}
return member != null;
}
The code deadlocks at line which calls async method:
Member member = api.Members.AddOrUpdateAsync(listId, profile).Result;
So my question is there any I am doing wrong in calling async method from sync?? Because I just cannot get this working.
UPDATE:
Regarding to these articles:
http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
https://blog.ciber.no/2014/05/19/using-task-configureawaitfalse-to-prevent-deadlocks-in-async-code/
I should be able to use it like in my example above with just setting .ConfigureAwait(false);
What is not mentioned in the question which is marked as duplicated.
Also this my code is working with a few other async methods fine. It just doesn't work with this just one method and it deadlocks in case Exception is occurred in async method.