public async Task Test_Batch() { List <Contact> contacts = CreateContacts(); var provider = _cacheProvider as RedisCacheProvider; IBatchProvider batch = await provider.CreateBatch() as IBatchProvider; Task writeTask1 = batch.SetAsync("People", people); Task writeTask2 = batch.SetAsync("Contacts", contacts); batch.Execute(); await Task.WhenAll(writeTask1, writeTask2); Task <List <Person> > readTask1 = batch.GetAsync <List <Person> >("People"); Task <List <Contact> > readTask2 = batch.GetAsync <List <Contact> >("Contacts"); batch.Execute(); await Task.WhenAll(readTask1, readTask2); var resultPeople = await readTask1; var resultContacts = await readTask2; Assert.IsNotNull(resultPeople); Assert.AreEqual(people.Count, resultPeople.Count); Assert.IsTrue(people.TrueForAll(p => p.Name.Equals(resultPeople.Find(f => p.Id.Equals(f.Id)).Name))); Assert.IsTrue(people.TrueForAll(p => p.Contacts.Count == resultPeople.Find(f => p.Id.Equals(f.Id)).Contacts.Count)); Assert.IsTrue(people.TrueForAll(p => p.Contacts. TrueForAll(c => c.Value.Equals(resultPeople.Find(f => p.Id.Equals(f.Id)).Contacts.Find(f => c.Type.Equals(f.Type)).Value)))); Assert.AreEqual(contacts.Count, resultContacts.Count); Assert.IsTrue(contacts. TrueForAll(c => c.Value.Equals(resultContacts.Find(f => c.Type.Equals(f.Type)).Value))); }
public async Task <ServiceLayerResponse> Post(IBatchProducer batch, bool returnContent = false) { MultipartContent multipart = new MultipartContent("mixed"); StringBuilder messageBuilder = new StringBuilder(512); string messageTemplate = string.Empty; messageBuilder.AppendLine("{0} {1}"); messageBuilder.AppendLine("Content-Type: application/json; charset=utf-8"); if (!returnContent) { messageBuilder.AppendLine("Prefer: return-no-content"); } messageBuilder.AppendLine(""); messageBuilder.AppendLine("{2}"); messageBuilder.AppendLine(""); messageTemplate = messageBuilder.ToString(); int index = 0; IBatchProvider provider = (IBatchProvider)batch; foreach (var s in provider.Items) { index++; string message = string.Format(messageTemplate, s.method.Method, s.query, s.payload); StringContent json = new StringContent(message, Encoding.UTF8, "application/json"); json.Headers.ContentType = new MediaTypeHeaderValue("application/http"); json.Headers.Add("Content-Transfer-Encoding", "binary"); json.Headers.Add("Content-ID", index.ToString()); multipart.Add(json); _logger.LogInformation($"POST (batch) {_SLServer}{s.query}"); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug(s.payload); } } return(await Send(HttpMethod.Post, "$batch", multipart, false)); }