FIRST STEPS WITH XAPI, C# AND VISUAL STUDIO
This tutorial shows how to combine xAPI, C# and Visual Studio to create wonderful financial applications. It describes some basic concepts on how to use the .NET wrapper.
Commands used in this tutorial:
- login,
- getServerTime,
- getAllSymbols.
Before you start...
- Download xAPI .NET Wrappers from:
http://developers.xstore.pro/api/wrappers.html, - In this tutorial we will be using Microsoft Visual Studio. If you don't have one installed, you can download a free Express version from:
http://www.microsoft.com/visualstudio/plk/downloads, - To use the wrappers you must have .NET Framework v.4.0 (or higher) installed. You can download it from here:
http://msdn.microsoft.com/netframework/.
Step 1. Create new project
Step 2. Add references in your project
Step 3. Set your project as a startup project
Wrappers are ready now so we can create our first application!
Step 4. Write the code
- Expand your project and double click the Program.cs class
- Add necessary imports at the beginning of the file
- Write the following code in the main function (between '{' and '}' brackets)
- Please remember to change your credentials in the first lines of the code above
using xAPI; using xAPI.Codes; using xAPI.Connection; using xAPI.Commands; using xAPI.Errors; using xAPI.Records; using xAPI.Responses; using xAPI.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
SpecificServer server = Servers.DEMO; string userId = "12345"; string password = "password"; string appName = "firstApp"; // should be left blank // to get more info about appId and appName visit http://developers.xstore.pro/api/tutorials/appid_and_appname string appId = ""; try { // Connect to server APISync apiClient = new APISync(server); Console.WriteLine("Connected to the server"); // Login to server Credentials credentials = new Credentials(userId, password, appId, appName); LoginResponse loginResponse = apiClient.Login(credentials); Console.WriteLine("Logged in as: " + userId); // Execute GetServerTime command GetServerTimeResponse serverTimeResponse = apiClient.GetServerTime(); Console.WriteLine("Server time: " + serverTimeResponse.TimeString); // Execute GetAllSymbols command GetAllSymbolsResponse allSymbolsResponse = apiClient.GetAllSymbols(); Console.WriteLine("All symbols count: " + allSymbolsResponse.SymbolRecords.Count); // Print first 5 symbols Console.WriteLine("First five symbols:"); foreach (SymbolRecord symbolRecord in allSymbolsResponse.SymbolRecords.Take(5)) { Console.WriteLine(" > " + symbolRecord.Symbol + " ask: " + symbolRecord.Ask + " bid: " + symbolRecord.Bid); } } catch (Exception ex) { Console.WriteLine("An exception occured: " + ex.ToString()); } Console.Read();
Step 5. Run the application
Summary
In this tutorial you learned how to prepare Visual Studio to work with xAPI and .NET wrappers. You have also created a very simple application using C# that:
- connects to the xAPI,
- logins to the server,
- gets server time,
- gets list of all available symbols,
- lists first five of them.
Now you are ready to create your own app!