Example #1
0
        /// <summary>
        /// Write a single document returned from a compiled query to the
        /// given HttpContext
        /// </summary>
        /// <param name="session"></param>
        /// <param name="query"></param>
        /// <param name="context"></param>
        /// <param name="contentType"></param>
        /// <typeparam name="TDoc"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        public static async Task WriteOne <TDoc, TOut>(this IQuerySession session, ICompiledQuery <TDoc, TOut> query, HttpContext context, string contentType = "application/json")
        {
            var stream = new MemoryStream();
            var found  = await session.StreamJsonOne(query, stream, context.RequestAborted).ConfigureAwait(false);

            if (found)
            {
                context.Response.StatusCode    = 200;
                context.Response.ContentLength = stream.Length;
                context.Response.ContentType   = contentType;

                stream.Position = 0;
                await stream.CopyToAsync(context.Response.Body).ConfigureAwait(false);
            }
            else
            {
                context.Response.StatusCode    = 404;
                context.Response.ContentLength = 0;
            }
        }