Exemplo n.º 1
0
        private void ReflectInternal(ProtocolReflector[] reflectors)
        {
            description = new ServiceDescription();
            description.TargetNamespace = serviceAttr.Namespace;
            ServiceDescriptions.Add(description);

            service = new Service();
            string name = serviceAttr.Name;

            if (name == null || name.Length == 0)
            {
                name = serviceType.Name;
            }
            service.Name = XmlConvert.EncodeLocalName(name);

            if (serviceAttr.Description != null && serviceAttr.Description.Length > 0)
            {
                service.Documentation = serviceAttr.Description;
            }
            description.Services.Add(service);

            reflectionContext = new Hashtable();
            exporter          = new XmlSchemaExporter(description.Types.Schemas);
            importer          = SoapReflector.CreateXmlImporter(serviceAttr.Namespace, SoapReflector.ServiceDefaultIsEncoded(serviceType));
            WebMethodReflector.IncludeTypes(methods, importer);

            for (int i = 0; i < reflectors.Length; i++)
            {
                reflectors[i].Reflect();
            }
        }
        void MoveToOperation(Operation operation)
        {
            this.operation = operation;

            operationBinding = null;
            foreach (OperationBinding b in binding.Operations)
            {
                if (operation.IsBoundBy(b))
                {
                    if (operationBinding != null)
                    {
                        throw OperationSyntaxException(Res.GetString(Res.DuplicateInputOutputNames0));
                    }
                    operationBinding = b;
                }
            }
            if (operationBinding == null)
            {
                throw OperationSyntaxException(Res.GetString(Res.MissingBinding0));
            }
            //NOTE: The following two excepions would never happen since IsBoundBy checks these conditions already.
            if (operation.Messages.Input != null && operationBinding.Input == null)
            {
                throw OperationSyntaxException(Res.GetString(Res.MissingInputBinding0));
            }
            if (operation.Messages.Output != null && operationBinding.Output == null)
            {
                throw OperationSyntaxException(Res.GetString(Res.MissingOutputBinding0));
            }

            this.inputMessage  = operation.Messages.Input == null ? null : ServiceDescriptions.GetMessage(operation.Messages.Input.Message);
            this.outputMessage = operation.Messages.Output == null ? null : ServiceDescriptions.GetMessage(operation.Messages.Output.Message);
        }
