/// <summary>
 /// Write the header to a byte stream.
 /// </summary>
 /// <param name="header">The header.</param>
 /// <returns>The serialized data.</returns>
 private static byte[] Write(MiniProfilerResultsHeader header)
 {
     var serializer = new DataContractSerializer(typeof(MiniProfilerResultsHeader), HeaderName, HeaderNamespace);
     using (var stream = new MemoryStream())
     {
         using (var writer = new XmlTextWriter(stream, Encoding.Unicode))
         {
             writer.WriteStartElement(HeaderName, HeaderNamespace);
             serializer.WriteObjectContent(writer, header);
             writer.WriteEndElement();
             writer.Flush();
             byte[] buffer = stream.GetBuffer();
             Array.Resize(ref buffer, (int)stream.Length);
             return buffer;
         }
     }            
 }
        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            var requestHeader = correlationState as MiniProfilerRequestHeader;
            MiniProfiler.Stop();
            var miniProfiler = MiniProfiler.Current;

            if (miniProfiler != null && requestHeader != null)
            {
                if (requestHeader.ExcludeTrivialMethods)
                {
                    miniProfiler.Root.RemoveTrivialTimings();
                }

                var header = new MiniProfilerResultsHeader
                {
                    ProfilerResults = miniProfiler
                };

                if (reply.Headers.MessageVersion != MessageVersion.None)
                {
                    var untypedHeader = new MessageHeader<MiniProfilerResultsHeader>(header)
                    .GetUntypedHeader(MiniProfilerResultsHeader.HeaderName, MiniProfilerResultsHeader.HeaderNamespace);

                    reply.Headers.Add(untypedHeader);
                }
                else if (_http || reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
                {
                    _http = true;

                    HttpResponseMessageProperty property = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
                    string text = header.ToHeaderText();
                    property.Headers.Add(MiniProfilerResultsHeader.HeaderName, text);
                }
                else
                    throw new InvalidOperationException("MVC Mini Profiler does not support EnvelopeNone unless HTTP is the transport mechanism");
            }

            //try
            //{
            //    var arrayOfIds = Settings.Storage.GetUnviewedIds(current.User).ToJson();
            //    // allow profiling of ajax requests
            //    response.AppendHeader("X-MiniProfiler-Ids", arrayOfIds);
            //}
            //catch { } // headers blew up
        }
        /// <summary>
        /// before the reply is sent.
        /// </summary>
        /// <param name="reply">The reply.</param>
        /// <param name="correlationState">The correlation state.</param>
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            var requestHeader = correlationState as MiniProfilerRequestHeader;
            MiniProfiler.Stop();
            var miniProfiler = MiniProfiler.Current;

            if (miniProfiler != null && requestHeader != null)
            {
                if (requestHeader.ExcludeTrivialMethods)
                {
                    miniProfiler.Root.RemoveTrivialTimings();
                }

                var header = new MiniProfilerResultsHeader
                {
                    ProfilerResults = miniProfiler
                };

                // ReSharper disable PossibleUnintendedReferenceComparison
                if (reply.Headers.MessageVersion != MessageVersion.None)
                // ReSharper restore PossibleUnintendedReferenceComparison
                {
                    var untypedHeader = new MessageHeader<MiniProfilerResultsHeader>(header)
                    .GetUntypedHeader(MiniProfilerResultsHeader.HeaderName, MiniProfilerResultsHeader.HeaderNamespace);

                    reply.Headers.Add(untypedHeader);
                }
                else if (_http || reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
                {
                    _http = true;

                    var property = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
                    string text = header.ToHeaderText();
                    property.Headers.Add(MiniProfilerResultsHeader.HeaderName, text);
                }
                else
                    throw new InvalidOperationException("MVC Mini Profiler does not support EnvelopeNone unless HTTP is the transport mechanism");
            }
        }