2 // TypedMessageConverter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2007 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.
28 using System.Collections.Generic;
29 using System.Collections.ObjectModel;
30 using System.Reflection;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Dispatcher;
34 using System.Runtime.Serialization;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
39 namespace System.ServiceModel.Description
41 internal class DefaultTypedMessageConverter : TypedMessageConverter
43 IClientMessageFormatter formatter;
45 public DefaultTypedMessageConverter (IClientMessageFormatter formatter)
47 this.formatter = formatter;
50 public override object FromMessage (Message message)
52 return formatter.DeserializeReply (message, null);
55 public override Message ToMessage (object typedMessage)
57 return ToMessage (typedMessage, MessageVersion.Default);
60 public override Message ToMessage (
61 object typedMessage, MessageVersion version)
63 return formatter.SerializeRequest (version, new object [] {typedMessage});
67 public abstract class TypedMessageConverter
69 internal const string TempUri = "http://tempuri.org/";
71 protected TypedMessageConverter ()
75 public static TypedMessageConverter Create (
76 Type type, string action)
78 return Create (type, action, TempUri);
81 public static TypedMessageConverter Create (
82 Type type, string action,
83 string defaultNamespace)
85 return Create (type, action, defaultNamespace, (DataContractFormatAttribute)null);
88 public static TypedMessageConverter Create (
89 Type type, string action,
90 DataContractFormatAttribute formatterAttribute)
92 return Create (type, action, TempUri, formatterAttribute);
95 public static TypedMessageConverter Create (
97 string action, string defaultNamespace,
98 DataContractFormatAttribute formatterAttribute)
100 return new DefaultTypedMessageConverter (
101 new DataContractMessagesFormatter (
102 MessageContractToMessagesDescription (type, defaultNamespace, action),
103 formatterAttribute));
106 public static TypedMessageConverter Create (
107 Type type, string action,
108 XmlSerializerFormatAttribute formatterAttribute)
110 return Create (type, action, TempUri, formatterAttribute);
113 public static TypedMessageConverter Create (
114 Type type, string action, string defaultNamespace,
115 XmlSerializerFormatAttribute formatterAttribute)
117 return new DefaultTypedMessageConverter (
118 new XmlMessagesFormatter (
119 MessageContractToMessagesDescription (type, defaultNamespace, action),
120 formatterAttribute));
123 public abstract object FromMessage (Message message);
125 public abstract Message ToMessage (object typedMessage);
127 public abstract Message ToMessage (object typedMessage, MessageVersion version);
129 static MessageDescriptionCollection MessageContractToMessagesDescription (
130 Type src, string defaultNamespace, string action)
132 MessageContractAttribute mca =
133 ContractDescriptionGenerator.GetMessageContractAttribute (src);
136 throw new ArgumentException (String.Format ("Type {0} and its ancestor types do not have MessageContract attribute.", src));
138 MessageDescriptionCollection messages = new MessageDescriptionCollection ();
139 // FIXME: not sure if isDirectionInput arguments are ignorable (and can be dummy) here...
140 messages.Add (ContractDescriptionGenerator.CreateMessageDescription (src, defaultNamespace, action, true, false, mca));
141 messages.Add (ContractDescriptionGenerator.CreateMessageDescription (src, defaultNamespace, action, false, false, mca));