Exemplo n.º 3
0
        /// <include file='doc\ServiceDescriptionImporter.uex' path='docs/doc[@for="ServiceDescriptionImporter.AddServiceDescription"]/*' />
        public void AddServiceDescription(ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
        {
            if (serviceDescription == null)
            {
                throw new ArgumentNullException("serviceDescription");
            }

            serviceDescription.AppSettingUrlKey  = appSettingUrlKey;
            serviceDescription.AppSettingBaseUrl = appSettingBaseUrl;
            ServiceDescriptions.Add(serviceDescription);
        }
        /// <include file='doc\ProtocolReflector.uex' path='docs/doc[@for="ProtocolReflector.GetServiceDescription"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public ServiceDescription GetServiceDescription(string ns)
        {
            ServiceDescription description = ServiceDescriptions[ns];

            if (description == null)
            {
                description = new ServiceDescription();
                description.TargetNamespace = ns;
                ServiceDescriptions.Add(description);
            }
            return(description);
        }
Exemplo n.º 5
0
        internal void Reflect(ServiceDescriptionReflector serviceReflector, Type type, string url, XmlSchemaExporter xxporter, SoapSchemaExporter sxporter)
        {
            portNames             = new CodeIdentifiers();
            this.serviceReflector = serviceReflector;
            serviceUrl            = url;
            serviceType           = type;

            schemaExporter     = xxporter;
            soapSchemaExporter = sxporter;

            typeInfo = TypeStubManager.GetTypeStub(type, ProtocolName);

            ServiceDescription desc = ServiceDescriptions [typeInfo.LogicalType.WebServiceNamespace];

            if (desc == null)
            {
                desc = new ServiceDescription();
                desc.TargetNamespace = typeInfo.LogicalType.WebServiceNamespace;
                desc.Name            = typeInfo.LogicalType.WebServiceName;
                ServiceDescriptions.Add(desc);
            }

            ImportService(desc, typeInfo, url);
        }
Exemplo n.º 6
0
        void ImportHeader(CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
        {
            Message msg = ServiceDescriptions.GetMessage(hb.Message);

            if (msg == null)
            {
                throw new InvalidOperationException("Message " + hb.Message + " not found");
            }
            MessagePart part = msg.Parts [hb.Part];

            if (part == null)
            {
                throw new InvalidOperationException("Message part " + hb.Part + " not found in message " + hb.Message);
            }

            XmlTypeMapping map;
            string         hname;

            if (hb.Use == SoapBindingUse.Literal)
            {
                map   = xmlImporter.ImportDerivedTypeMapping(part.Element, typeof(SoapHeader));
                hname = part.Element.Name;
                xmlExporter.ExportTypeMapping(map);
            }
            else
            {
                map   = soapImporter.ImportDerivedTypeMapping(part.Type, typeof(SoapHeader), true);
                hname = part.Type.Name;
                soapExporter.ExportTypeMapping(map);
            }

            string varName = headerVariables [map] as string;

            if (varName == null)
            {
                if (hname == map.TypeName)
                {
                    varName = memberIds.AddUnique(CodeIdentifier.MakeValid(hname + "Value"), hb);
                }
                else
                {
                    varName = memberIds.AddUnique(CodeIdentifier.MakeValid(hname), hb);
                }

#if NET_2_0
                string propName = varName;
                varName = varName + "Field";
#endif
                headerVariables.Add(map, varName);
                CodeMemberField codeField = new CodeMemberField(map.TypeFullName, varName);
                CodeTypeDeclaration.Members.Add(codeField);

#if NET_2_0
                codeField.Attributes = MemberAttributes.Private;
                CodeMemberProperty codeProperty = new CodeMemberProperty();
                codeProperty.Name       = propName;
                codeProperty.Type       = new CodeTypeReference(map.TypeFullName);
                codeProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                codeProperty.HasGet     = codeProperty.HasSet = true;
                CodeExpression ce = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), varName);
                codeProperty.SetStatements.Add(new CodeAssignStatement(ce, new CodePropertySetValueReferenceExpression()));
                codeProperty.GetStatements.Add(new CodeMethodReturnStatement(ce));
                CodeTypeDeclaration.Members.Add(codeProperty);

                varName = propName;
#else
                codeField.Attributes = MemberAttributes.Public;
#endif
            }

            CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Web.Services.Protocols.SoapHeaderAttribute");
            att.Arguments.Add(GetArg(varName));
#if ONLY_1_0
            att.Arguments.Add(GetArg("Required", false));
#endif
            if (direction != SoapHeaderDirection.In)
            {
                att.Arguments.Add(GetEnumArg("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString()));
            }
            AddCustomAttribute(method, att, true);
        }
Exemplo n.º 7
0
        void ClasifySchemas(ArrayList importInfo)
        {
            // I don't like this, but I could not find any other way of clasifying
            // schemas between encoded and literal.

            xmlSchemas  = new XmlSchemas();
            soapSchemas = new XmlSchemas();

            foreach (ImportInfo info in importInfo)
            {
                foreach (Service service in info.ServiceDescription.Services)
                {
                    foreach (Port port in service.Ports)
                    {
                        this.iinfo = info;
                        this.port  = port;
                        binding    = ServiceDescriptions.GetBinding(port.Binding);
                        if (binding == null)
                        {
                            continue;
                        }
                        portType = ServiceDescriptions.GetPortType(binding.Type);
                        if (portType == null)
                        {
                            continue;
                        }

                        foreach (OperationBinding oper in binding.Operations)
                        {
                            operationBinding = oper;
                            operation        = FindPortOperation();
                            if (operation == null)
                            {
                                continue;
                            }

                            foreach (OperationMessage omsg in operation.Messages)
                            {
                                Message msg = ServiceDescriptions.GetMessage(omsg.Message);
                                if (msg == null)
                                {
                                    continue;
                                }

                                if (omsg is OperationInput)
                                {
                                    inputMessage = msg;
                                }
                                else
                                {
                                    outputMessage = msg;
                                }
                            }

                            if (GetMessageEncoding(oper.Input) == SoapBindingUse.Encoded)
                            {
                                AddMessageSchema(soapSchemas, oper.Input, inputMessage);
                            }
                            else
                            {
                                AddMessageSchema(xmlSchemas, oper.Input, inputMessage);
                            }

                            if (oper.Output != null)
                            {
                                if (GetMessageEncoding(oper.Output) == SoapBindingUse.Encoded)
                                {
                                    AddMessageSchema(soapSchemas, oper.Output, outputMessage);
                                }
                                else
                                {
                                    AddMessageSchema(xmlSchemas, oper.Output, outputMessage);
                                }
                            }
                        }
                    }
                }
            }

            XmlSchemas defaultList = xmlSchemas;

            if (xmlSchemas.Count == 0 && soapSchemas.Count > 0)
            {
                defaultList = soapSchemas;
            }

            // Schemas not referenced by any message
            foreach (XmlSchema sc in Schemas)
            {
                if (!soapSchemas.Contains(sc) && !xmlSchemas.Contains(sc))
                {
                    if (ImportsEncodedNamespace(sc))
                    {
                        soapSchemas.Add(sc);
                    }
                    else
                    {
                        defaultList.Add(sc);
                    }
                }
            }
        }
Exemplo n.º 8
0
        void ImportPortBinding(bool multipleBindings)
        {
            if (port != null)
            {
                if (multipleBindings)
                {
                    className = binding.Name;
                }
                else
                {
                    className = service.Name;
                }
            }
            else
            {
                className = binding.Name;
            }

            className = classNames.AddUnique(CodeIdentifier.MakeValid(className), port);
            className = className.Replace("_x0020_", ""); // MS.NET seems to do this

            try
            {
                portType = ServiceDescriptions.GetPortType(binding.Type);
                if (portType == null)
                {
                    throw new Exception("Port type not found: " + binding.Type);
                }

                CodeTypeDeclaration codeClass = BeginClass();
                codeTypeDeclaration = codeClass;
                AddCodeType(codeClass, port != null ? port.Documentation : null);
                codeClass.Attributes = MemberAttributes.Public;

                if (service != null && service.Documentation != null && service.Documentation != "")
                {
                    AddComments(codeClass, service.Documentation);
                }

                if (Style == ServiceDescriptionImportStyle.Client)
                {
                    CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Diagnostics.DebuggerStepThroughAttribute");
                    AddCustomAttribute(codeClass, att, true);

                    att = new CodeAttributeDeclaration("System.ComponentModel.DesignerCategoryAttribute");
                    att.Arguments.Add(GetArg("code"));
                    AddCustomAttribute(codeClass, att, true);
                }
                else
                {
                    codeClass.TypeAttributes = System.Reflection.TypeAttributes.Abstract | System.Reflection.TypeAttributes.Public;
                }

                if (binding.Operations.Count == 0)
                {
                    warnings |= ServiceDescriptionImportWarnings.NoMethodsGenerated;
                    return;
                }

                foreach (OperationBinding oper in binding.Operations)
                {
                    operationBinding = oper;
                    operation        = FindPortOperation();
                    if (operation == null)
                    {
                        throw new Exception("Operation " + operationBinding.Name + " not found in portType " + PortType.Name);
                    }

                    inputMessage  = null;
                    outputMessage = null;

                    foreach (OperationMessage omsg in operation.Messages)
                    {
                        Message msg = ServiceDescriptions.GetMessage(omsg.Message);
                        if (msg == null)
                        {
                            throw new Exception("Message not found: " + omsg.Message);
                        }

                        if (omsg is OperationInput)
                        {
                            inputMessage = msg;
                        }
                        else
                        {
                            outputMessage = msg;
                        }
                    }

                    CodeMemberMethod method = GenerateMethod();

                    if (method != null)
                    {
                        methodName = method.Name;
                        if (operation.Documentation != null && operation.Documentation != "")
                        {
                            AddComments(method, operation.Documentation);
                        }
#if NET_2_0
                        if (Style == ServiceDescriptionImportStyle.Client)
                        {
                            AddAsyncMembers(method.Name, method);
                        }
#endif
                    }
                }

#if NET_2_0
                if (Style == ServiceDescriptionImportStyle.Client)
                {
                    AddAsyncTypes();
                }
#endif

                EndClass();
            }
            catch (InvalidOperationException ex)
            {
                warnings |= ServiceDescriptionImportWarnings.NoCodeGenerated;
                UnsupportedBindingWarning(ex.Message);
            }
        }
Exemplo n.º 9
0
        internal bool Import(ServiceDescriptionImporter descriptionImporter, CodeNamespace codeNamespace, ArrayList importInfo)
        {
            this.descriptionImporter = descriptionImporter;
            this.classNames          = new CodeIdentifiers();;
            this.codeNamespace       = codeNamespace;

            warnings = (ServiceDescriptionImportWarnings)0;

            bool found = false;

            ClasifySchemas(importInfo);

            BeginNamespace();

            foreach (ImportInfo info in importInfo)
            {
                foreach (Service service in info.ServiceDescription.Services)
                {
                    this.service = service;
                    int bindingCount = 0;
                    foreach (Port port in service.Ports)
                    {
                        binding = ServiceDescriptions.GetBinding(port.Binding);
                        if (IsBindingSupported())
                        {
                            bindingCount++;
                        }
                    }

                    foreach (Port port in service.Ports)
                    {
                        this.iinfo = info;
                        this.port  = port;
                        binding    = ServiceDescriptions.GetBinding(port.Binding);
                        if (!IsBindingSupported())
                        {
                            continue;
                        }

                        found = true;
                        ImportPortBinding(bindingCount > 1);
                    }
                }
            }

            if (!found)
            {
                // Looks like MS.NET generates classes for all bindings if
                // no services are present

                foreach (ImportInfo info in importInfo)
                {
                    this.iinfo = info;
                    foreach (Binding b in info.ServiceDescription.Bindings)
                    {
                        this.binding = b;
                        this.service = null;
                        this.port    = null;
                        if (!IsBindingSupported())
                        {
                            continue;
                        }
                        found = true;
                        ImportPortBinding(true);
                    }
                }
            }

            EndNamespace();

            if (!found)
            {
                warnings = ServiceDescriptionImportWarnings.NoCodeGenerated;
            }
            return(found);
        }
        internal bool GenerateCode(CodeNamespace codeNamespace, ImportContext importContext, Hashtable exportContext)
        {
            bindingCount = 0;
            anyPorts     = false;

            this.codeNamespace = codeNamespace;

            Hashtable supportedBindings   = new Hashtable();
            Hashtable unsupportedBindings = new Hashtable();

            // look for ports with bindings
            foreach (ServiceDescription serviceDescription in ServiceDescriptions)
            {
                foreach (Service service in serviceDescription.Services)
                {
                    foreach (Port port in service.Ports)
                    {
                        Binding binding = ServiceDescriptions.GetBinding(port.Binding);
                        if (supportedBindings.Contains(binding))
                        {
                            continue;
                        }
                        PortType portType = ServiceDescriptions.GetPortType(binding.Type);
                        MoveToBinding(service, port, binding, portType);
                        if (IsBindingSupported())
                        {
                            bindingCount++;
                            anyPorts = true;
                            supportedBindings.Add(binding, binding);
                        }
                        else if (binding != null)
                        {
                            unsupportedBindings[binding] = binding;
                        }
                    }
                }
            }

            // no ports, look for bindings
            if (bindingCount == 0)
            {
                foreach (ServiceDescription serviceDescription in ServiceDescriptions)
                {
                    foreach (Binding binding in serviceDescription.Bindings)
                    {
                        if (unsupportedBindings.Contains(binding))
                        {
                            continue;
                        }
                        PortType portType = ServiceDescriptions.GetPortType(binding.Type);
                        MoveToBinding(binding, portType);
                        if (IsBindingSupported())
                        {
                            bindingCount++;
                        }
                    }
                }
            }

            // give up if no bindings
            if (bindingCount == 0)
            {
                // if we generated comments return true so that the comments get written
                return(codeNamespace.Comments.Count > 0);
            }

            this.importContext = importContext;
            this.exportContext = exportContext;
            BeginNamespace();

            supportedBindings.Clear();
            foreach (ServiceDescription serviceDescription in ServiceDescriptions)
            {
                if (anyPorts)
                {
                    foreach (Service service in serviceDescription.Services)
                    {
                        foreach (Port port in service.Ports)
                        {
                            Binding  binding  = ServiceDescriptions.GetBinding(port.Binding);
                            PortType portType = ServiceDescriptions.GetPortType(binding.Type);
                            MoveToBinding(service, port, binding, portType);
                            if (IsBindingSupported() && !supportedBindings.Contains(binding))
                            {
                                GenerateClassForBinding();
                                supportedBindings.Add(binding, binding);
                            }
                        }
                    }
                }
                else
                {
                    foreach (Binding binding in serviceDescription.Bindings)
                    {
                        PortType portType = ServiceDescriptions.GetPortType(binding.Type);
                        MoveToBinding(binding, portType);
                        if (IsBindingSupported())
                        {
                            GenerateClassForBinding();
                        }
                    }
                }
            }

            EndNamespace();
            return(true);
        }
Exemplo n.º 11
0
        void ImportBinding(ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
        {
            port      = new Port();
            port.Name = portNames.AddUnique(binfo.Name, port);
            bool bindingFull = true;

            if (binfo.Namespace != desc.TargetNamespace)
            {
                if (binfo.Location == null || binfo.Location == string.Empty)
                {
                    ServiceDescription newDesc = new ServiceDescription();
                    newDesc.TargetNamespace = binfo.Namespace;
                    newDesc.Name            = binfo.Name;
                    bindingFull             = ImportBindingContent(newDesc, typeInfo, url, binfo);
                    if (bindingFull)
                    {
                        int id = ServiceDescriptions.Add(newDesc);
                        AddImport(desc, binfo.Namespace, GetWsdlUrl(url, id));
                    }
                }
                else
                {
                    AddImport(desc, binfo.Namespace, binfo.Location);
                    bindingFull = true;
                }
            }
            else
            {
                bindingFull = ImportBindingContent(desc, typeInfo, url, binfo);
            }

            if (bindingFull)
            {
                port.Binding = new XmlQualifiedName(binding.Name, binfo.Namespace);

                int    n    = 0;
                string name = binfo.Name;
                bool   found;
                do
                {
                    found = false;
                    foreach (Port p in service.Ports)
                    {
                        if (p.Name == name)
                        {
                            found = true; n++; name = binfo.Name + n; break;
                        }
                    }
                }while (found);
                port.Name = name;
                service.Ports.Add(port);
            }

#if NET_2_0
            if (binfo.WebServiceBindingAttribute != null && binfo.WebServiceBindingAttribute.ConformsTo != WsiProfiles.None && String.IsNullOrEmpty(binfo.WebServiceBindingAttribute.Name))
            {
                BasicProfileViolationCollection violations = new BasicProfileViolationCollection();
                desc.Types.Schemas.Add(Schemas);
                ServiceDescriptionCollection col = new ServiceDescriptionCollection();
                col.Add(desc);
                ConformanceCheckContext ctx = new ConformanceCheckContext(col, violations);
                ctx.ServiceDescription = desc;
                ConformanceChecker[] checkers = WebServicesInteroperability.GetCheckers(binfo.WebServiceBindingAttribute.ConformsTo);
                foreach (ConformanceChecker checker in checkers)
                {
                    ctx.Checker = checker;
                    WebServicesInteroperability.Check(ctx, checker, binding);
                    if (violations.Count > 0)
                    {
                        throw new InvalidOperationException(violations [0].ToString());
                    }
                }
            }
#endif
        }