/// <summary> /// Callback for the Upload HttpWebRequest.beginGetRequestStream /// </summary> private void WriteUploadRequestStream(Stream requestStream, AsyncArgsWrapper wrapper) { try { // Create a SyncWriter to write the contents this.syncWriter = (SerializationFormat == SerializationFormat.ODataAtom) ? new ODataAtomWriter(BaseUri) : (SyncWriter) new ODataJsonWriter(BaseUri); syncWriter.StartFeed(wrapper.CacheRequest.IsLastBatch, wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); foreach (IOfflineEntity entity in wrapper.CacheRequest.Changes) { // Skip tombstones that dont have a ID element. if (entity.GetServiceMetadata().IsTombstone&& string.IsNullOrEmpty(entity.GetServiceMetadata().Id)) { continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty(entity.GetServiceMetadata().Id)) { if (wrapper.TempIdToEntityMapping == null) { wrapper.TempIdToEntityMapping = new Dictionary <string, IOfflineEntity>(); } tempId = Guid.NewGuid().ToString(); wrapper.TempIdToEntityMapping.Add(tempId, entity); } syncWriter.AddItem(entity, tempId); } if (SerializationFormat == SerializationFormat.ODataAtom) { syncWriter.WriteFeed(XmlWriter.Create(requestStream)); } else { this.syncWriter.WriteFeed(new XmlJsonWriter(requestStream)); } requestStream.Flush(); } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; } }
// This method ensures that we have a valid feed through the formatters. The BodyWriter delegate in WCF // seems to not recover from an unhandled exception caused by the formatters and sends out an empty response to the caller. // This is a workaround for this issue until we find a better solution. private SyncWriter GetSyncWriterWithContents() { // Get the appropriate SyncWriter instance based on the serialization format. SyncWriter oDataWriter = WebUtil.GetSyncWriter(_responseSerializationFormat, _baseUri); oDataWriter.StartFeed(_getChangesResponse.IsLastBatch, _getChangesResponse.ServerBlob); // Write entities for response. foreach (var entity in _getChangesResponse.EntityList) { // Set the Id and add item with a null tempId. entity.ServiceMetadata.Id = WebUtil.GenerateOfflineEntityId(entity); oDataWriter.AddItem(entity, null /*tempId*/); } return(oDataWriter); }
NSData CreateRequestBody() { _syncWriter = (SyncWriter) new ODataAtomWriter(base.BaseUri); _syncWriter.StartFeed(_wrapper.CacheRequest.IsLastBatch, _wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); MemoryStream requestStream = new MemoryStream(); if (_wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) { foreach (IOfflineEntity entity in _wrapper.CacheRequest.Changes) { // Skip tombstones that dont have a ID element. if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { if (_wrapper.TempIdToEntityMapping == null) { _wrapper.TempIdToEntityMapping = new Dictionary <string, IOfflineEntity> (); } tempId = Guid.NewGuid().ToString(); _wrapper.TempIdToEntityMapping.Add(tempId, entity); } _syncWriter.AddItem(entity, tempId); } } if (base.SerializationFormat == SerializationFormat.ODataAtom) { _syncWriter.WriteFeed(XmlWriter.Create(requestStream)); } else { _syncWriter.WriteFeed(JsonReaderWriterFactory.CreateJsonWriter(requestStream)); } string result = ""; requestStream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(requestStream)) { result = reader.ReadToEnd(); } requestStream.Flush(); requestStream.Close(); NSString dataString = new NSString(result); return(dataString.DataUsingEncoding(NSStringEncoding.UTF8)); }