PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How to async task and Mailchimp API v3.0

 September 06, 2022     automation, c#, console-application, mailchimp-api-v3.0     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing