Asynchronous API Calls Events - LorenData/ECGrid-API GitHub Wiki
For each of the synchronous API Methods there is a corresponding asynchronous method called <MethodName>Async. Each of these asynchronous API methods also have an associated <MethodName>Completed Event, <MethodName>CompletedEventHandler, and <MethodName>CompletedEventArgs class.
ECGridOS uses the Event-based Asynchronous Pattern (EAP) for most of its API Methods. An overview of this pattern from MSDN can be found here. This allows control to be returned to your system/program/user while you wait for data to be returned.
using System;
using System.Web.Services.Protocols;
using System.Xml;
using ECGridService = ECGrid_API.net.ecgridos;
namespace ECGrid_API
{
public class ECGrid_Main
{
static void ECGrid_WhoAmICompleted(object sender, ECGridService.WhoAmICompletedEventArgs e)
{
ECGridService.SessionInfo results = e.Result;
Console.WriteLine($"{results.FirstName} {results.LastName}");
}
/// <summary>
/// Main Method for Running the Program
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
try
{
using (ECGridService.ECGridOSAPIv3 ECGrid = new ECGridService.ECGridOSAPIv3())
{
try
{
string SessionID = "00000000-0000-0000-0000-000000000000";
ECGrid.WhoAmICompleted += new ECGridService.WhoAmICompletedEventHandler(ECGrid_WhoAmICompleted);
Console.WriteLine("Pre Async Call");
ECGrid.WhoAmIAsync(SessionID);
Console.WriteLine("Post Async Call");
Console.WriteLine("Who Am I?");
}
catch (SoapException SoapEx)
{
// See SOAP Exceptions in the Appendix
var ECG_Ex = CatchException(SoapEx);
Console.WriteLine($"ECGridOS Soap Exception: {ECG_Ex.ErrorCode} , Item: {ECG_Ex.ErrorItem}, Message: {ECG_Ex.ErrorMessage}, String: {ECG_Ex.ErrorString}");
}
} // END USING
}
catch (Exception ex){ Console.WriteLine("Unhandled Exception: " + ex.ToString()); }
Console.WriteLine("Press any Key to quit...");
Console.ReadKey();
} // END MAIN
} // END CLASS
} // END NAMESPACE
using System;
using System.Windows.Forms;
using ECGridService = ECGridOS_Async.net.ecgridos;
namespace ECGridOS_Async
{
public partial class Form1 : Form
{
ECGridService.ECGridOSAPIv3 ECGrid = new ECGridService.ECGridOSAPIv3();
public Form1()
{
InitializeComponent();
ECGrid.WhoAmICompleted += new ECGridService.WhoAmICompletedEventHandler(ECGrid_WhoAmICompleted);
}
private void WhoAmI_Button_Click(object sender, EventArgs e)
{
string SessionID = "00000000-0000-0000-0000-000000000000";
ECGrid.WhoAmIAsync(SessionID);
}
void ECGrid_WhoAmICompleted(object sender, ECGridService.WhoAmICompletedEventArgs e)
{
var results = e.Result;
WhoAmITextBox.Text = $"{results.FirstName} {results.LastName}";
}
}
}