WebService synchronous coroutine

Mar 8, 2011 at 3:31 PM

Hi, I'm using trying to execute some webservice which get some value and with that execute some second webservice, the problem is that when I call the second web service it doesn't get executed until the first one ends, I've seen that I can use to control the workflow coroutines but actually I dont see how to make the calls to my services, I hope you can help me with a guidance about how to do that. Here is the code I'm using.

 

public ConstructorViewModel()
      {
         infoInfraestructuraClient.ObtenerSistemaXIdCampoCompleted += new EventHandler<ObtenerSistemaXIdCampoCompletedEventArgs>(infoInfraestructuraClient_ObtenerSistemaXIdCampoCompleted);
         infoInfraestructuraClient.ObtenerSistemaXIdCampoAsync();
      }


void infoInfraestructuraClient_ObtenerSistemaXIdCampoCompleted(object sender, ObtenerSistemaXIdCampoCompletedEventArgs e)
      {
         infoInfraestructuraClient.ObtenerPuntosRecoInfoCompleted += new EventHandler<ObtenerPuntosRecoInfoCompletedEventArgs>(infoInfraestructuraClient_ObtenerPuntosRecoInfoCompleted);
         
         foreach (Nuc_Sist sistema in e.Result)
         {
            infoInfraestructuraClient.ObtenerPuntosRecoInfoAsync(sistema.SistemaId);
            this.Categorias.Add(new Category { Id = sistema.SistemaId, Name = sistema.Nombre, SubCategories = itemPunto });
         }
      }


      public void infoInfraestructuraClient_ObtenerPuntosRecoInfoCompleted(object sender, ObtenerPuntosRecoInfoCompletedEventArgs e)
      {
         itemPunto.Clear();        

         foreach (Nuc_RecoInfo punto in e.Result)
            itemPunto.Add(new Category { Id = punto.PuntoId, Name = punto.NombrePunto });
 
      }


Mar 10, 2011 at 11:16 PM
Edited Mar 10, 2011 at 11:16 PM

To call WebServices within a corutine you have to build a custom IResult.

You can find an implementation of WebServiceResult (ported from Caliburn) here: http://marcoamendola.wordpress.com/2010/10/23/a-quick-caliburn-micro-tip-webservice-result/

Then your VM should look like this:

 

public class ConstructorViewModel: Screen {

	protected override OnInitialize() {
		Coroutine.BeginExecute(CallService.GetEnumerator());
	}
	
	public IEnumerable<IResult> CallService()
	{
		SistemaXIdCampo result;
		yield return new WebServiceResult<MyServiceClient, ObtenerSistemaXIdCampoCompletedEventArgs>(
			x => x.ObtenerSistemaXIdCampoAsync(theParameter),
			x => firstResult = x.Result
		);
		
		foreach (Nuc_Sist sistema in result)
		{
			this.Categorias.Add(new Category { Id = sistema.SistemaId, Name = sistema.Nombre, SubCategories = itemPunto });
		}
		
		foreach (Nuc_Sist sistema in firstResult)
		{
			yield return new WebServiceResult<MyServiceClient, ObtenerPuntosRecoInfoAsync>(
				x => x.ObtenerSistemaXIdCampoAsync(theParameter),
				x => {
					foreach (Nuc_RecoInfo punto in x.Result)
						itemPunto.Add(new Category { Id = punto.PuntoId, Name = punto.NombrePunto });
 
				}
			);
		}
	}
}