ArcGIS Runtime SDK for WPF - Library Reference
OpenReadAsync Method
See Also  Example
ESRI.ArcGIS.Client Namespace > ArcGISWebClient Class : OpenReadAsync Method

uri
The URI of the resource to retrieve.
parameters
The collection of query key/value pairs associated with the request.
httpMethod
The HTTP method to use for this request.
userState
The user-specified identifier for the asynchronous task.
Opens a readable stream containing the specified resource.

Syntax

Visual Basic (Declaration) 
Public Sub OpenReadAsync( _
   ByVal uri As Uri, _
   Optional ByVal parameters As IDictionary(Of String,String), _
   Optional ByVal httpMethod As ArcGISWebClient.HttpMethods, _
   Optional ByVal userState As Object _
) 
C# 
public void OpenReadAsync( 
   Uri uri,
   IDictionary<string,string> parameters,
   ArcGISWebClient.HttpMethods httpMethod,
   object userState
)

Remarks

Use the ArcGISWebClient.DownloadStringAsync Method / ArcGISWebClient.DownloadStringCompleted Event to process (read/write) text such as: JSON, XAML, XML, and HTML.

Use the ArcGISWebClient.OpenReadAsync Method / ArcGISWebClient.OpenReadCompleted Event to process (read/write) binary streams of data such as: images, files (.doc, .pdf, .exe), music, movies, etc.

Parameters

uri
The URI of the resource to retrieve.
parameters
The collection of query key/value pairs associated with the request.
httpMethod
The HTTP method to use for this request.
userState
The user-specified identifier for the asynchronous task.

Example

How to use:

Click the 'ExecuteAsync' button to execute an ArcGISWebClient operation via OpenReadAsync for the Url in the TextBox. The return result of the OpenReadAsync call is a binary stream of data; in this case it is an image. Status information on the processing request will be shown in the ListBox. Experiment by double clicking the 'ExecuteAsync' twice in rapid succession to see how to trap for if the ArcGISWebClient.OpenReadAsync is busy. The 'CancelAsync' button terminates a currently executing ArcGISWebClient.OpenReadAsync operation.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Using the ArcGISWebClient.OpenReadAsync Method to retrieve a binary stream and display in an image control.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
  
  <!-- The Url to a web service that can be used by an ArcGISWebClient. -->
  <sdk:Label Height="20" HorizontalAlignment="Left" Margin="8,100,0,0" Name="Label_Url" 
       VerticalAlignment="Top" Width="31" Content="Url:"/>
  <TextBox Height="23" HorizontalAlignment="Left" Margin="8,118,0,0" Name="TextBox_Url" 
           VerticalAlignment="Top" Width="605" FontSize="10" />
  
  <!-- Buttons to perform the work. -->
  <Button Content="ExecuteAsync" Height="35" Width="201" HorizontalAlignment="Left"  
          Name="Button_ExecuteAsync" Click="Button_ExecuteAsync_Click" IsEnabled="True" Margin="12,158,0,0" 
          VerticalAlignment="Top"/>
  <Button Content="CancelAsync" Height="35" Width="201" HorizontalAlignment="Left" Margin="12,203,0,0" 
          Name="Button_CancelAsync" VerticalAlignment="Top" Click="Button_CancelAsync_Click" />
  
  <!-- Provide ArcGISWebClient information messages. -->
  <sdk:Label Height="19" HorizontalAlignment="Left" Margin="229,146,0,0" Name="Label_StatusInfo" 
       VerticalAlignment="Top" Width="120" Content="Status Information:"/>
  <ListBox Height="79" HorizontalAlignment="Left" Margin="229,162,0,0" Name="ListBox_StatusInformation" 
           VerticalAlignment="Top" Width="383" />
  
  <!-- An Image control. This will display the results of the ArcGISWebClient. -->
  <Image Height="350" HorizontalAlignment="Left" Margin="11,250,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="602" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="94" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="701" 
  TextWrapping="Wrap" Text="Click the 'ExecuteAsync' button to execute an ArcGISWebClient operation via OpenReadAsync for the 
  Url in the TextBox. The return result of the OpenReadAsync call is a binary stream of data; in this case it is
  an image. Status information on the processing request will be shown in the ListBox. Experiment by double 
  clicking the 'ExecuteAsync' twice in rapid succession to see how to trap for if the ArcGISWebClient.OpenReadAsync 
  is busy. The 'CancelAsync' button terminates a currently executing ArcGISWebClient.OpenReadAsync operation." />
   
