This is a mirror of official site: http://jasper-net.blogspot.com/

Silverlight and Synchronous Network Calls

| Wednesday, August 10, 2011
image_thumb.png

Someone tweeted on Twitter that they wanted to stop the UI thread in Silverlight while they performed a couple of asynchronous network activities because one activity depended on the output of another.

To be honest, I don’t really like the idea and I’ve drawn this slide in the past;

where I’ve advised people not to try and subvert the asynchronous networking aspects of Silverlight so I suppose it’s no surprise that I’m not a big fan of trying to block the UI thread while networking activity completes.

Let’s say I’ve got this simple web service to invoke;

[ServiceContract]
public interface IService
{
  [OperationContract]
  string InitialCall();

  [OperationContract]
  string SubsequentCall(string input);
}

and all it does is return hard-coded strings;

public class Service : IService
{
  public string InitialCall()
  {
    return ("Hello");
  }

  public string SubsequentCall(string input)
  {
    return (input + " World");
  }
}

but to call it, I need to make 2 asynchronous trips across the network to invoke the InitialCall operation and then the SubsequentCall operation with the idea being that the second operation gets passed the results from the first one.

Now, in the Silverlight world the WCF proxy generation tools in Visual Studio will represent these asynchronous operations using the Event Based Asynchronous Pattern giving me an initial method to call XXXAsync and an EventArgs derived from AsyncCompletedEventArgs.

If I create a quick view;

<UserControl
  x:Class="SilverlightApplication5.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  d:DesignHeight="300"
  d:DesignWidth="400"
  xmlns:local="clr-namespace:SilverlightApplication5">
  <UserControl.DataContext>
    <local:ViewModel />
  </UserControl.DataContext>
  <Grid


Read more: Mike Taulty's Blog
QR: silverlight-and-synchronous-network-calls.aspx

Posted via email from Jasper-net

0 comments: