Fixed.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Description / TypedMessageConverter.cs
1 //
2 // TypedMessageConverter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2007 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
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;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
38
39 namespace System.ServiceModel.Description
40 {
41         internal class DefaultTypedMessageConverter : TypedMessageConverter
42         {
43                 IClientMessageFormatter formatter;
44
45                 public DefaultTypedMessageConverter (IClientMessageFormatter formatter)
46                 {
47                         this.formatter = formatter;
48                 }
49
50                 public override object FromMessage (Message message)
51                 {
52                         return formatter.DeserializeReply (message, null);
53                 }
54
55                 public override Message ToMessage (object typedMessage)
56                 {
57                         return ToMessage (typedMessage, MessageVersion.Default);
58                 }
59
60                 public override Message ToMessage (
61                         object typedMessage, MessageVersion version)
62                 {
63                         return formatter.SerializeRequest (version, new object [] {typedMessage});
64                 }
65         }
66
67         public abstract class TypedMessageConverter
68         {
69                 internal const string TempUri = "http://tempuri.org/";
70
71                 protected TypedMessageConverter ()
72                 {
73                 }
74
75                 public static TypedMessageConverter Create (
76                         Type type, string action)
77                 {
78                         return Create (type, action, TempUri);
79                 }
80
81                 public static TypedMessageConverter Create (
82                         Type type, string action,
83                         string defaultNamespace)
84                 {
85                         return Create (type, action, defaultNamespace, (DataContractFormatAttribute)null);
86                 }
87
88                 public static TypedMessageConverter Create (
89                         Type type, string action,
90                         DataContractFormatAttribute formatterAttribute)
91                 {
92                         return Create (type, action, TempUri, formatterAttribute);
93                 }
94
95                 public static TypedMessageConverter Create (
96                         Type type,
97                         string action, string defaultNamespace,
98                         DataContractFormatAttribute formatterAttribute)
99                 {
100                         return new DefaultTypedMessageConverter (
101                                 new DataContractMessagesFormatter (
102                                         MessageContractToMessagesDescription (type, defaultNamespace, action),
103                                         formatterAttribute));
104                 }
105
106                 public static TypedMessageConverter Create (
107                         Type type, string action,
108                         XmlSerializerFormatAttribute formatterAttribute)
109                 {
110                         return Create (type, action, TempUri, formatterAttribute);
111                 }
112
113                 public static TypedMessageConverter Create (
114                         Type type, string action, string defaultNamespace,
115                         XmlSerializerFormatAttribute formatterAttribute)
116                 {
117                         return new DefaultTypedMessageConverter (
118                                 new XmlMessagesFormatter (
119                                         MessageContractToMessagesDescription (type, defaultNamespace, action),
120                                         formatterAttribute));
121                 }
122
123                 public abstract object FromMessage (Message message);
124
125                 public abstract Message ToMessage (object typedMessage);
126
127                 public abstract Message ToMessage (object typedMessage, MessageVersion version);
128
129                 static MessageDescriptionCollection MessageContractToMessagesDescription (
130                         Type src, string defaultNamespace, string action)
131                 {
132                         MessageContractAttribute mca =
133                                 ContractDescriptionGenerator.GetMessageContractAttribute (src);
134
135                         if (mca == null)
136                                 throw new ArgumentException (String.Format ("Type {0} and its ancestor types do not have MessageContract attribute.", src));
137
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));
142                         return messages;
143                 }
144         }
145 }