Problems Xamarin Asynchronous method

Asked

Viewed 561 times

6

In the event of a button of my APP had the following code it worked running on the android emulator, but when I passed the app to the mobile 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 asynchronously, I looked for several examples but none became clear to me and I saw that my 'client' object exists a method has a beginenvioPessoa() and other endenvioPessoa(); Based on this link Easy way to use WCF service with async/await I tried to assemble a method to consume the web service async

call of the method:

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

At the end my code does not work 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: cannot Convert from 'method group' to 'System.Iasyncresult' (CS1503)

inserir a descrição da imagem aqui

  • In the second you should look for a envioPessoa that returns a Task<T> and make await before calling. But your first example should be simpler. In the first code, which error?

1 answer

2

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

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

Imagining that you created the ServiceReferencewith the methods Task based. Otherwise you’ll be like this:

inserir a descrição da imagem aqui

If created with the pattern you place async in the name 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();
    }
  • So Canova problem is that these methods with final async and completed were not generated, even though I have enabled to generate Async.

  • @Diegofilipepedrosantos Tá , you checked the checkbox and then selected the "Generate asynchronous Operations" on the radio button? Why is this what should generate the method with Async in the name.

  • I understood I did it yes, but it did not generate these methods, will by version of the framework or because I am using Xamarin Studio.

  • @Diegofilipepedrosantos Probably because of Xamarin Studio. When the method return type envioPessoa? What options do you have at the drop of Generate Asynchronous?

  • Only Beginenviopessoa Endenviopessoa

  • @Diegofilipepedrosantos What options do you have at the drop of Generate Asynchronous?

  • Generate Asynchronous: Assync or No.

  • Generate Asynchronous: Assync or No. @Vitorcanova

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.