ARGH -- Updating to new versions and things going wrong.

binkman71binkman71 ✭✭USMember ✭✭

Now, keep in mind, this app works perfectly well, and has been published to both iTunes and Google Play app stores. After doing so, I decided to go through with the upgrade to the Unified iOS API and all that entails. I followed all the upgrade documents posted (had been updated to Forms 1.3 prior to this), and the iOS app seems to be working on my test devices.

Android, however, is crashing when I attempt the following:

WebResponse wr = await Task.Factory.FromAsync<WebResponse>( request.BeginGetResponse, request.EndGetResponse, request );

Then I get an unhandled exception with the following call stack:

0x21 in System.Diagnostics.Debugger.Mono_UnhandledException_internal
0x1 in System.Diagnostics.Debugger.Mono._UnhandledException
0x20 in object.d0380c7f-78bb-4a7a-9ca1-028c680ed18f
0x6 in System.Runtime.CompilerServices.AsyncVoidMethodBuilder.AnonymousMethod_0
0xC in Android.App.SyncContext.Post.AnonymousMethod_0 at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18,19
0x11 in Java.Lang.Thread.RunnableImplementor.Run at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36,6
0xA in Java.Lang.IRunnableInvoker.n_Run at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Java.Lang.IRunnable.cs:71,4
0x11 in object.d0380c7f-78bb-4a7a-9ca1-028c680ed18f

I hope that's all right, I hand copied all that from the error window. ;)

Now, again, all this worked prior to the upgrade. But now with the latest Xamarin 3.9 and the latest Forms 1.3.4, I'm getting this error. And, the aforementioned call is in a try...catch block, so why it is saying unhandled I don't know. And also again, it works on iOS.

I don't know if this should be in the Android forum, but I figure it's a Forms app, so I'd start here...any ideas appreciated.

Thanks.

Posts

  • adamkempadamkemp mod USInsider, Developer Group Leader mod

    What does the rest of this function look like?

  • binkman71binkman71 ✭✭ USMember ✭✭

    As usual, several minutes after posting this, the solution was found.

    This is weird tho -- the URL specified https and caused the error. When I changed it to http, the call succeeds. Shouldn't that return some sort of network error? Why is that causing an unhandled exception? (I am assuming, of course, the server didn't like the https and somehow sent a failure response, but if so, that response didn't get to my code...)

  • adamkempadamkemp mod USInsider, Developer Group Leader mod

    I believe this API converts HTTP/network errors into exceptions. You have to handle those.

  • binkman71binkman71 ✭✭ USMember ✭✭

    I DO handle exceptions. I'm not getting this one in my code...the crash occurred outside my try/catch statements....

  • adamkempadamkemp mod USInsider, Developer Group Leader mod

    So what does the rest of the code look like?

    If there is a problem that causes these exceptions to not get caught then you have a potential crash lurking in your app that may show up in the future if there really is a network issue. I don't recommend ignoring it just because you got it working for now.

  • binkman71binkman71 ✭✭ USMember ✭✭

    Here's some of the code, without going too deep into the details. The user sets up a URL and login credentials:

    public async Task Authenticate()
    {
                    String[] strParams = { "Login" };
                    HttpWebRequest request = GetRequest( MakeURL( strParams ) + "/" );
    
                    WebResponse wr = await Task.Factory.FromAsync<WebResponse>( request.BeginGetResponse, request.EndGetResponse, request );
    
                    Stream responseStream = wr.GetResponseStream();
    
            // process response here....
    }
    
            public HttpWebRequest GetRequest( String url )
            {
                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create( url );
                StringBuilder strb = new StringBuilder( "" );
                strb.AppendFormat( "logonid={0};password={1};customer={2}", _us.UserName, _us.Password, _us.ID );
                var bytes = Encoding.UTF8.GetBytes( strb.ToString() );
                wr.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en";
                wr.Headers[HttpRequestHeader.Authorization] = Convert.ToBase64String( bytes );
                return wr;
            }
    
        public async void UserLogin()
        {
            try
            {
                    await TimeoutAwait( Authenticate() );
                    // other code here....
            }
            catch( TimeoutException te )
            {
                    // handle it here...
            }
            catch( System.Net.Webexception we )
            {
                    // handle it here....
            }
            catch(Exception e )
            {
                    // generic catch-all here...
            }
        }
    
    // TIMEOUT is defined to 30 seconds
            public static async Task TimeoutAwait( this Task task )
            {
                var timeoutCancellation = new CancellationTokenSource();
                var completedTask = await Task.WhenAny( task, Task.Delay( TIMEOUT, timeoutCancellation.Token ) );
                if( task == completedTask )
                {
                    timeoutCancellation.Cancel();
                    await task;
                }
                else
                {
                    throw new TimeoutException();
                }
            }
    
  • adamkempadamkemp mod USInsider, Developer Group Leader mod

    If you can get it into a smaller sample project then you should report it as a bug (http://bugzilla.xamarin.com).

Sign In or Register to comment.