private async Task <SampleCustomer> UpdateItemAsync(DocumentClient client, Uri collectionUri, SampleCustomer item) { //DeepCopy the item item = Helpers.Clone(item); //Make a change to the item to update. item.Region = Helpers.ParseEndpoint(client.WriteEndpoint); item.UserDefinedId = Helpers.RandomNext(0, 1000); Console.WriteLine($"Update - Name: {item.Name}, City: {item.City}, UserDefId: {item.UserDefinedId}, Region: {item.Region}"); try { var response = await client.ReplaceDocumentAsync(item.SelfLink, item, new RequestOptions { AccessCondition = new AccessCondition { Type = AccessConditionType.IfMatch, Condition = item.ETag } }); return((SampleCustomer)(dynamic)response.Resource); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.PreconditionFailed || ex.StatusCode == HttpStatusCode.NotFound) { //No conflict is induced. return(null); } throw; } }
private async Task <SampleCustomer> InsertItemAsync(DocumentClient client, Uri collectionUri, SampleCustomer item) { //DeepCopy the item item = Helpers.Clone(item); //Update UserDefinedId for each item to random number for Conflict Resolution item.UserDefinedId = Helpers.RandomNext(0, 1000); //Update the write region to the client regions so we know which client wrote the item item.Region = Helpers.ParseEndpoint(client.WriteEndpoint); Console.WriteLine($"Attempting insert - Name: {item.Name}, City: {item.City}, UserDefId: {item.UserDefinedId}, Region: {item.Region}"); try { var response = await client.CreateDocumentAsync(collectionUri, item); return((SampleCustomer)(dynamic)response.Resource); } catch (DocumentClientException ex) { if (ex.StatusCode == System.Net.HttpStatusCode.Conflict) { //Item has already replicated so return null return(null); } throw; } }