none
Task throws System.Reflection.TargetInvocationException RRS feed

  • Question

  • Hi,

    I am getting this error:

    System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

    Here is how I am getting it..

    In my Click event, I am doing:

    StackLayoutSignin.IsVisible = false;
    BusyIndicatorSignin.IsVisible = true;
    await Task.Run(SigninUser);
    
    

    the SigninUser is:

    async void SigninUser()
    {
        var salt = await PasswordHash.GetPasswordSalt(TextBoxSigninEmailAddress.Text);
        ....................
        ....................
        ....................
        ....................
    }
    
    

    and the error occurs on this line:

    var salt = await PasswordHash.GetPasswordSalt(TextBoxSigninEmailAddress.Text);

    My GetPasswordSalt is here:

    public static async Task<Tuple<string, string>> GetPasswordSalt(string email_address)
    {
        // var client = new HttpClient();
    
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://www.my.com/salt.php");
    
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("email", email_address)
            });
    
            var response = await client.PostAsync("https://www.my.com/salt.php", content);
    
            var result = await response.Content.ReadAsStringAsync();
    
            if (!String.IsNullOrWhiteSpace(result))
            {
                dynamic data = JsonConvert.DeserializeObject(result);
    
                salt_result = data[0].result.ToString();
                password_salt = data[0].password_salt.ToString();
            }
            else
            {
                password_salt = "X";
            }
    
            return new Tuple<string, string>(salt_result, password_salt);
        }
    }
    

    kindly help...

    Thanks,
    Jassim



    Saturday, January 11, 2020 12:30 PM

All replies

  • I would not deserialize into a dynamic.  I would deserialize into a class.  I am guess you are getting the error because it does not think data is an array.  Easiest way to create a class to deserialize into is to call web service and copy the json response into the clipboard.  In the edit menu there is a paste special--> paste json as classes.
    Saturday, January 11, 2020 1:17 PM
  • Which data? the User data or the GetPasswordSalt?
    Saturday, January 11, 2020 1:23 PM
  • My PasswordSalt is a separate class..

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Eithaar
    {
        public class PasswordHash
        {
            static string salt_result;
            static string password_salt;
    
            public static async Task<Tuple<string, string>> GetPasswordSalt(string email_address)
            {
                // var client = new HttpClient();
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://www.my.com/salt.php");
    
                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("email", email_address)
                    });
    
                    var response = await client.PostAsync("https://www.my.com/salt.php", content);
    
                    var result = await response.Content.ReadAsStringAsync();
    
                    if (!String.IsNullOrWhiteSpace(result))
                    {
                        dynamic data = JsonConvert.DeserializeObject(result);
    
                        salt_result = data[0].result.ToString();
                        password_salt = data[0].password_salt.ToString();
                    }
                    else
                    {
                        password_salt = "X";
                    }
    
                    return new Tuple<string, string>(salt_result, password_salt);
                }
            }
        }
    }
    

    Saturday, January 11, 2020 1:28 PM
  • Hi Jassim Rahma,

    Thank you for posting here.

    As Ken Tucker suggested, try to use replace 'dynamic' in 

    dynamic data = JsonConvert.DeserializeObject(result);

    with a certain class, and make sure 'result' in the code is a valid json string.

    Besides, if it cannot help you solve the problem, please provide more details about your exception with which line of the code thrown the exception? 

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 15, 2020 8:24 AM
    Moderator