Problems Xamarin method Asynchronous

6

In the event of a button of my APP had the following code it worked running in the android emulator, but when I passed the app to the cell phone generates a message the app stopped.

        Button buttonPessoasNecessita = FindViewById<Button> (Resource.Id.mybuttonPesNec);
        buttonPessoasNecessita.Click += delegate {
        Service1Client client;
        var binding = new BasicHttpBinding () { Name= "basicHttpBinding", MaxReceivedMessageSize = 67108864,};

        binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() 
        { MaxArrayLength = 2147483646, MaxStringContentLength = 5242880, }; 

        var timeout = new TimeSpan(0,60,60); 
        binding.SendTimeout= timeout; 
        binding.OpenTimeout = timeout; 
        binding.ReceiveTimeout = timeout; 


        client = new Service1Client(binding, new EndpointAddress ("http://engb.uni5.net/Service1.svc")); 

        buttonPessoasNecessita.text = client.envioPessoa();

        };

Trying to solve the case I thought of using asynchronous, I looked for several examples but none was clear to me and I saw that my client object exists a method has a beginenvio methodPessoa () and another endenvioPessoa (); Based on this link Easy way to use WCF service with async / await tried to set up a method to consume the web service in an async way

method call:

      var t = await executeAsync(binding);

Method:

       public async Task<string> executeAsync(BasicHttpBinding binding){

        Service1Client client = new Service1Client(binding, new EndpointAddress ("http://engb.uni5.net/Service1.svc")); 

        var t = Task<string>.Factory.FromAsync (
            ((IService1)client.InnerChannel).BeginenvioPessoa,
            ((IService1)client.InnerChannel).EndenvioPessoa);
        return await t;
    }

In the end my code does not work it displays syntax errors

  

Error CS1502: The best overloaded method match for   'System.Threading.Tasks.TaskFactory.FromAsync (System.IAsyncResult,   System.Func) 'has some invalid arguments   (CS1502)

     

Error CS1503: Argument 1: can not convert from 'method group' to   'System.IAsyncResult' (CS1503)

    
asked by anonymous 05.12.2015 / 19:14

1 answer

2

As you need to use the asynchronous form probably what you need is this:

    using (var client = new Service1Client())
    {
        string resultado = await client.envioPessoaAsync();
    }

Assuming you created ServiceReference with the Task based methods. Otherwise it will look like this:

If you created it with the pattern that places async in the name it gets a little bigger:

    using (var client = new Service1Client())
    {
        client.envioPessoaCompleted += (s, e) =>
        {
            //Vai executar esse trecho só quando retornar
            string resultado = e.Result;
        };
        //Vai efetivamente fazer a chamada e quando
        // o resultado  retornar cairá no trecho acima
        client.envioPessoaAsync();
    }
    
07.12.2015 / 16:47