【问题标题】:"arg_targetinvocationexception" - how determine the cause?“arg targetinvocationexception” - 如何确定原因?
【发布时间】:2022-01-05 17:44:20
【问题描述】:

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”问题,请检查。我正在使用 MAUI 和 VS2022 上的最新预览版。当我访问此功能时,我正面临“arg_targetinvocationexception”这个问题

当我在调试模式下运行应用程序时,它工作正常。但是当我在 Realese 模式下运行应用程序时,在附加图像中显示的警告框内显示此错误。

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();
            }

        }

【问题讨论】:

  • 哪一行具体会导致异常?堆栈跟踪显示什么? InnerException 属性显示了什么?
  • 当我访问此 SendOTPClicked() 函数时,“arg_targetinvocationexception”消息显示在我的警报框中,我没有在任何特定行中找到。或者当我在调试中运行应用程序时的主要内容那个时候的模式它正在工作。但是当我在 Realese 模式下运行应用程序时显示这个错误
  • @Jason 你知道我该如何解决这个问题吗?
  • 添加一些日志记录以在发布模式下捕获异常的详细信息
  • 尝试在HttpClient初始化中设置handler作为参数,检查stackoverflow.com/a/70169928/8187800

标签: xamarin xamarin.forms xamarin.android maui


【解决方案1】:

我使用了“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();
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多