public async Task <AddWordResponse> AddWord(string word) { // Determine the partition key that should handle the request long partitionKey = GetPartitionKey(word); // Use service partition client to resolve the service and partition key. // This determines the endpoint of the replica that should handle the request. // Internally, the service partition client handles exceptions and retries appropriately. ServicePartitionClient <CommunicationClient> servicePartitionClient = new ServicePartitionClient <CommunicationClient>( clientFactory, new Uri(WordCountServiceName), partitionKey); var res = await servicePartitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, string.Format("AddWord/{0}", word)); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "PUT"; request.ContentLength = 0; request.Timeout = (int)client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string httpResult = reader.ReadToEnd().Trim(); } var retVal = new AddWordResponse(); retVal.PartitionId = client.ResolvedServicePartition.Info.Id; retVal.ServiceAddress = serviceAddress.ToString(); retVal.Word = word; return(Task.FromResult(retVal)); }); return(res); }
public async Task<AddWordResponse> AddWord(string word) { // Determine the partition key that should handle the request long partitionKey = GetPartitionKey(word); // Use service partition client to resolve the service and partition key. // This determines the endpoint of the replica that should handle the request. // Internally, the service partition client handles exceptions and retries appropriately. ServicePartitionClient<CommunicationClient> servicePartitionClient = new ServicePartitionClient<CommunicationClient>( clientFactory, new Uri(WordCountServiceName), partitionKey); var res = await servicePartitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, string.Format("AddWord/{0}", word)); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "PUT"; request.ContentLength = 0; request.Timeout = (int) client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int) client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string httpResult = reader.ReadToEnd().Trim(); } var retVal = new AddWordResponse(); retVal.PartitionId = client.ResolvedServicePartition.Info.Id; retVal.ServiceAddress = serviceAddress.ToString(); retVal.Word = word; return Task.FromResult(retVal); }); return res; }