Issue
private HttpClient CreateHttpClient(string UserToken = "")
{
HttpClientHandler handler = new HttpClientHandler();
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(20);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add(AppConfig.HeaderKey_AppToken, AppConfig.HeaderValue_AppToken);
if (!string.IsNullOrEmpty(UserToken))
{
httpClient.DefaultRequestHeaders.Add("UserToken", UserToken);
}
return httpClient;
}
The Exception is occure on "HttpClient httpClient = new HttpClient(); httpClient.Timeout = TimeSpan.FromSeconds(20);" this line
I have attached image what exact "arg_targetinvocationexception" issue i am facing please check.I am using MAUI And latest preview versio on VS2022 .When i am access this function that time I'm facing "arg_targetinvocationexception" this issue
Main thing when I'm run the application in debugg mode that time It's working fine.But when I'm run the application on Realese mode that time show this error indide a alert box show in attached image.
private async Task SendOTPClicked()
{
try
{
var networkFlag = Common.Instance.IsNetworkConnected();
if (networkFlag)
{
CustomProgressDialog.Instance.ShowLoading(AppResources.Sending_lbl);
string url = $"{AppConfig.ApiBaseUrl}{AppConfig.ApiKeypoints_sendotp}";
string phonenumber = CountryCode.Replace("+", "") + PhoneNumber;
string ApiUrl = $"{url}{phonenumber}";
var response = await RequestProviderService.Instance.PostAsync<ResponseViewModel<string>>(ApiUrl, phonenumber, "", AppConfig.TenantId_Value, "", null);
if (response != null)
{
if (response.Status)
{
await UserDialogs.Instance.AlertAsync(response.Message, AppResources.Title_Success, AppResources.Ok_Flag);
await App.Current.MainPage.Navigation.PushAsync(new EnterOTPView(CountryCode.Replace("+", "") + _phonenumber, User));
CustomProgressDialog.Instance.HideLoading();
}
else
{
string errorMessage = string.IsNullOrEmpty(response.ErrorMessage) ? response.Message : response.ErrorMessage;
CustomProgressDialog.Instance.HideLoading();
if (!string.IsNullOrEmpty(errorMessage))
{
await UserDialogs.Instance.AlertAsync(errorMessage, AppResources.Alert_Label, AppResources.Ok_Flag);
}
}
}
else
{
CustomProgressDialog.Instance.HideLoading();
}
}
else
{
await UserDialogs.Instance.AlertAsync(AppResources.NetworkError_CheckConnection, AppResources.Alert_Label, AppResources.Ok_Flag);
}
}
catch (System.Exception ex)
{
CustomProgressDialog.Instance.HideLoading();
}
}
Solution
I have use "AndroidMessageHandler()" and resolve my issue
if(Device.RuntimePlatform == Device.Android)
{
//HttpClientHandler handler = new HttpClientHandler();
var Msghandler = new Xamarin.Android.Net.AndroidMessageHandler();
httpClient = new HttpClient(Msghandler);
}
else
{
httpClient = new HttpClient();
}
Answered By - user16258378
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.