Xamarin iOS System.FormatException:输入的字符串格式不正确

问题描述 投票:0回答:1

我在iOS 13.3和13.4中面临一个奇怪的问题,即仅某些用户拥有System.FormatException。该应用程序是使用Xamarin Forms开发的。在调用后端API以获取用户配置文件之后但在到达后端API之前发生错误(由于AppInsights跟踪日志未显示该调用,因此验证未到达)。

我已经检查了可能发生错误的地方,但是没有任何线索。

在AppCenter中记录的异常堆栈跟踪

System.FormatException: Input string was not in a correct format.
System.Text            StringBuilder.FormatError ()
System.Text            StringBuilder.AppendFormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args)
System                 String.FormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args)
System                 String.Format (System.String format, System.Object[] args)
Mobile.Services        ApiService.GetAsync[T] (Wipro.MyBAT.Mobile.Core.Logging.AnalyticsEvent analyticsEvent, System.String endPoint, System.String queryString, System.Boolean requiresAuthentication, System.Threading.CancellationToken cancelToken, System.Boolean withRetry)
Mobile.Services        ApiService.GetUserProfile ()
Mobile.Services.Users  UserService.GetUserProfileFromServer ()

获取用户个人资料方法

public async Task<Response<Common.Model.User>> GetUserProfile()
{
    var result = await GetAsync<Response<Common.Model.User>>(AnalyticsEvent.UserProfilePerformance, Endpoints.PROFILE).ConfigureAwait(false);
    return result;
}

获取用户个人资料方法

private async Task<T> GetAsync<T>(AnalyticsEvent analyticsEvent, string endPoint, string queryString = "", bool requiresAuthentication = true, CancellationToken cancelToken = default, bool withRetry = true)
            where T : new()
{
    Stopwatch stopwatch = new Stopwatch();
    var dict = new Dictionary<string, string>
    {
        { "Throttler count", _throttler.CurrentCount.ToString() },
        { "Throttler limit", THROTTLER_LIMIT.ToString() },
        { "Retry", (!withRetry).ToString() }
    };

    await _throttler.WaitAsync();
    stopwatch.Start();
    string callId = System.Guid.NewGuid().ToString();

    string url;
    string responseString = string.Empty;
    try
    {
        if (_connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.None || _connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.Unknown)
        {
            return default(T);
        }

        url = $"{_environment.HostUrl}/api/{endPoint}{queryString}";
        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, url);
        if (requiresAuthentication)
        {
            var token = await _authService.GetAccessToken();
            message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
            message.Headers.Add("callid", callId);
        }

        // Error should be before this line as the call it not reaching API side
        HttpResponseMessage response = await _httpClient.SendAsync(message, cancelToken);

我在iOS 13.3和13.4中面临一个奇怪的问题,即仅某些用户拥有System.FormatException。该应用程序是使用Xamarin Forms开发的。调用后端API来获取用户之后,就会发生错误...

ios xamarin xamarin.forms xamarin.ios
1个回答
0
投票

最后找到了问题的根本原因。它实际上正在调用API服务器,但是由于用户在wifi网络中有一些VPN,因此在离开移动设备之前引发异常。改用4G代替Wifi解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.