</Grid>
C#Copy Code
// Create a Global (aka. Member) variable for the ArcGISWebClient object that will be used in other parts of the application.
private ESRI.ArcGIS.Client.ArcGISWebClient _ArcGISWebClient;
            
public MainPage()
{
  InitializeComponent();
  
  // Provide a valid Url for the ArcGISWebClient operation.
  TextBox_Url.Text = "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/export";
  
  // Set the _ArcGISWebClient variable to a new instance of the ArcGISWebClient Class.
  _ArcGISWebClient = new ESRI.ArcGIS.Client.ArcGISWebClient();
  
  // Prevent the client application from using a cache for the ArcGISWebClient operations.
  _ArcGISWebClient.DisableClientCaching = true;
  
  // Wire up an Event Handler for the ArcGISWebClient.OpenReadCompleted Event.
  _ArcGISWebClient.OpenReadCompleted += ArcGISWebClient_OpenReadCompleted;
}
            
private void Button_ExecuteAsync_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Clear out the image picture (i.e. blank).
  Image1.Source = null;
  
  // Rather than having all of the parameter arguments as part of the Url, create a parameter collection.
  System.Collections.Generic.IDictionary<string, string> myParameters = new System.Collections.Generic.Dictionary<string, string>();
  myParameters.Add("bbox", "11250589,-5286671,18589732,-1057334"); // Restrict extent of the map to the following coordinates.
  myParameters.Add("f", "image"); // Get the results back an image format.
  
  // NOTE: 
  // If you try to submit more than one ArcGISWebClient operation via ArcGISWebClient.OpenReadAsync at a time 
  // using the ExecuteAsync methodology you will get a Visual Studio (Runtime) error: 
  // NotSupportedException was unhandled by user code. "Request does not support concurrent I/O operations." 
  // In order to overcome this, use the ArcGISWebClient.IsBusy Property and take action accordingly.
  if (_ArcGISWebClient.IsBusy == true)
  {
  
  // There is an existing ArcGISWebClient operation running. Don't call the ArcGISWebClient.OpenReadAsync until
  // the current operation is completed.
  
  // Update the status informational messages.
  ListBox_StatusInformation.Items.Add("The ArcGISWebClient operaiton is busy (still processing).");
  
  }
  else if (_ArcGISWebClient.IsBusy == false)
  {
  // There is not an existing ArcGISWebClient operation running.
  
  // Clear out the status informational messages. 
  ListBox_StatusInformation.Items.Clear();
  
  // Create a UserToken to identify the ArcGISWebClient operation that is running.
  string myToken = "MY TOKEN";
  
  // Call the ArcGISWebClient.OpenReadAsync operation using a parameter list and user token.
  _ArcGISWebClient.OpenReadAsync( new Uri(TextBox_Url.Text),  myParameters, ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto, myToken );
  
  // Update the status informational messages.
  ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was started.");
  }
}
            
private void Button_CancelAsync_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // This will cancel the ExecuteAsync operation. 
  _ArcGISWebClient.CancelAsync();
}
            
private void ArcGISWebClient_OpenReadCompleted(object sender, ESRI.ArcGIS.Client.ArcGISWebClient.OpenReadCompletedEventArgs e)
{
  // Get the various Properties of the ArcGISWebClient.OpenReadCompletedEventArgs. 
  object theUserState = e.UserState;
  bool IsCanceled = e.Cancelled;
  System.Exception theException = e.Error; // TODO: Handle any errors as necessary.
  System.IO.Stream theResult = e.Result;
  
  if (IsCanceled == false)
  {
    // The ArcGISWebClient operation ran successfully.
    
    // Create a BitmapImage and set it's source to be the binary return result (i.e. an image stream) from ArcGIS Server.
    System.Windows.Media.Imaging.BitmapImage myBitmapImage = new System.Windows.Media.Imaging.BitmapImage();
    myBitmapImage.SetSource(theResult);
    
    // Set the Image.Source to the BitmapImage and set it Stretch Property.
    Image1.Source = myBitmapImage;
    Image1.Stretch = Stretch.None;
    
    // Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation completed for: " + e.UserState.ToString());
  }
  else
  {
    // The ArcGISWebClient operation was Canceled by the user.
    
    // Clear out the image picture (i.e. blank).
    Image1.Source = null;
    
    // Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was  CANCELED for: " + e.UserState.ToString());
  }
}
VB.NETCopy Code
' Create a Global (aka. Member) variable for the ArcGISWebClient object that will be used in other parts of the application.
Private _ArcGISWebClient As ESRI.ArcGIS.Client.ArcGISWebClient
            
