/// <summary> /// Callback for the Download HttpWebRequest.beginGetRequestStream /// </summary> private void WriteDownloadRequestStream(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 = new ODataAtomWriter(BaseUri); syncWriter.StartFeed(wrapper.CacheRequest.IsLastBatch, wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); 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; } }
/// <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; } }
/// <summary> /// Delegate passed into the custom body writer to form the outgoing response. /// </summary> /// <param name="writer"></param> /// <param name="syncWriter"></param> private static void WriteResponse(XmlDictionaryWriter writer, SyncWriter syncWriter) { try { syncWriter.WriteFeed(writer); } catch (Exception exception) { // An exception at this point seems to be unrecoverable but ideally we should not hit exceptions since we are only // writing to the XmlDictionaryWriter. SyncServiceTracer.TraceError("Exception in WriteResponse method. Details: {0}", WebUtil.GetExceptionMessage(exception)); } }
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)); }