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);
74 codeClass.IsPartial = true;
77 string location = null;
79 HttpAddressBinding sab = (HttpAddressBinding) Port.Extensions.Find (typeof(HttpAddressBinding));
80 if (sab != null) location = sab.Location;
83 CodeConstructor cc = new CodeConstructor ();
84 cc.Attributes = MemberAttributes.Public;
85 GenerateServiceUrl (location, cc.Statements);
86 codeClass.Members.Add (cc);
88 memberIds = new CodeIdentifiers ();
92 protected override void BeginNamespace ()
94 xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
95 soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
96 xmlExporter = new XmlCodeExporter (CodeNamespace, null);
97 xmlReflectionImporter = new XmlReflectionImporter ();
100 protected override void EndClass ()
102 if (xmlExporter.IncludeMetadata.Count > 0)
104 if (CodeTypeDeclaration.CustomAttributes == null)
105 CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
106 CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
110 protected override void EndNamespace ()
114 protected override bool IsBindingSupported ()
116 throw new NotImplementedException ();
120 protected override bool IsOperationFlowSupported (OperationFlow flow)
122 throw new NotImplementedException ();
125 protected override CodeMemberMethod GenerateMethod ()
129 HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
130 if (httpOper == null) throw new Exception ("Http operation binding not found");
132 XmlMembersMapping inputMembers = ImportInMembersMapping (InputMessage);
133 XmlTypeMapping outputMember = ImportOutMembersMapping (OutputMessage);
135 CodeMemberMethod met = GenerateMethod (memberIds, httpOper, inputMembers, outputMember);
137 xmlExporter.ExportMembersMapping (inputMembers);
138 if (outputMember != null)
139 xmlExporter.ExportTypeMapping (outputMember);
145 UnsupportedOperationBindingWarning (ex.Message);
150 XmlMembersMapping ImportInMembersMapping (Message msg)
152 SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
153 for (int n=0; n<mems.Length; n++)
155 SoapSchemaMember mem = new SoapSchemaMember();
156 mem.MemberName = msg.Parts[n].Name;
157 mem.MemberType = msg.Parts[n].Type;
160 return soapImporter.ImportMembersMapping (Operation.Name, "", mems);
163 XmlTypeMapping ImportOutMembersMapping (Message msg)
165 if (msg.Parts.Count == 0) return null;
167 if (msg.Parts[0].Name == "Body" && msg.Parts[0].Element == XmlQualifiedName.Empty)
168 return xmlReflectionImporter.ImportTypeMapping (typeof(XmlNode));
170 // This is a bit hacky. The issue is that types such as string[] are to be imported
171 // as such, not as ArrayOfString class. ImportTypeMapping will return a
172 // class if the type has not been imported as an array before, hence the
173 // call to ImportMembersMapping.
174 xmlImporter.ImportMembersMapping (new XmlQualifiedName[] {msg.Parts[0].Element});
175 return xmlImporter.ImportTypeMapping (msg.Parts[0].Element);
179 CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, HttpOperationBinding httpOper, XmlMembersMapping inputMembers, XmlTypeMapping outputMember)
181 CodeIdentifiers pids = new CodeIdentifiers ();
182 CodeMemberMethod method = new CodeMemberMethod ();
183 CodeMemberMethod methodBegin = new CodeMemberMethod ();
184 CodeMemberMethod methodEnd = new CodeMemberMethod ();
185 method.Attributes = MemberAttributes.Public;
186 methodBegin.Attributes = MemberAttributes.Public;
187 methodEnd.Attributes = MemberAttributes.Public;
189 // Find unique names for temporary variables
191 for (int n=0; n<inputMembers.Count; n++)
192 pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
194 string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
195 string varCallback = pids.AddUnique ("callback","callback");
196 string varAsyncState = pids.AddUnique ("asyncState","asyncState");
198 string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
200 method.Name = Operation.Name;
201 methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + Operation.Name),method);
202 methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + Operation.Name),method);
204 method.ReturnType = new CodeTypeReference (typeof(void));
205 methodEnd.ReturnType = new CodeTypeReference (typeof(void));
206 methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
208 CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
210 for (int n=0; n<inputMembers.Count; n++)
212 string ptype = GetSimpleType (inputMembers[n]);
213 CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (ptype, inputMembers[n].MemberName);
215 param.Direction = FieldDirection.In;
216 method.Parameters.Add (param);
217 methodBegin.Parameters.Add (param);
218 paramArray [n] = new CodeVariableReferenceExpression (param.Name);
222 if (outputMember != null)
224 method.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
225 methodEnd.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
226 xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, outputMember, "");
230 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
231 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
232 methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
234 // Array of input parameters
236 CodeArrayCreateExpression methodParams;
237 if (paramArray.Length > 0)
238 methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
240 methodParams = new CodeArrayCreateExpression (typeof(object), 0);
242 // Generate method url
244 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
246 CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
247 CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
248 CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
252 CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
253 CodeMethodInvokeExpression inv;
255 inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, expMethodLocation, methodParams);
257 method.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (method.ReturnType, inv)));
259 method.Statements.Add (inv);
263 CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
264 CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
265 inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, expMethodLocation, methodParams, expCallb, expAsyncs);
266 methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
270 CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
271 inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
273 methodEnd.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (methodEnd.ReturnType, inv)));
275 methodEnd.Statements.Add (inv);
279 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.HttpMethodAttribute");
280 att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetOutMimeFormatter ())));
281 att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetInMimeFormatter ())));
282 AddCustomAttribute (method, att, true);
284 CodeTypeDeclaration.Members.Add (method);
285 CodeTypeDeclaration.Members.Add (methodBegin);
286 CodeTypeDeclaration.Members.Add (methodEnd);
292 internal override CodeExpression BuildInvokeAsync (string messageName, CodeArrayCreateExpression paramsArray, CodeExpression delegateField, CodeExpression userStateVar)
294 HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
296 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
298 CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
299 CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
300 CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
302 CodeMethodInvokeExpression inv2 = new CodeMethodInvokeExpression (ethis, "InvokeAsync");
303 inv2.Parameters.Add (new CodePrimitiveExpression (messageName));
304 inv2.Parameters.Add (expMethodLocation);
305 inv2.Parameters.Add (paramsArray);
306 inv2.Parameters.Add (delegateField);
307 inv2.Parameters.Add (userStateVar);
312 protected virtual Type GetInMimeFormatter ()
317 protected virtual Type GetOutMimeFormatter ()
319 if (OperationBinding.Output.Extensions.Find (typeof(MimeXmlBinding)) != null)
320 return typeof (XmlReturnReader);
322 MimeContentBinding bin = (MimeContentBinding) OperationBinding.Output.Extensions.Find (typeof(MimeContentBinding));
323 if (bin != null && bin.Type == "text/xml")
324 return typeof (XmlReturnReader);
326 return typeof(NopReturnReader);
329 string GetSimpleType (XmlMemberMapping member)
331 // MS seems to always use System.String for input parameters, except for byte[]
333 switch (member.TypeName)
337 return "System.String";
340 string ptype = member.TypeFullName;
341 int i = ptype.IndexOf ('[');
343 return "System.String";
345 return "System.String" + ptype.Substring (i);