locked
C# store app Await causes method to start again and provide me with dubbel data from Microsoft dynamics navision.

    Question

  • Hello,

    I'm having major issues with the await operator to call async tasks. 

    I got several different methods to get data from Microsoft Dynamics Nav. Now here is an example of a method i created.

    Inside the getNAVCategorie method there I call another method called checkURL(url) to check if an URL is valid or not, that method seems to be the problem and is also posted below.

    It has been tested with an hardcoded image and that works fine. The problem is the await operator in the checkURL(url)

    Whenever the code reaches the point that its gonna await, the method is sort of interrupted and starts to read from the top down again. This will provide me with double data since the first threat isn't stopped, but still works. And the app is just rereading the whole method.

    As you can see i have a foreach loop in the code. when i start debugging ITEMC in the foreachloop contains 3 results(Itemcategorys). But it will loop through it 2 times (since i'm provided with 6 categorys when the method is done in my observable collection, every category is doubled). 

    NOTE* This will also happen on the 

    GetItemCategory_Result catresult = await connection.GetItemCategoryAsync(catrequest);

    but i managed that by creating a new observableCollection later in the code.

    Actually this ALWAYS happen when i await a method. AND I DON'T KNOW WHY! 

    Are there more people with this problem or am i the only one? 

    Thanks for your response in advance.

    public async Task getNAVCategorie()
            {
                
                var connection = NavReference.getNavConnection();
    
                ItemCategory ic = new ItemCategory();
                GetItemCategory catrequest = new GetItemCategory(ic);
                GetItemCategory_Result catresult = await connection.GetItemCategoryAsync(catrequest);
    
                Groups = new ObservableCollection<ProductGroup>();
                var results = catresult.itemCategoryExport.ItemCategorys;
    
                foreach (ItemCategorys itemc in results)
                {
                    ProductGroup p = new ProductGroup(itemc.Code, itemc.Description, "");
    
                    if (p.Title != "")
                    {
                        string url = "www.thisisanurl.com/urls/url/ur/" + p.Title.ToLower() + ".jpg";
                        checkURL(url);//This method is the problem, located down here.
    
                        if (check == true)
                        {
                            p.ImagePath = url;
                        }
                        else
                        {
                            string noImg = "http://nthis is another url250.jpg";
                            p.ImagePath = noImg;
                        }
                        Groups.Add(p);
                    }
                }
            }
            private bool check;
            private async Task checkURL(string url)
            {
    
                HttpClient client = new HttpClient();
                client.MaxResponseContentBufferSize = 256000;
                HttpResponseMessage response = await client.GetAsync(url);//THIS IS THE PROBLEM!
    
                var checkURL = response.StatusCode;
    
                if (checkURL.ToString() != "OK")
                {
                    check = false;
                }
                else
                {
                    check = true;
                }
            }


    Tuesday, August 26, 2014 10:28 AM

Answers

  • Hi Proliges,

    You have a major workflow break point in your code . Meanwhile Async method is waiting other programs workfow is continue. There for some of the workflow cycle of your code may be still contains previous value check with the flow.

    To avoid that change

    private async Task<bool> checkURL(string url)
            {
    
                HttpClient client = new HttpClient();
                client.MaxResponseContentBufferSize = 256000;
                HttpResponseMessage response = await client.GetAsync(url);//THIS IS THE PROBLEM!
    
                var checkURL = response.StatusCode;
    
                if (checkURL.ToString() != "OK")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }


    and inside foreach

      foreach (ItemCategorys itemc in results)
                {
                    ProductGroup p = new ProductGroup(itemc.Code, itemc.Description, "");
    
                    if (p.Title != "")
                    {
                        string url = "www.thisisanurl.com/urls/url/ur/" + p.Title.ToLower() + ".jpg";
                        check = await checkURL(url);//This method is the problem, located down here.

    It is the best  practice ..

    And make sure to call all the Async operations with await keyword..

    without change much you can try below code also. but it also contains workflow problem. may be it will not consideration according to your situation

      foreach (ItemCategorys itemc in results)
                {
                    ProductGroup p = new ProductGroup(itemc.Code, itemc.Description, "");
    
                    if (p.Title != "")
                    {
                        string url = "www.thisisanurl.com/urls/url/ur/" + p.Title.ToLower() + ".jpg";
                        await checkURL(url);//This method is the problem, located down here.

    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    ---------------------------------------

    M Prabath Maduranga Peiris
    Microsoft Student Partner
    Blogs : prabathsl.blogspot.com

    Tuesday, August 26, 2014 3:52 PM