/// <inheritdoc /> public async Task SerializeResult(ISerializedResult toSerialize, IContentTypeProvider contentTypeProvider, CancellationToken cancellationToken) { switch (toSerialize.Result) { case Head head: head.Headers["EntityCount"] = head.EntityCount.ToString(); return; case Binary binary: await binary.BinaryResult.WriteToStream(toSerialize.Body, cancellationToken).ConfigureAwait(false); return; case IEntities <object> entities and Content content: await SerializeContentDataCollection((dynamic)entities, content, toSerialize, contentTypeProvider, cancellationToken).ConfigureAwait(false); break; case Report report: await SerializeContentDataCollection((dynamic)report.ToAsyncSingleton(), report, toSerialize, contentTypeProvider, cancellationToken).ConfigureAwait(false); break; case Error error: await SerializeError(error, toSerialize, contentTypeProvider, cancellationToken).ConfigureAwait(false); break; default: return; } }
private static Response ToResponse(ISerializedResult result) { var response = new Response { StatusCode = (ushort)result.StatusCode, StatusDescription = result.StatusDescription }; if (result.Body != null) { if (result.Headers.ContentType.HasValue) { response.ContentType = result.Headers.ContentType.ToString(); } if (result.Body.CanSeek && result.Body.Length > 0) { response.StreamedBody = result.Body; } else { var stream = new MemoryStream(); using (result.Body) result.Body.CopyTo(stream); if (stream.Position > 0) { stream.Seek(0, SeekOrigin.Begin); response.StreamedBody = stream; } else { stream.Dispose(); } } } result.Headers.ForEach(header => response.Headers[header.Key] = header.Value); response.Cookies = result.Cookies as List <string> ?? response.Cookies.ToList(); return(response); }
/// <inheritdoc /> public void StreamResult(ISerializedResult result, int messageSize, TimeSpan?timeElapsed = null, bool writeHeaders = false, bool disposeResult = true) { if (result == null) { throw new ArgumentNullException(nameof(result)); } if (!result.IsSerialized) { result = result.Serialize(); } if (!(result is Content content) || !(content.Body?.Length > 0)) { SendResult(result, result.TimeElapsed, writeHeaders, disposeResult); return; } if (content.IsLocked) { throw new InvalidOperationException("Unable to stream a result that is already assigned to a different streaming " + "job. A result can only be streamed once."); } content.IsLocked = true; TerminalConnection?.Suspend(); if (messageSize < MinStreamBufferSize) { messageSize = MinStreamBufferSize; } else if (MaxStreamBufferSize < messageSize) { messageSize = MaxStreamBufferSize; } StreamManifest?.Dispose(); StreamManifest = new StreamManifest(content, messageSize); SendJson(StreamManifest); buffer = null; }
public async Task StreamSerializedResult(ISerializedResult r, int m, TimeSpan?t = null, bool w = false, bool d = true) { await WaitTask.ConfigureAwait(false); await WebSocket.StreamSerializedResult(r, m, t, w, d).ConfigureAwait(false); }
public async Task SendSerializedResult(ISerializedResult serializedResult, TimeSpan?t = null, bool w = false, bool d = true) { await WaitTask.ConfigureAwait(false); await WebSocket.SendSerializedResult(serializedResult, t, w, d).ConfigureAwait(false); }
/// <summary> /// Serializes the given result object to this body, using the appropriate content type /// provider assigned to the request or response that this body belongs to. /// </summary> /// <param name="result"></param> public async Task Serialize(ISerializedResult result, CancellationToken cancellationToken) { await ProtocolHolder.CachedProtocolProvider.ProtocolProvider.SerializeResult(result, ContentTypeProvider, cancellationToken).ConfigureAwait(false); TryRewind(); }
private async Task SerializeContentDataCollection <T>(IAsyncEnumerable <T> dataCollection, Content content, ISerializedResult toSerialize, IContentTypeProvider contentTypeProvider, CancellationToken cancellationToken) where T : class { content.SetContentDisposition(contentTypeProvider.ContentDispositionFileExtension); if (contentTypeProvider is not IJsonProvider jsonProvider) { await contentTypeProvider.SerializeCollection(dataCollection, toSerialize.Body, content.Request, cancellationToken).ConfigureAwait(false); return; } var swr = new StreamWriter(toSerialize.Body, Encoding.UTF8, 4096, true); #if NETSTANDARD2_1 await using (swr) #else using (swr) #endif { using var jwr = JsonProvider.GetJsonWriter(swr); await jwr.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("Status", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync("success", cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("ResourceType", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(content.ResourceType.FullName, cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("Data", cancellationToken).ConfigureAwait(false); var entityCount = await jsonProvider.SerializeCollection(dataCollection, jwr, cancellationToken).ConfigureAwait(false); toSerialize.EntityCount = entityCount; await jwr.WritePropertyNameAsync("DataCount", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(entityCount, cancellationToken).ConfigureAwait(false); if (content is IEntities entities) { if (toSerialize.HasPreviousPage) { var previousPageLink = entities.GetPreviousPageLink(toSerialize.EntityCount); await jwr.WritePropertyNameAsync("PreviousPage", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(previousPageLink.ToUriString(), cancellationToken).ConfigureAwait(false); } if (toSerialize.HasNextPage) { var nextPageLink = entities.GetNextPageLink(toSerialize.EntityCount, -1); await jwr.WritePropertyNameAsync("NextPage", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(nextPageLink.ToUriString(), cancellationToken).ConfigureAwait(false); } } await jwr.WritePropertyNameAsync("TimeElapsedMs", cancellationToken).ConfigureAwait(false); var milliseconds = toSerialize.TimeElapsed.GetRESTableElapsedMs(); await jwr.WriteValueAsync(milliseconds, cancellationToken).ConfigureAwait(false); await jwr.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); if (entityCount == 0) { content.MakeNoContent(); } } }
private async Task SerializeError(Error error, ISerializedResult toSerialize, IContentTypeProvider contentTypeProvider, CancellationToken cancellationToken) { if (contentTypeProvider is not IJsonProvider) { return; } var swr = new StreamWriter(toSerialize.Body, Encoding.UTF8, 4096, true); #if NETSTANDARD2_1 await using (swr) #else using (swr) #endif { using var jwr = JsonProvider.GetJsonWriter(swr); await jwr.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("Status", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync("fail", cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("ErrorType", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(error.GetType().FullName, cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("Data", cancellationToken).ConfigureAwait(false); await jwr.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (error is InvalidInputEntity failedValidation) { foreach (var invalidMember in failedValidation.InvalidEntity.InvalidMembers) { await jwr.WritePropertyNameAsync(invalidMember.MemberName, cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(invalidMember.Message, cancellationToken).ConfigureAwait(false); } await jwr.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); if (failedValidation.InvalidEntity.Index is long index) { await jwr.WritePropertyNameAsync("InvalidEntityIndex", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(index, cancellationToken).ConfigureAwait(false); } } else { await jwr.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); } await jwr.WritePropertyNameAsync("ErrorCode", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(error.ErrorCode.ToString(), cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("Message", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(error.Message, cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("MoreInfoAt", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(error.Headers.Error, cancellationToken).ConfigureAwait(false); await jwr.WritePropertyNameAsync("TimeStamp", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(DateTime.UtcNow.ToString("O"), cancellationToken).ConfigureAwait(false); if (error.Request?.UriComponents is IUriComponents uriComponents) { await jwr.WritePropertyNameAsync("Uri", cancellationToken).ConfigureAwait(false); await jwr.WriteValueAsync(ToUriString(uriComponents), cancellationToken).ConfigureAwait(false); } await jwr.WritePropertyNameAsync("TimeElapsedMs", cancellationToken).ConfigureAwait(false); var milliseconds = toSerialize.TimeElapsed.GetRESTableElapsedMs(); await jwr.WriteValueAsync(milliseconds, cancellationToken).ConfigureAwait(false); await jwr.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); toSerialize.EntityCount = 0; } }
public void StreamResult(ISerializedResult r, int m, TimeSpan?t = null, bool w = false, bool d = true) => ActionQueue.Enqueue(() => ToQueueFor.StreamResult(r, m, t, w, d));
/// <inheritdoc /> protected SerializedResultWrapper(ISerializedResult result) : base(result) { SerializedResult = result; }