public async Task <List <Employee> > Get(int offset, int limit) { // Set the Accept header. _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_dataFormat)); string url = @"http://localhost:" + _dataWarehousePortNumber + "/api/employees/?offset= " + offset + "&limit= " + limit; // Make a remote request. HttpResponseMessage response = await _httpClient.GetAsync(url); // Read response body and deserialize it. string body = await response.Content.ReadAsStringAsync(); if (_dataFormat == "application/xml") { // Validate XML document. ValidateXmlSchema(body); // Deserialize XML document. Employee[] employees = await UtilityXml.DeserializeXmlAsync <Employee[]>(body); return(employees.ToList()); } if (_dataFormat == "application/json") { Employee[] employees = await UtilityJson.DeserializeJsonAsync <Employee[]>(body); return(employees.ToList()); } // Return empty list. return(new List <Employee>()); }
public async Task <Employee> Get(int id) { // Set the Accept header. _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_dataFormat)); string url = @"http://localhost:{_dataWarehousePortNumber}/api/employees/{id}"; // Make a remote request. HttpResponseMessage response = await _httpClient.GetAsync(url); // Read response body and deserialize it. string body = await response.Content.ReadAsStringAsync(); if (_dataFormat == "application/xml") { // Validate XML document. ValidateXmlSchema(body); // Deserialize XML document. Employee employee = await UtilityXml.DeserializeXmlAsync <Employee>(body); return(employee); } if (_dataFormat == "application/json") { Employee employee = await UtilityJson.DeserializeJsonAsync <Employee>(body); return(employee); } return(null); }
private static void Main(string[] args) { Console.WriteLine("Server application..."); Thread.Sleep(1000); Task t = Task.Run(async() => { // === Read settings === // Emplyees file name. string employeesFileName = args[0]; // Server port. string serverPortSetting = args[1]; int serverPort = int.Parse(serverPortSetting); // Data format. string dataFormat = args[2]; // Read Employees data from the XML file. string employeesDataText = File.ReadAllText("D:\\DistributionCollections\\" + employeesFileName); Employee[] employees = await UtilityXml.DeserializeXmlAsync <Employee[]>(employeesDataText, rootElementName: "Employees"); // Add Employees to the repository. employees.ToList().ForEach(e => { EmployeeRepository.Instance.Add(e); }); // PUT all employees data to the DataWarehouse. foreach (Employee employee in employees) { await SendEmployee(employee, dataFormat, serverPort); } // Run the HTTP Transport Service. var httpTransportService = new HttpTransportService(serverPort, dataFormat); httpTransportService.Start(); Thread.Sleep(60000); httpTransportService.Stop(); }); t.Wait(); Console.ReadLine(); }