/// <summary>
 /// Prepares the cached entry to be consumed by the caller, notably by setting the content.
 /// </summary>
 /// <param name="request">The request that invoked retrieving this response and need to be attached to the response.</param>
 /// <param name="cachedData">The cache data that hold the data.</param>
 /// <returns>A valid HttpResponseMessage that can be consumed by the caller of this message handler.</returns>
 private static HttpResponseMessage PrepareCachedEntry(HttpRequestMessage request, CacheData cachedData)
 {
     HttpResponseMessage copy = cachedData.CachableResponse.CopyCachable();
     copy.Content = new ByteArrayContent(cachedData.Data);
     copy.RequestMessage = request;
     return copy;
 }
 /// <summary>
 /// Takes an HttpResponseMessage and converts that to a <see cref="CacheData"/>.
 /// </summary>
 /// <param name="response">The response to put into the cache.</param>
 /// <returns>A cache entry that can be placed into the cache.</returns>
 public static async Task<CacheData> ToCacheEntry(this HttpResponseMessage response)
 {
     var data = await response.Content.ReadAsByteArrayAsync();
     var copy = response.CopyCachable();
     var entry = new CacheData(data, copy);
     return entry;
 }
        /// <summary>
        /// Prepares the cached entry to be consumed by the caller, notably by setting the content.
        /// </summary>
        /// <param name="request">The request that invoked retrieving this response and need to be attached to the response.</param>
        /// <param name="cachedData">The deserialized data from the cache.</param>
        /// <returns>A valid HttpResponseMessage that can be consumed by the caller of this message handler.</returns>
        public static HttpResponseMessage PrepareCachedEntry(this HttpRequestMessage request, CacheData cachedData)
        {
            var response = cachedData.CachableResponse;

            if (cachedData.Headers != null)
            {
                foreach (var kvp in cachedData.Headers)
                {
                    response.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
                }
            }

            response.Content = new ByteArrayContent(cachedData.Data);
            if (cachedData.ContentHeaders != null)
            {
                foreach (var kvp in cachedData.ContentHeaders)
                {
                    response.Content.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
                }
            }
            response.RequestMessage = request;
            return(response);
        }