Issue
I am trying to get from MailChimp a MailChimp List Collection. I have set up the process as displayed in the example on MailChimp.net for getting all lists but it exits out before the list is returned unless I use a Console read after the task is called. How do I get this supposedly simple task to work?
static void Main(string[] args)
{
AddUpdateMailChimp();
Console.Read();
}
static async void AddUpdateMailChimp()
{
lstIDs = await Get_MailChimp_Info();
for(int i = 0; i < lstIDs.Count; i++)
{
AddUpDateMailChimpAsync(lstIDs[i]);
}
}
private static async Task< List< string >> Get_MailChimp_Info()
{
var lstIDs = new List< string > ();
apikey = GetApiKey() //from config file
manager = new MailChimpManager(apikey);
//............below line is where it bombs unless I use a concole.Read in the main..........//
**IEnumerable< MailChimp.Net.Models.List> mailChimpListCollection = await manager.Lists.GetAllAsyunc().ConfigureAwait( continueOnCapturedContext: false);**
............catch statements
//.......foreach loop to get the list Ids
}
Solution
I had this problem recently. Because AddUpdateMailChimp()
is async, then the Main()
method is continuing execution as soon as it hits it, and not waiting on the result.
Also the method returns void so it's seen as fire-and-forget.
If you want to be able to await it, then it should return a task.
That way, in Main()
you can do
var result = AddUpdateMailChimp().Result;
Answered By - Barry O'Kane Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.