2 // WebOperationContext.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
34 #if NET_2_1 // Note that moonlight System.ServiceModel.Web.dll does not contain this class.
35 using IncomingWebRequestContext = System.Object;
36 using OutgoingWebResponseContext = System.Object;
38 using System.Runtime.Serialization.Json;
39 using System.ServiceModel.Syndication;
41 using System.Xml.Serialization;
44 namespace System.ServiceModel.Web
46 public class WebOperationContext
48 : IExtension<OperationContext>
52 public static WebOperationContext Current {
54 if (OperationContext.Current == null)
56 var ret = OperationContext.Current.Extensions.Find<WebOperationContext> ();
58 ret = new WebOperationContext (OperationContext.Current);
59 OperationContext.Current.Extensions.Add (ret);
66 IncomingWebRequestContext incoming_request;
67 IncomingWebResponseContext incoming_response;
68 OutgoingWebRequestContext outgoing_request;
69 OutgoingWebResponseContext outgoing_response;
71 public WebOperationContext (OperationContext operation)
73 if (operation == null)
74 throw new ArgumentNullException ("operation");
76 outgoing_request = new OutgoingWebRequestContext ();
77 incoming_response = new IncomingWebResponseContext (operation);
79 incoming_request = new IncomingWebRequestContext (operation);
80 outgoing_response = new OutgoingWebResponseContext ();
85 public IncomingWebRequestContext IncomingRequest {
86 get { return incoming_request; }
90 public IncomingWebResponseContext IncomingResponse {
91 get { return incoming_response; }
94 public OutgoingWebRequestContext OutgoingRequest {
95 get { return outgoing_request; }
99 public OutgoingWebResponseContext OutgoingResponse {
100 get { return outgoing_response; }
104 public void Attach (OperationContext context)
109 public void Detach (OperationContext context)
114 #if NET_4_0 && !MOBILE
115 static readonly XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true, Indent = false };
116 XmlSerializer document_serializer, feed_serializer, item_serializer;
118 Message CreateAtom10Response<T> (T obj, ref XmlSerializer serializer)
120 if (serializer == null)
121 serializer = new XmlSerializer (typeof (T));
122 var ms = new MemoryStream ();
123 using (var xw = XmlWriter.Create (ms, settings))
124 serializer.Serialize (xw, obj);
126 return Message.CreateMessage (MessageVersion.None, null, XmlReader.Create (ms));
129 public Message CreateAtom10Response (ServiceDocument document)
131 return CreateAtom10Response<AtomPub10ServiceDocumentFormatter> (new AtomPub10ServiceDocumentFormatter (document), ref document_serializer);
134 public Message CreateAtom10Response (SyndicationFeed feed)
136 return CreateAtom10Response<Atom10FeedFormatter> (new Atom10FeedFormatter (feed), ref feed_serializer);
139 public Message CreateAtom10Response (SyndicationItem item)
141 return CreateAtom10Response<Atom10ItemFormatter> (new Atom10ItemFormatter (item), ref item_serializer);
144 public Message CreateJsonResponse<T> (T instance)
146 return CreateJsonResponse<T> (instance, new DataContractJsonSerializer (typeof (T)));
149 public Message CreateJsonResponse<T> (T instance, DataContractJsonSerializer serializer)
151 return Message.CreateMessage (MessageVersion.None, null, instance, serializer);