//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ namespace System.Web.Services.Description { using System.Web.Services; using System.Web.Services.Protocols; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; using System.Collections; using System; using System.Reflection; using System.Security.Permissions; using System.Collections.Generic; /// /// /// [To be supplied.] /// [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")] [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")] public abstract class ProtocolReflector { ServiceDescriptionReflector reflector; LogicalMethodInfo method; Operation operation; OperationBinding operationBinding; Port port; PortType portType; Binding binding; WebMethodAttribute methodAttr; Message inputMessage; Message outputMessage; MessageCollection headerMessages; ServiceDescription bindingServiceDescription; CodeIdentifiers portNames; bool emptyBinding; internal void Initialize(ServiceDescriptionReflector reflector) { this.reflector = reflector; } internal bool IsEmptyBinding { get { return emptyBinding; } } /// /// /// [To be supplied.] /// public Service Service { get { return reflector.Service; } } /// /// /// [To be supplied.] /// public ServiceDescription ServiceDescription { get { return reflector.ServiceDescription; } } /// /// /// [To be supplied.] /// public ServiceDescriptionCollection ServiceDescriptions { get { return reflector.ServiceDescriptions; } } internal List> UriFixups { get { return this.reflector.UriFixups; } } /// /// /// [To be supplied.] /// public XmlSchemas Schemas { get { return reflector.Schemas; } } /// /// /// [To be supplied.] /// public XmlSchemaExporter SchemaExporter { get { return reflector.SchemaExporter; } } /// /// /// [To be supplied.] /// public XmlReflectionImporter ReflectionImporter { get { return reflector.ReflectionImporter; } } /// /// /// [To be supplied.] /// public string DefaultNamespace { get { return reflector.ServiceAttribute.Namespace; } } /// /// /// [To be supplied.] /// public string ServiceUrl { get { return reflector.ServiceUrl; } } /// /// /// [To be supplied.] /// public Type ServiceType { get { return reflector.ServiceType; } } /// /// /// [To be supplied.] /// public LogicalMethodInfo Method { get { return method; } } /// /// /// [To be supplied.] /// public Binding Binding { get { return binding; } } /// /// /// [To be supplied.] /// public PortType PortType { get { return portType; } } /// /// /// [To be supplied.] /// public Port Port { get { return port; } } /// /// /// [To be supplied.] /// public Operation Operation { get { return operation; } } /// /// /// [To be supplied.] /// public OperationBinding OperationBinding { get { return operationBinding; } } /// /// /// [To be supplied.] /// public WebMethodAttribute MethodAttribute { get { return methodAttr; } } /// /// /// [To be supplied.] /// public LogicalMethodInfo[] Methods { get { return reflector.Methods; } } internal Hashtable ReflectionContext { get { return reflector.ReflectionContext; } } /// /// /// [To be supplied.] /// public Message InputMessage { get { if (inputMessage == null) { string messageName = XmlConvert.EncodeLocalName(methodAttr.MessageName.Length == 0 ? Method.Name : methodAttr.MessageName); bool diffNames = messageName != Method.Name; inputMessage = new Message(); inputMessage.Name = messageName + ProtocolName + "In"; OperationInput input = new OperationInput(); if (diffNames) input.Name = messageName; input.Message = new XmlQualifiedName(inputMessage.Name, bindingServiceDescription.TargetNamespace); operation.Messages.Add(input); OperationBinding.Input = new InputBinding(); if (diffNames) OperationBinding.Input.Name = messageName; } return inputMessage; } } /// /// /// [To be supplied.] /// public Message OutputMessage { get { if (outputMessage == null) { string messageName = XmlConvert.EncodeLocalName(methodAttr.MessageName.Length == 0 ? Method.Name : methodAttr.MessageName); bool diffNames = messageName != Method.Name; outputMessage = new Message(); outputMessage.Name = messageName + ProtocolName + "Out"; OperationOutput output = new OperationOutput(); if (diffNames) output.Name = messageName; output.Message = new XmlQualifiedName(outputMessage.Name, bindingServiceDescription.TargetNamespace); operation.Messages.Add(output); OperationBinding.Output = new OutputBinding(); if (diffNames) OperationBinding.Output.Name = messageName; } return outputMessage; } } /// /// /// [To be supplied.] /// public MessageCollection HeaderMessages { get { if (headerMessages == null) { headerMessages = new MessageCollection(bindingServiceDescription); } return headerMessages; } } void MoveToMethod(LogicalMethodInfo method) { this.method = method; this.methodAttr = method.MethodAttribute; } class ReflectedBinding { internal ReflectedBinding() { } internal ReflectedBinding(WebServiceBindingAttribute bindingAttr) { this.bindingAttr = bindingAttr; } public WebServiceBindingAttribute bindingAttr; public ArrayList methodList; } internal void Reflect() { emptyBinding = false; Hashtable bindings = new Hashtable(); Hashtable reflectedBindings = new Hashtable(); for (int i = 0; i < reflector.Methods.Length; i++) { MoveToMethod(reflector.Methods[i]); string bindingName = ReflectMethodBinding(); if (bindingName == null) bindingName = string.Empty; ReflectedBinding reflectedBinding = (ReflectedBinding)reflectedBindings[bindingName]; if (reflectedBinding == null) { reflectedBinding = new ReflectedBinding(); reflectedBinding.bindingAttr = WebServiceBindingReflector.GetAttribute(method, bindingName); if (reflectedBinding.bindingAttr == null || (bindingName.Length == 0 && reflectedBinding.bindingAttr.Location.Length > 0)) { reflectedBinding.bindingAttr = new WebServiceBindingAttribute(); } reflectedBindings.Add(bindingName, reflectedBinding); } if (reflectedBinding.bindingAttr.Location.Length == 0) { if (reflectedBinding.methodList == null) reflectedBinding.methodList = new ArrayList(); reflectedBinding.methodList.Add(method); bindings[reflectedBinding.bindingAttr.Name] = method; } else { AddImport(reflectedBinding.bindingAttr.Namespace, reflectedBinding.bindingAttr.Location); } } foreach (ReflectedBinding reflectedBinding in reflectedBindings.Values) { ReflectBinding(reflectedBinding); } // Only check for empty binding if we do not have real bindings if (reflectedBindings.Count == 0) { // It should be possible to get the value for WebReference.ServiceLocationUrl even if the web service has no web methods. // This is a common scenario for Whitehorse during the early stages of development when a user is defining the web // components and their inter-connections, but not the details of whatmethods will be present on each web service. // get all WebServiceBindings emptyBinding = true; ReflectedBinding binding = null; object[] attrs = ServiceType.GetCustomAttributes(typeof(WebServiceBindingAttribute), false); for (int i = 0; i < attrs.Length; i++) { WebServiceBindingAttribute bindingAttribute = (WebServiceBindingAttribute)attrs[i]; if (bindings[bindingAttribute.Name] != null) continue; if (binding != null) { binding = null; break; } binding = new ReflectedBinding(bindingAttribute); } if (binding != null) ReflectBinding(binding); } Type[] interfaces = ServiceType.GetInterfaces(); // iterate through all the interfaces for this type foreach (Type bindingInterface in interfaces) { object[] attrs = bindingInterface.GetCustomAttributes(typeof(WebServiceBindingAttribute), false); for (int i = 0; i < attrs.Length; i++) { WebServiceBindingAttribute bindingAttribute = (WebServiceBindingAttribute)attrs[i]; if (bindings[bindingAttribute.Name] != null) continue; ReflectBinding(new ReflectedBinding(bindingAttribute)); } } ReflectDescription(); } void AddImport(string ns, string location) { foreach (Import import in ServiceDescription.Imports) { if (import.Namespace == ns && import.Location == location) { return; } } Import newImport = new Import(); newImport.Namespace = ns; newImport.Location = location; ServiceDescription.Imports.Add(newImport); } /// /// /// [To be supplied.] /// public ServiceDescription GetServiceDescription(string ns) { ServiceDescription description = ServiceDescriptions[ns]; if (description == null) { description = new ServiceDescription(); description.TargetNamespace = ns; ServiceDescriptions.Add(description); } return description; } void ReflectBinding(ReflectedBinding reflectedBinding) { string bindingName = XmlConvert.EncodeLocalName(reflectedBinding.bindingAttr.Name); string bindingNamespace = reflectedBinding.bindingAttr.Namespace; if (bindingName.Length == 0) bindingName = Service.Name + ProtocolName; if (bindingNamespace.Length == 0) bindingNamespace = ServiceDescription.TargetNamespace; WsiProfiles claims = WsiProfiles.None; if (reflectedBinding.bindingAttr.Location.Length > 0) { // If a URL is specified for the WSDL, file, then we just import the // binding from there instead of generating it in this WSDL file. portType = null; binding = null; } else { bindingServiceDescription = GetServiceDescription(bindingNamespace); CodeIdentifiers bindingNames = new CodeIdentifiers(); foreach (Binding b in bindingServiceDescription.Bindings) bindingNames.AddReserved(b.Name); bindingName = bindingNames.AddUnique(bindingName, binding); portType = new PortType(); binding = new Binding(); portType.Name = bindingName; binding.Name = bindingName; binding.Type = new XmlQualifiedName(portType.Name, bindingNamespace); claims = reflectedBinding.bindingAttr.ConformsTo & this.ConformsTo; if (reflectedBinding.bindingAttr.EmitConformanceClaims && claims != WsiProfiles.None) { ServiceDescription.AddConformanceClaims(binding.GetDocumentationElement(), claims); } bindingServiceDescription.Bindings.Add(binding); bindingServiceDescription.PortTypes.Add(portType); } if (portNames == null) { portNames = new CodeIdentifiers(); foreach (Port p in Service.Ports) portNames.AddReserved(p.Name); } port = new Port(); port.Binding = new XmlQualifiedName(bindingName, bindingNamespace); port.Name = portNames.AddUnique(bindingName, port); Service.Ports.Add(port); BeginClass(); if (reflectedBinding.methodList != null && reflectedBinding.methodList.Count > 0) { foreach (LogicalMethodInfo method in reflectedBinding.methodList) { MoveToMethod(method); operation = new Operation(); operation.Name = XmlConvert.EncodeLocalName(method.Name); if (methodAttr.Description != null && methodAttr.Description.Length > 0) operation.Documentation = methodAttr.Description; operationBinding = new OperationBinding(); operationBinding.Name = operation.Name; inputMessage = null; outputMessage = null; headerMessages = null; if (ReflectMethod()) { if (inputMessage != null) bindingServiceDescription.Messages.Add(inputMessage); if (outputMessage != null) bindingServiceDescription.Messages.Add(outputMessage); if (headerMessages != null) { foreach (Message headerMessage in headerMessages) { bindingServiceDescription.Messages.Add(headerMessage); } } binding.Operations.Add(operationBinding); portType.Operations.Add(operation); } } } if (binding != null && claims == WsiProfiles.BasicProfile1_1 && ProtocolName == "Soap") { BasicProfileViolationCollection warnings = new BasicProfileViolationCollection(); WebServicesInteroperability.AnalyzeBinding(binding, bindingServiceDescription, ServiceDescriptions, warnings); if (warnings.Count > 0) { throw new InvalidOperationException(Res.GetString(Res.WebWsiViolation, ServiceType.FullName, warnings.ToString())); } } EndClass(); } internal virtual WsiProfiles ConformsTo { get { return WsiProfiles.None; } } /// /// /// [To be supplied.] /// public abstract string ProtocolName { get; } // These overridable methods have no parameters. The subclass uses properties on this // base object to obtain the information. This allows us to grow the set of // information passed to the methods over time w/o breaking anyone. They are protected // instead of public because this object is passed to extensions and we don't want // those calling these methods. /// /// /// [To be supplied.] /// protected virtual void BeginClass() { } /// /// /// [To be supplied.] /// protected abstract bool ReflectMethod(); /// /// /// [To be supplied.] /// protected virtual string ReflectMethodBinding() { return string.Empty; } /// /// /// [To be supplied.] /// protected virtual void EndClass() { } /// /// /// [To be supplied.] /// protected virtual void ReflectDescription() { } } }