2 // System.Web.Services.Description.ProtocolReflector.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
11 using System.Web.Services;
12 using System.Web.Services.Protocols;
13 using System.Xml.Serialization;
15 using System.Xml.Schema;
16 using System.Collections;
18 namespace System.Web.Services.Description {
19 public abstract class ProtocolReflector {
24 string defaultNamespace;
25 MessageCollection headerMessages;
27 LogicalMethodInfo[] methods;
29 OperationBinding operationBinding;
30 Message outputMessage;
34 XmlSchemaExporter schemaExporter;
36 ServiceDescription serviceDescription;
39 SoapSchemaExporter soapSchemaExporter;
40 MethodStubInfo methodStubInfo;
41 TypeStubInfo typeInfo;
42 ArrayList extensionReflectors;
43 ServiceDescriptionReflector serviceReflector;
45 XmlReflectionImporter reflectionImporter;
46 SoapReflectionImporter soapReflectionImporter;
52 protected ProtocolReflector ()
54 defaultNamespace = WebServiceAttribute.DefaultNamespace;
55 extensionReflectors = ExtensionManager.BuildExtensionReflectors ();
58 #endregion // Constructors
62 public Binding Binding {
63 get { return binding; }
66 public string DefaultNamespace {
67 get { return defaultNamespace; }
70 public MessageCollection HeaderMessages {
71 get { return headerMessages; } // TODO: set
74 public Message InputMessage {
75 get { return inputMessage; }
78 public LogicalMethodInfo Method {
79 get { return methodStubInfo.MethodInfo; }
82 public WebMethodAttribute MethodAttribute {
83 get { return methodStubInfo.MethodAttribute; }
86 public LogicalMethodInfo[] Methods {
87 get { return typeInfo.LogicalType.LogicalMethods; }
90 public Operation Operation {
91 get { return operation; }
94 public OperationBinding OperationBinding {
95 get { return operationBinding; }
98 public Message OutputMessage {
99 get { return outputMessage; }
106 public PortType PortType {
107 get { return portType; }
110 public abstract string ProtocolName {
114 public XmlReflectionImporter ReflectionImporter
118 if (reflectionImporter == null) {
119 reflectionImporter = typeInfo.XmlImporter;
120 if (reflectionImporter == null)
121 reflectionImporter = new XmlReflectionImporter();
123 return reflectionImporter;
127 internal SoapReflectionImporter SoapReflectionImporter
131 if (soapReflectionImporter == null) {
132 soapReflectionImporter = typeInfo.SoapImporter;
133 if (soapReflectionImporter == null)
134 soapReflectionImporter = new SoapReflectionImporter();
136 return soapReflectionImporter;
140 public XmlSchemaExporter SchemaExporter {
141 get { return schemaExporter; }
144 public SoapSchemaExporter SoapSchemaExporter {
145 get { return soapSchemaExporter; }
148 public XmlSchemas Schemas {
149 get { return serviceReflector.Schemas; }
152 public Service Service {
153 get { return service; }
156 public ServiceDescription ServiceDescription {
157 get { return serviceDescription; }
160 public ServiceDescriptionCollection ServiceDescriptions {
161 get { return serviceReflector.ServiceDescriptions; }
164 public Type ServiceType {
165 get { return serviceType; }
168 public string ServiceUrl {
169 get { return serviceUrl; }
172 internal MethodStubInfo MethodStubInfo {
173 get { return methodStubInfo; }
176 internal TypeStubInfo TypeInfo {
177 get { return typeInfo; }
181 #endregion // Properties
185 internal void Reflect (ServiceDescriptionReflector serviceReflector, Type type, string url, XmlSchemaExporter xxporter, SoapSchemaExporter sxporter)
187 this.serviceReflector = serviceReflector;
191 schemaExporter = xxporter;
192 soapSchemaExporter = sxporter;
194 typeInfo = TypeStubManager.GetTypeStub (type, ProtocolName);
196 ServiceDescription desc = ServiceDescriptions [typeInfo.LogicalType.WebServiceNamespace];
200 desc = new ServiceDescription ();
201 desc.TargetNamespace = typeInfo.LogicalType.WebServiceNamespace;
202 desc.Name = typeInfo.LogicalType.WebServiceName;
203 ServiceDescriptions.Add (desc);
206 ImportService (desc, typeInfo, url);
209 void ImportService (ServiceDescription desc, TypeStubInfo typeInfo, string url)
211 service = desc.Services [typeInfo.LogicalType.WebServiceName];
214 service = new Service ();
215 service.Name = typeInfo.LogicalType.WebServiceName;
216 service.Documentation = typeInfo.LogicalType.Description;
217 desc.Services.Add (service);
220 foreach (BindingInfo binfo in typeInfo.Bindings)
221 ImportBinding (desc, service, typeInfo, url, binfo);
224 void ImportBinding (ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
227 port.Name = binfo.Name;
228 port.Binding = new XmlQualifiedName (binfo.Name, binfo.Namespace);
229 service.Ports.Add (port);
231 if (binfo.Namespace != desc.TargetNamespace)
233 if (binfo.Location == null || binfo.Location == string.Empty)
235 ServiceDescription newDesc = new ServiceDescription();
236 newDesc.TargetNamespace = binfo.Namespace;
237 int id = ServiceDescriptions.Add (newDesc);
238 AddImport (desc, binfo.Namespace, GetWsdlUrl (url,id));
239 ImportBindingContent (newDesc, typeInfo, url, binfo);
242 AddImport (desc, binfo.Namespace, binfo.Location);
245 ImportBindingContent (desc, typeInfo, url, binfo);
248 void ImportBindingContent (ServiceDescription desc, TypeStubInfo typeInfo, string url, BindingInfo binfo)
250 serviceDescription = desc;
251 binding = new Binding ();
252 binding.Name = binfo.Name;
253 binding.Type = new XmlQualifiedName (binfo.Name, binfo.Namespace);
254 desc.Bindings.Add (binding);
256 portType = new PortType ();
257 portType.Name = binding.Name;
258 desc.PortTypes.Add (portType);
262 foreach (MethodStubInfo method in typeInfo.Methods)
264 methodStubInfo = method;
266 string metBinding = ReflectMethodBinding ();
267 if (metBinding != null && (metBinding != binding.Name)) continue;
269 operation = new Operation ();
270 operation.Name = method.OperationName;
271 operation.Documentation = method.MethodAttribute.Description;
273 inputMessage = new Message ();
274 inputMessage.Name = method.Name + ProtocolName + "In";
275 ServiceDescription.Messages.Add (inputMessage);
277 outputMessage = new Message ();
278 outputMessage.Name = method.Name + ProtocolName + "Out";
279 ServiceDescription.Messages.Add (outputMessage);
281 OperationInput inOp = new OperationInput ();
282 if (method.Name != method.OperationName) inOp.Name = method.Name;
283 Operation.Messages.Add (inOp);
284 inOp.Message = new XmlQualifiedName (inputMessage.Name, ServiceDescription.TargetNamespace);
286 OperationOutput outOp = new OperationOutput ();
287 if (method.Name != method.OperationName) outOp.Name = method.Name;
288 Operation.Messages.Add (outOp);
289 outOp.Message = new XmlQualifiedName (outputMessage.Name, ServiceDescription.TargetNamespace);
291 portType.Operations.Add (operation);
292 ImportOperationBinding ();
296 foreach (SoapExtensionReflector reflector in extensionReflectors)
298 reflector.ReflectionContext = this;
299 reflector.ReflectMethod ();
306 void ImportOperationBinding ()
308 operationBinding = new OperationBinding ();
309 operationBinding.Name = methodStubInfo.OperationName;
311 InputBinding inOp = new InputBinding ();
312 operationBinding.Input = inOp;
314 OutputBinding outOp = new OutputBinding ();
315 operationBinding.Output = outOp;
317 if (methodStubInfo.OperationName != methodStubInfo.Name)
318 inOp.Name = outOp.Name = methodStubInfo.Name;
320 binding.Operations.Add (operationBinding);
323 internal static void AddImport (ServiceDescription desc, string ns, string location)
325 Import im = new Import();
327 im.Location = location;
328 desc.Imports.Add (im);
331 string GetWsdlUrl (string baseUrl, int id)
333 return baseUrl + "?wsdl=" + id;
336 protected virtual void BeginClass ()
340 protected virtual void EndClass ()
344 public ServiceDescription GetServiceDescription (string ns)
346 return ServiceDescriptions [ns];
349 protected abstract bool ReflectMethod ();
351 protected virtual string ReflectMethodBinding ()