0

I've created an android application using Xamarin.android that consumes an asmx webservice.the latter connects my app to sql server. the app works perfectly with good internet connection. but when I have bad internet service, I get an exception in visual studio (while deploying my app to a physical device). the exception is: System.Reflection.TargetInvocationException. and when my phone is not connected to my laptop and I run the app with poor connection, my phone gets out from the app. I am trying to catch this exception so that the app doesn't crash and at least give me an alert dialog that my connection failed so I did this:

 WSitems.WebService1 ws = new WSitems.WebService1();
            try
            {
                ws.itemsinfoAsync();
                ws.itemsinfoCompleted += Ws_itemsinfoCompleted;
            }
            catch(Exception exp )
            {
                Context context = this.Context;

                AlertDialog.Builder alert = new AlertDialog.Builder(context);

                alert.SetTitle("Connection to Server failed");

                alert.SetMessage("Please, check your internet connection!");
                alert.SetPositiveButton("okay",( senderAlert, args) => {

                    alert.Dispose();
                });
            
                _dialog = alert.Create();

                _dialog.Show();
            }

but the problem wasn't solved. what should I do? thanks in advance.

rana hd
  • 355
  • 3
  • 18
  • Can you have a try to `await ws.itemsinfoAsync();`? – nevermore Jul 27 '20 at 07:43
  • do I add it within the try command or do I remove the try catch and use await on its own? – rana hd Jul 29 '20 at 10:54
  • Just add it within the try command. – nevermore Jul 30 '20 at 01:27
  • when I added await I got an error. "The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. I looked at the details of the exception I read the following: System.Net.WebException: Error: ConnectFailure (Network is unreachable) and sometimes system.net.webexception no rout to host. I tried putting catch(System.Net.WebException) but still it couldn't catch it. what should I do? – rana hd Jul 31 '20 at 12:28
  • Make your method async, something like: `public async void test(){}`. – nevermore Aug 03 '20 at 01:25

0 Answers0