2 // System.Web.Services.Description.HttpSimpleProtocolImporter.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2003 Ximian, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Services;
33 using System.Web.Services.Protocols;
34 using System.Web.Services.Configuration;
36 using System.Xml.Serialization;
37 using System.Configuration;
38 using System.Collections;
40 namespace System.Web.Services.Description
42 internal abstract class HttpSimpleProtocolImporter : ProtocolImporter
47 HttpBinding httpBinding;
49 SoapCodeExporter soapExporter;
50 SoapSchemaImporter soapImporter;
51 XmlCodeExporter xmlExporter;
52 XmlSchemaImporter xmlImporter;
53 CodeIdentifiers memberIds;
54 XmlReflectionImporter xmlReflectionImporter;
60 public HttpSimpleProtocolImporter ()
64 #endregion // Constructors
68 protected override CodeTypeDeclaration BeginClass ()
70 httpBinding = (HttpBinding) Binding.Extensions.Find (typeof(HttpBinding));
72 CodeTypeDeclaration codeClass = new CodeTypeDeclaration (ClassName);
73 codeClass.IsPartial = true;
75 string location = null;
77 HttpAddressBinding sab = (HttpAddressBinding) Port.Extensions.Find (typeof(HttpAddressBinding));
78 if (sab != null) location = sab.Location;
81 CodeConstructor cc = new CodeConstructor ();
82 cc.Attributes = MemberAttributes.Public;
83 GenerateServiceUrl (location, cc.Statements);
84 codeClass.Members.Add (cc);
86 memberIds = new CodeIdentifiers ();
90 protected override void BeginNamespace ()
92 xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
93 soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
94 xmlExporter = new XmlCodeExporter (CodeNamespace, null);
95 xmlReflectionImporter = new XmlReflectionImporter ();
98 protected override void EndClass ()
100 if (xmlExporter.IncludeMetadata.Count > 0)
102 if (CodeTypeDeclaration.CustomAttributes == null)
103 CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
104 CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
108 protected override void EndNamespace ()
112 protected override bool IsBindingSupported ()
114 throw new NotImplementedException ();
118 protected override bool IsOperationFlowSupported (OperationFlow flow)
120 throw new NotImplementedException ();
123 protected override CodeMemberMethod GenerateMethod ()
127 HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
128 if (httpOper == null) throw new Exception ("Http operation binding not found");
130 XmlMembersMapping inputMembers = ImportInMembersMapping (InputMessage);
131 XmlTypeMapping outputMember = ImportOutMembersMapping (OutputMessage);
133 CodeMemberMethod met = GenerateMethod (memberIds, httpOper, inputMembers, outputMember);
135 xmlExporter.ExportMembersMapping (inputMembers);
136 if (outputMember != null)
137 xmlExporter.ExportTypeMapping (outputMember);
143 UnsupportedOperationBindingWarning (ex.Message);
148 XmlMembersMapping ImportInMembersMapping (Message msg)
150 SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
151 for (int n=0; n<mems.Length; n++)
153 SoapSchemaMember mem = new SoapSchemaMember();
154 mem.MemberName = msg.Parts[n].Name;
155 mem.MemberType = msg.Parts[n].Type;
158 return soapImporter.ImportMembersMapping (Operation.Name, "", mems);
161 XmlTypeMapping ImportOutMembersMapping (Message msg)
163 if (msg.Parts.Count == 0) return null;
165 if (msg.Parts[0].Name == "Body" && msg.Parts[0].Element == XmlQualifiedName.Empty)
166 return xmlReflectionImporter.ImportTypeMapping (typeof(XmlNode));
168 // This is a bit hacky. The issue is that types such as string[] are to be imported
169 // as such, not as ArrayOfString class. ImportTypeMapping will return a
170 // class if the type has not been imported as an array before, hence the
171 // call to ImportMembersMapping.
172 xmlImporter.ImportMembersMapping (new XmlQualifiedName[] {msg.Parts[0].Element});
173 return xmlImporter.ImportTypeMapping (msg.Parts[0].Element);
177 CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, HttpOperationBinding httpOper, XmlMembersMapping inputMembers, XmlTypeMapping outputMember)
179 CodeIdentifiers pids = new CodeIdentifiers ();
180 CodeMemberMethod method = new CodeMemberMethod ();
181 CodeMemberMethod methodBegin = new CodeMemberMethod ();
182 CodeMemberMethod methodEnd = new CodeMemberMethod ();
183 method.Attributes = MemberAttributes.Public;
184 methodBegin.Attributes = MemberAttributes.Public;
185 methodEnd.Attributes = MemberAttributes.Public;
187 // Find unique names for temporary variables
189 for (int n=0; n<inputMembers.Count; n++)
190 pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
192 string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
193 string varCallback = pids.AddUnique ("callback","callback");
194 string varAsyncState = pids.AddUnique ("asyncState","asyncState");
196 string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
198 method.Name = Operation.Name;
199 methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + Operation.Name),method);
200 methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + Operation.Name),method);
202 method.ReturnType = new CodeTypeReference (typeof(void));
203 methodEnd.ReturnType = new CodeTypeReference (typeof(void));
204 methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
206 CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
208 for (int n=0; n<inputMembers.Count; n++)
210 string ptype = GetSimpleType (inputMembers[n]);
211 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (ptype, inputMembers[n].MemberName);
213 param.Direction = FieldDirection.In;
214 method.Parameters.Add (param);
215 methodBegin.Parameters.Add (param);
216 paramArray [n] = new CodeVariableReferenceExpression (param.Name);
220 if (outputMember != null)
222 method.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
223 methodEnd.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
224 xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, outputMember, "");
228 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
229 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
230 methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
232 // Array of input parameters
234 CodeArrayCreateExpression methodParams;
235 if (paramArray.Length > 0)
236 methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
238 methodParams = new CodeArrayCreateExpression (typeof(object), 0);
240 // Generate method url
242 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
244 CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
245 CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
246 CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
250 CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
251 CodeMethodInvokeExpression inv;
253 inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, expMethodLocation, methodParams);
255 method.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (method.ReturnType, inv)));
257 method.Statements.Add (inv);
261 CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
262 CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
263 inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, expMethodLocation, methodParams, expCallb, expAsyncs);
264 methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
268 CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
269 inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
271 methodEnd.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (methodEnd.ReturnType, inv)));
273 methodEnd.Statements.Add (inv);
277 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.HttpMethodAttribute");
278 att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetOutMimeFormatter ())));
279 att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetInMimeFormatter ())));
280 AddCustomAttribute (method, att, true);
282 CodeTypeDeclaration.Members.Add (method);
283 CodeTypeDeclaration.Members.Add (methodBegin);
284 CodeTypeDeclaration.Members.Add (methodEnd);
289 internal override CodeExpression BuildInvokeAsync (string messageName, CodeArrayCreateExpression paramsArray, CodeExpression delegateField, CodeExpression userStateVar)
291 HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
293 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
295 CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
296 CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
297 CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
299 CodeMethodInvokeExpression inv2 = new CodeMethodInvokeExpression (ethis, "InvokeAsync");
300 inv2.Parameters.Add (new CodePrimitiveExpression (messageName));
301 inv2.Parameters.Add (expMethodLocation);
302 inv2.Parameters.Add (paramsArray);
303 inv2.Parameters.Add (delegateField);
304 inv2.Parameters.Add (userStateVar);
308 protected virtual Type GetInMimeFormatter ()
313 protected virtual Type GetOutMimeFormatter ()
315 if (OperationBinding.Output.Extensions.Find (typeof(MimeXmlBinding)) != null)
316 return typeof (XmlReturnReader);
318 MimeContentBinding bin = (MimeContentBinding) OperationBinding.Output.Extensions.Find (typeof(MimeContentBinding));
319 if (bin != null && bin.Type == "text/xml")
320 return typeof (XmlReturnReader);
322 return typeof(NopReturnReader);
325 string GetSimpleType (XmlMemberMapping member)
327 // MS seems to always use System.String for input parameters, except for byte[]
329 switch (member.TypeName)
333 return "System.String";
336 string ptype = member.TypeFullName;
337 int i = ptype.IndexOf ('[');
339 return "System.String";
341 return "System.String" + ptype.Substring (i);