/// <summary> /// Returns the list of employees from all the partitions. In our sample we have only 1 partition /// Also, we are making use of proxy to determine the right partition to connect to. /// Please refer this link for more details. https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reverseproxy /// </summary> /// <returns></returns> public async Task <List <Employee> > GetEmployees() { Uri serviceName = EmployeeWeb.GetEmployeeDataServiceName(_context); Uri proxyAddress = this.GetProxyAddress(serviceName); ServicePartitionList partitions = await _fabricClient.QueryManager.GetPartitionListAsync(serviceName); List <Employee> employees = new List <Employee>(); foreach (Partition partition in partitions) { string proxyUrl = $"{proxyAddress}/api/Employee?PartitionKey={((Int64RangePartitionInformation)partition.PartitionInformation).LowKey}&PartitionKind=Int64Range"; using (HttpResponseMessage response = await _httpClient.GetAsync(proxyUrl)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { continue; } employees.AddRange(JsonConvert.DeserializeObject <List <Employee> >(await response.Content.ReadAsStringAsync())); } } return(employees); }
/// <summary> /// Deletes an Employee /// </summary> /// <param name="employee"></param> /// <returns></returns> public async Task DeleteEmployee(long employeeId) { Uri serviceName = EmployeeWeb.GetEmployeeDataServiceName(_context); Uri proxyAddress = this.GetProxyAddress(serviceName); long partitionKey = employeeId; string proxyUrl = $"{proxyAddress}/api/Employee/{employeeId}?PartitionKey={partitionKey}&PartitionKind=Int64Range"; await this._httpClient.DeleteAsync(proxyUrl); }
/// <summary> /// Creates an Employee /// </summary> /// <param name="employee"></param> /// <returns></returns> public async Task CreateEmployee(Employee employee) { Uri serviceName = EmployeeWeb.GetEmployeeDataServiceName(_context); Uri proxyAddress = this.GetProxyAddress(serviceName); long partitionKey = employee.Id; string proxyUrl = $"{proxyAddress}/api/Employee?PartitionKey={partitionKey}&PartitionKind=Int64Range"; await this._httpClient.PostAsJsonAsync <Employee>(proxyUrl, employee); }