"arg_targetinvocationexception" - 如何确定原因?
"arg_targetinvocationexception" - how determine the cause?
enter image description here
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;
}
异常发生在“HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(20);"这一行
我附上了我所面临的确切“arg_targetinvocationexception”问题的图片 check.I 我正在使用 MAUI 和 VS2022 上的最新预览版。当我访问这个功能时,我正面临“arg_targetinvocationexception”本期
主要是当我 运行 应用程序处于调试模式时它正在工作 fine.But 当我 运行 应用程序处于实时模式时显示此错误附件图像中显示一个警告框。
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();
}
}
我已经使用“AndroidMessageHandler()”并解决了我的问题
if(Device.RuntimePlatform == Device.Android)
{
//HttpClientHandler handler = new HttpClientHandler();
var Msghandler = new Xamarin.Android.Net.AndroidMessageHandler();
httpClient = new HttpClient(Msghandler);
}
else
{
httpClient = new HttpClient();
}
enter image description here
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;
}
异常发生在“HttpClient httpClient = new HttpClient(); httpClient.Timeout = TimeSpan.FromSeconds(20);"这一行
我附上了我所面临的确切“arg_targetinvocationexception”问题的图片 check.I 我正在使用 MAUI 和 VS2022 上的最新预览版。当我访问这个功能时,我正面临“arg_targetinvocationexception”本期
主要是当我 运行 应用程序处于调试模式时它正在工作 fine.But 当我 运行 应用程序处于实时模式时显示此错误附件图像中显示一个警告框。
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();
}
}
我已经使用“AndroidMessageHandler()”并解决了我的问题
if(Device.RuntimePlatform == Device.Android)
{
//HttpClientHandler handler = new HttpClientHandler();
var Msghandler = new Xamarin.Android.Net.AndroidMessageHandler();
httpClient = new HttpClient(Msghandler);
}
else
{
httpClient = new HttpClient();
}