Exemplo n.º 1
0
        public Stream GetPhotoContent(int id)
        {
            WebOperationContext context = WebOperationContext.Current;

            StreamingServicePhoto photo = StreamingServiceContext.GetPhoto(id);

            if (photo == null)
            {
                context.OutgoingResponse.SetStatusAsNotFound();
                return(new MemoryStream());
            }

            if (context.IncomingRequest.Headers[HttpRequestHeader.Accept] == "test/ReplyWithHeaders")
            {
                XDocument doc = StreamingServiceHelpers.SerializeRequestHeaders(context.IncomingRequest.Headers);

                context.OutgoingResponse.Headers[HttpResponseHeader.ContentEncoding] = "UTF-8";
                MemoryStream      s  = new MemoryStream();
                XmlWriterSettings ws = new XmlWriterSettings()
                {
                    OmitXmlDeclaration = true,
                    Encoding           = Encoding.UTF8
                };

                using (XmlWriter w = XmlWriter.Create(s, ws))
                {
                    doc.Save(w);
                }

                context.OutgoingResponse.ContentLength = s.Length;
                s.Seek(0, SeekOrigin.Begin);
                return(s);
            }

            if (context.IncomingRequest.Headers[HttpRequestHeader.Accept] == "test/AddTestHeaders")
            {
                context.OutgoingResponse.Headers.Add("test-single", "value1");
                context.OutgoingResponse.Headers.Add("test-multi", "value2");
                context.OutgoingResponse.Headers.Add("test-multi", "value3");
            }

            if (context.IncomingRequest.Headers[HttpRequestHeader.Accept] == "test/AddContentPropertyHeaders")
            {
                context.OutgoingResponse.Headers.Add(HttpResponseHeader.ContentType, "test/ContentType");
                context.OutgoingResponse.Headers.Add("Content-Disposition", "test-content-disposition");
            }

            if (context.OutgoingResponse.ContentType == null)
            {
                if (photo.ContentType != "no-content-type")
                {
                    context.OutgoingResponse.ContentType = photo.ContentType;
                }
            }

            context.OutgoingResponse.ContentLength = photo.Content.Length;
            return(new MemoryStream(photo.Content));
        }
Exemplo n.º 2
0
        protected override void OnStartProcessingRequest(ProcessRequestArgs args)
        {
            base.OnStartProcessingRequest(args);
            WebOperationContext context = WebOperationContext.Current;

            if (context != null)
            {
                if (!args.IsBatchOperation)
                {
                    StreamingServiceContext.AddRequestHeaders(context.IncomingRequest.Method, args.RequestUri.ToString(),
                                                              StreamingServiceHelpers.SerializeRequestHeaders(context.IncomingRequest.Headers));
                }
                if (context.IncomingRequest.Headers["Test_FailThisRequest"] == "true")
                {
                    throw new InvalidOperationException("Request failed for testing purposes.");
                }
            }
        }