2010-03-11 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Dispatcher / ClientOperation.cs
1 //
2 // ClientOperation.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 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;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Reflection;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.Text;
36
37 namespace System.ServiceModel.Dispatcher
38 {
39         [MonoTODO]
40         public sealed class ClientOperation
41         {
42                 internal class ClientOperationCollection :
43 #if NET_2_1
44                         KeyedCollection<string, ClientOperation>
45 #else
46                         SynchronizedKeyedCollection<string, ClientOperation>
47 #endif
48                 {
49                         protected override string GetKeyForItem (ClientOperation o)
50                         {
51                                 return o.Name;
52                         }
53                 }
54
55                 ClientRuntime parent;
56                 string name, action, reply_action;
57                 MethodInfo sync_method, begin_method, end_method;
58                 bool deserialize_reply = true, serialize_request = true;
59                 bool is_initiating, is_terminating, is_oneway;
60                 IClientMessageFormatter formatter, actual_formatter;
61                 SynchronizedCollection<IParameterInspector> inspectors
62                         = new SynchronizedCollection<IParameterInspector> ();
63 #if !NET_2_1
64                 SynchronizedCollection<FaultContractInfo> fault_contract_infos;
65 #endif
66
67                 public ClientOperation (ClientRuntime parent,
68                         string name, string action)
69                 {
70                         this.parent = parent;
71                         this.name = name;
72                         this.action = action;
73                 }
74
75                 public ClientOperation (ClientRuntime parent,
76                         string name, string action, string replyAction)
77                 {
78                         this.parent = parent;
79                         this.name = name;
80                         this.action = action;
81                         this.reply_action = replyAction;
82                 }
83
84                 public string Action {
85                         get { return action; }
86                 }
87
88                 public string ReplyAction {
89                         get { return reply_action; }
90                 }
91
92                 public MethodInfo BeginMethod {
93                         get { return begin_method; }
94                         set { begin_method = value; }
95                 }
96
97                 public bool DeserializeReply {
98                         get { return deserialize_reply; }
99                         set { deserialize_reply = value; }
100                 }
101
102                 public MethodInfo EndMethod {
103                         get { return end_method; }
104                         set { end_method = value; }
105                 }
106
107 #if !NET_2_1
108                 public SynchronizedCollection<FaultContractInfo> FaultContractInfos {
109                         get {
110                                 if (fault_contract_infos == null) {
111                                         var l = new SynchronizedCollection<FaultContractInfo> ();
112                                         foreach (var f in Description.Faults)
113                                                 l.Add (new FaultContractInfo (f.Action, f.DetailType));
114                                         fault_contract_infos = l;
115                                 }
116                                 return fault_contract_infos;
117                         }
118                 }
119 #endif
120
121                 public IClientMessageFormatter Formatter {
122                         get { return formatter; }
123                         set { formatter = value; }
124                 }
125
126                 public bool IsInitiating {
127                         get { return is_initiating; }
128                         set { is_initiating = value; }
129                 }
130
131                 public bool IsOneWay {
132                         get { return is_oneway; }
133                         set { is_oneway = value; }
134                 }
135
136                 public bool IsTerminating {
137                         get { return is_terminating; }
138                         set { is_terminating = value; }
139                 }
140
141                 public string Name {
142                         get { return name; }
143                 }
144
145                 public SynchronizedCollection<IParameterInspector> ParameterInspectors {
146                         get { return inspectors; }
147                 }
148
149                 public ClientRuntime Parent {
150                         get { return parent; }
151                 }
152
153                 public bool SerializeRequest {
154                         get { return serialize_request; }
155                         set { serialize_request = value; }
156                 }
157
158                 public MethodInfo SyncMethod {
159                         get { return sync_method; }
160                         set { sync_method = value; }
161                 }
162
163                 OperationDescription Description {
164                         get {
165                                 // FIXME: ContractDescription should be acquired from elsewhere.
166                                 ContractDescription cd = ContractDescription.GetContract (Parent.ContractClientType);
167                                 OperationDescription od = cd.Operations.Find (Name);
168                                 if (od == null) {
169                                         if (Name == "*")
170                                                 throw new Exception (String.Format ("INTERNAL ERROR: Contract {0} in namespace {1} does not contain Operations.", Parent.ContractName, Parent.ContractNamespace));
171                                         else
172                                                 throw new Exception (String.Format ("INTERNAL ERROR: Operation {0} was not found.", Name));
173                                 }
174                                 return od;
175                         }
176                 }
177
178                 internal IClientMessageFormatter GetFormatter ()
179                 {
180                         if (actual_formatter == null) {
181                                 if (Formatter != null)
182                                         actual_formatter = Formatter;
183                                 else
184                                         actual_formatter = new OperationFormatter (Description, false, false); // FIXME: pass correct isRpc, isEncoded
185                         }
186                         return actual_formatter;
187                 }
188         }
189 }