static IOutgoingAttachments GetAttachments(this ExtendableOptions options)
        {
            var contextBag = options.GetExtensions();

            if (contextBag.TryGet <IOutgoingAttachments>(out var attachments))
            {
                return(attachments);
            }

            attachments = new OutgoingAttachments();
            contextBag.Set(attachments);
            return(attachments);
        }
Beispiel #2
0
        static IOutgoingAttachments GetAttachments(this ExtendableOptions options)
        {
            Guard.AgainstNull(options, nameof(options));
            var contextBag = options.GetExtensions();

            // check the context for a IOutgoingAttachments in case a mocked instance is injected for testing
            if (contextBag.TryGet <IOutgoingAttachments>(out var attachments))
            {
                return(attachments);
            }

            attachments = new OutgoingAttachments();
            contextBag.Set(attachments);
            return(attachments);
        }
    static async Task WriteMultipart(
        IDocumentWriter writer,
        HttpResponse response,
        ExecutionResult result,
        OutgoingAttachments attachments,
        CancellationToken cancellation)
    {
        List <HttpContent> httpContents = new();

        try
        {
            using MultipartFormDataContent multipart = new()
                  {
                      //TODO: no point doing ToString
                      { new StringContent(await writer.WriteToStringAsync(result)), "result" }
                  };

            foreach (var attachment in attachments.Inner)
            {
                httpContents.Add(await AddAttachment(attachment, multipart, cancellation));
            }

            response.ContentLength = multipart.Headers.ContentLength;
            response.ContentType   = multipart.Headers.ContentType?.ToString();
            await multipart.CopyToAsync(response.Body, cancellation);
        }
        finally
        {
            foreach (var httpContent in httpContents)
            {
                httpContent.Dispose();
            }

            foreach (var cleanup in attachments.Inner.Select(x => x.Value.Cleanup))
            {
                cleanup?.Invoke();
            }
        }
    }
Beispiel #4
0
        static async Task WriteMultipart(
            HttpResponse response,
            ExecutionResult result,
            OutgoingAttachments attachments,
            CancellationToken cancellation)
        {
            var httpContents = new List <HttpContent>();

            try
            {
                using var multipart = new MultipartFormDataContent();
                var serializedResult = JsonConvert.SerializeObject(result);
                multipart.Add(new StringContent(serializedResult));

                foreach (var attachment in attachments.Inner)
                {
                    httpContents.Add(await AddAttachment(attachment, multipart, cancellation));
                }

                response.ContentLength = multipart.Headers.ContentLength;
                response.ContentType   = multipart.Headers.ContentType.ToString();
                await multipart.CopyToAsync(response.Body);
            }
            finally
            {
                foreach (var httpContent in httpContents)
                {
                    httpContent.Dispose();
                }

                foreach (var cleanup in attachments.Inner.Select(x => x.Value.Cleanup))
                {
                    cleanup?.Invoke();
                }
            }
        }