Public Sub New()
  
  InitializeComponent()
  
  ' Provide a valid Url for the ArcGISWebClient operation.
  TextBox_Url.Text = "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/export"
  
  ' Set the _ArcGISWebClient variable to a new instance of the ArcGISWebClient Class.
  _ArcGISWebClient = New ESRI.ArcGIS.Client.ArcGISWebClient
  
  ' Prevent the client application from using a cache for the ArcGISWebClient operations.
  _ArcGISWebClient.DisableClientCaching = True
  
  ' Wire up an Event Handler for the ArcGISWebClient.OpenReadCompleted Event.
  AddHandler _ArcGISWebClient.OpenReadCompleted, AddressOf ArcGISWebClient_OpenReadCompleted
  
End Sub
            
Private Sub Button_ExecuteAsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Clear out the image picture (i.e. blank).
  Image1.Source = Nothing
  
  ' Rather than having all of the parameter arguments as part of the Url, create a parameter collection.
  Dim myParameters As System.Collections.Generic.IDictionary(Of String, String) = New System.Collections.Generic.Dictionary(Of String, String)
  myParameters.Add("bbox", "11250589,-5286671,18589732,-1057334") ' Restrict extent of the map to the following coordinates.
  myParameters.Add("f", "image") ' Get the results back an image format.
  
  ' NOTE: 
  ' If you try to submit more than one ArcGISWebClient operation via ArcGISWebClient.OpenReadAsync at a time 
  ' using the ExecuteAsync methodology you will get a Visual Studio (Runtime) error: 
  ' NotSupportedException was unhandled by user code. "Request does not support concurrent I/O operations." 
  ' In order to overcome this, use the ArcGISWebClient.IsBusy Property and take action accordingly.
  If _ArcGISWebClient.IsBusy = True Then
    
    ' There is an existing ArcGISWebClient operation running. Don't call the ArcGISWebClient.OpenReadAsync until
    ' the current operation is completed.
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operaiton is busy (still processing).")
    
  ElseIf _ArcGISWebClient.IsBusy = False Then
    
    ' There is not an existing ArcGISWebClient operation running.
    
    ' Clear out the status informational messages. 
    ListBox_StatusInformation.Items.Clear()
    
    ' Create a UserToken to identify the ArcGISWebClient operation that is running.
    Dim myToken As String = "MY TOKEN"
    
    ' Call the ArcGISWebClient.OpenReadAsync operation using a parameter list and user token.
    _ArcGISWebClient.OpenReadAsync(New Uri(TextBox_Url.Text), myParameters, ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto, myToken)
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was started.")
    
  End If
  
End Sub
            
Private Sub Button_CancelAsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' This will cancel the ExecuteAsync operation. 
  _ArcGISWebClient.CancelAsync()
  
 End Sub
  
Private Sub ArcGISWebClient_OpenReadCompleted(sender As Object, e As ESRI.ArcGIS.Client.ArcGISWebClient.OpenReadCompletedEventArgs)
  
  ' Get the various Properties of the ArcGISWebClient.OpenReadCompletedEventArgs. 
  Dim theUserState As Object = e.UserState
  Dim IsCanceled As Boolean = e.Cancelled
  Dim theException As System.Exception = e.Error ' TODO: Handle any errors as necessary.
  Dim theResult As IO.Stream = e.Result
  
  If IsCanceled = False Then
    
    ' The ArcGISWebClient operation ran successfully.
    
    ' Create a BitmapImage and set it's source to be the binary return result (i.e. an image stream) from ArcGIS Server.
    Dim myBitmapImage As New Imaging.BitmapImage
    myBitmapImage.SetSource(theResult)
    
    ' Set the Image.Source to the BitmapImage and set it Stretch Property.
    Image1.Source = myBitmapImage
    Image1.Stretch = Stretch.None
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation completed for: " + e.UserState.ToString)
    
  Else
    
    ' The ArcGISWebClient operation was Canceled by the user.
    
    ' Clear out the image picture (i.e. blank).
    Image1.Source = Nothing
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was  CANCELED for: " + e.UserState.ToString)
    
  End If
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8

See Also

© ESRI, Inc. All Rights Reserved.