Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Could not find method due to a type load - AsyncVoidMethodBuilder.Start[TStateMachine]

$
0
0

I'm facing a problem to run my application on IOS. It is running normally on Android but when I try run on IOS, it is returning bellow exception:

{System.TypeLoadException: Could not find method due to a type load error
at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.18/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:84
at MLL_Partner.View.Login.AccessLogin() [0x00013] in :0
at MLL_Partner.View.Login.ButtonAccess_Clicked (System.Object sender, System.EventArgs e) [0x00002] in G:\Visual Studio Projects\MLL\Mobile\MLL_Partner\MLL_Partner\View\Login.xaml.cs:46 }

I have a login page content that make a instance a RestfulClient class that contains async methods.
Follow my Login function code:

private void ButtonAccess_Clicked(object sender, EventArgs e)
{
try
{
AccessLogin();
}
catch (Exception ex)
{
DisplayAlert("Erro", ex.Message, "Close");
}
}
private async void AccessLogin()
{
IsBusy = true;

        var restLogin = new RestfulClient<TokenResponseViewModel>(ViewModel.Session.BaseAddress, ViewModel.Session.Token);
        var resp = await restLogin.Login(String.Format("api/security/token"), entryUserName.Text, entryPassword.Text);

        if (resp.ErrorDescription == null)
        {
            ...
        }
        else
        {
            ...
        }
        IsBusy = false;
    }

Follow My RestfulClient Class code:

using Accounts;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace MLL_Partner.ViewModel
{
public class RestfulClient
{
private HttpClient _httpClient { get; set; }
public RestfulClient(String pBaseAddress, String pToken)
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(pBaseAddress);
_httpClient.Timeout = new TimeSpan(0, 0, 30);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", pToken);
}
public async Task Login(string tokenRequestUrl, string username, string password)
{
HttpResponseMessage response = new HttpResponseMessage();
TokenResponseViewModel actionResult = new TokenResponseViewModel();

        var prms = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", username),
            new KeyValuePair<string, string>("password", password)
        };

        response = await _httpClient.PostAsync(tokenRequestUrl, new FormUrlEncodedContent(prms));
        var resp = await response.Content.ReadAsStringAsync();
        var dataResult = JsonConvert.DeserializeObject<TokenResponseViewModel>(resp);
        dataResult.Username = username;
        return actionResult;
    }

    public async Task<ActionResultViewModel<T>> GetAsync(String requestURI)
    {
        ActionResultViewModel<T> actionResult;
        try
        {
            var resp = await _httpClient.GetAsync(requestURI);
            if (resp.IsSuccessStatusCode)
            {
                var respStr = await resp.Content.ReadAsStringAsync();
                actionResult = JsonConvert.DeserializeObject<ActionResultViewModel<T>>(respStr);
                actionResult.LOGIN = false;

                return actionResult;
            }
            else
            {
                if(resp.ReasonPhrase == "Unauthorized" || resp.ReasonPhrase == "Not Found")
                    actionResult = new ActionResultViewModel<T> { MESSAGE = "Favor refazer o login!", DATA = Activator.CreateInstance<T>(), OK = false, LOGIN = true };
                else
                    actionResult = new ActionResultViewModel<T> { MESSAGE = "Erro desconhecido, favor verificar sua conexão com a internet!", DATA = Activator.CreateInstance<T>(), OK = false };
            }
        }
        catch (Exception e)
        {
            actionResult = new ActionResultViewModel<T> { MESSAGE = "Favor verificar sua conexão com a internet!", DATA = Activator.CreateInstance<T>(), OK = false };
        }

        return actionResult;
    }
    /// <summary>
    /// Method GET
    /// </summary>
    /// <typeparam name="T">Entity Object Type</typeparam>
    /// <param name="requestURI">Controller name: Eg: /API/USERS/1 or /API/USERS</param>
    /// <returns></returns>
    public async Task<ActionResultViewModel<T>> GetSingleAsync(String requestURI)
    {
        ActionResultViewModel<T> actionResult;
        try
        {
            var resp = await _httpClient.GetAsync(requestURI);
            if (resp.IsSuccessStatusCode)
            {
                var respStr = await resp.Content.ReadAsStringAsync();
                actionResult = JsonConvert.DeserializeObject<ActionResultViewModel<T>>(respStr);
                actionResult.LOGIN = false;
                return actionResult;
            }
            else
            {
                if (resp.ReasonPhrase == "Unauthorized" || resp.ReasonPhrase == "Not Found")
                    actionResult = new ActionResultViewModel<T> { MESSAGE = "Favor refazer o login!", DATA = Activator.CreateInstance<T>(), OK = false, LOGIN = true };
                else
                    actionResult = new ActionResultViewModel<T> { MESSAGE = "Erro desconhecido, favor verificar sua conexão com a internet!", DATA = Activator.CreateInstance<T>(), OK = false };
            }
        }
        catch (Exception e)
        {
            actionResult = new ActionResultViewModel<T> { MESSAGE = "Favor verificar sua conexão com a internet!", DATA = Activator.CreateInstance<T>(), OK = false };
        }

        return actionResult;
    }
    public async Task<ActionResultViewModel<T>> PostAsync(String requestURI, T entity)
    {
        ActionResultViewModel<T> actionResult;
        try
        {
            var content = JsonConvert.SerializeObject(entity);
            var response = await _httpClient.PostAsync(requestURI, new StringContent(content, Encoding.UTF8, "application/json"));
            response.EnsureSuccessStatusCode();

            var respStr = await response.Content.ReadAsStringAsync();
            actionResult = JsonConvert.DeserializeObject<ActionResultViewModel<T>>(respStr);
            actionResult.LOGIN = false;
            return actionResult;
        }
        catch (Exception ex)
        {
            actionResult = new ActionResultViewModel<T> { MESSAGE = "Favor verificar sua conexão com a internet!", DATA = Activator.CreateInstance<T>(), OK = false };
        }

        return actionResult;
    }

    public async void DeleteAsync(String requestURI)
    {
        var resp = await _httpClient.DeleteAsync(requestURI);
        resp.EnsureSuccessStatusCode();
    }

}

Could you help me to find and fix my this problem? Thank you!


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>