Beispiel #1
0
        /// <summary>
        /// Gets the XML schemas from a given WSDL document
        /// </summary>
        /// <param name="options">The metadata resolving options.</param>
        /// <returns>A collection of the XML schemas.</returns>
        public static XmlSchemas GetXmlSchemaSetFromWsdlFile(MetadataResolverOptions options)
        {
            if (options == null)
            {
                throw new ArgumentException("MetadataResolverOptions could not be null.");
            }

            if (string.IsNullOrEmpty(options.MetadataLocation))
            {
                throw new ArgumentException("MetadataLocation option could not be null or an empty string.");
            }

            try
            {
                // First download the contracts if they are accessed over the web.
                DownloadContract(options);

                AntDiscoveryClientProtocol dcp = new AntDiscoveryClientProtocol();
                dcp.Credentials       = GetCredentials(options);
                dcp.AllowAutoRedirect = true;
                dcp.DiscoverAny(options.MetadataLocation);
                dcp.ResolveAll();

                XmlSchemaSet schemaSet = CreateXmlSchemaSet();
                foreach (string url in dcp.Documents.Keys)
                {
                    object document = dcp.Documents[url];
                    if (document is System.Web.Services.Description.ServiceDescription)
                    {
                        foreach (XmlSchema schema in ((WebServiceDescription)document).Types.Schemas)
                        {
                            if (!IsEmptySchema(schema))
                            {
                                if (options.GenerateSeparateFilesEachXsd)
                                {
                                    if (string.IsNullOrWhiteSpace(schema.SourceUri))
                                    {
                                        schema.SourceUri = url;
                                    }
                                    ResolveIncludedSchemas(schemaSet, schema);
                                }
                                schemaSet.Add(schema);
                            }
                        }
                        continue;
                    }
                    if (document is XmlSchema)
                    {
                        XmlSchema xmlSchema = (XmlSchema)document;
                        if (options.GenerateSeparateFilesEachXsd)
                        {
                            if (string.IsNullOrWhiteSpace(xmlSchema.SourceUri))
                            {
                                xmlSchema.SourceUri = url;
                            }
                            ResolveIncludedSchemas(schemaSet, xmlSchema);
                        }
                        schemaSet.Add(xmlSchema);
                    }
                }

                RemoveDuplicates(ref schemaSet);
                schemaSet.Compile();

                XmlSchemas schemas = new XmlSchemas();
                foreach (XmlSchema schema in schemaSet.Schemas())
                {
                    schemas.Add(schema);
                }

                return(schemas);
            }
            catch (Exception ex)
            {
                // TODO: Log exception.
                throw new MetadataResolveException("Could not resolve metadata. " + ex, ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the XML schemas for generating data contracts.
        /// </summary>
        /// <param name="options">The metadata resolving options.</param>
        /// <returns>A collection of the XML schemas.</returns>
        public static XmlSchemas GetXmlSchemaSetFromDataContractFiles(MetadataResolverOptions options)
        {
            if (options.DataContractFiles == null)
            {
                throw new ArgumentNullException("No data contract files provided");
            }

            // Resolve the schemas.
            XmlSchemaSet schemaSet = CreateXmlSchemaSet();

            for (int fi = 0; fi < options.DataContractFiles.Length; fi++)
            {
                // Skip the non xsd/wsdl files.
                string lowext = Path.GetExtension(options.DataContractFiles[fi]).ToLower();
                if (lowext == ".xsd") // This is an XSD file.
                {
                    XmlTextReader xmltextreader = null;

                    try
                    {
                        xmltextreader = new XmlTextReader(options.DataContractFiles[fi]);
                        XmlSchema schema = XmlSchema.Read(xmltextreader, null);
                        if (options.GenerateSeparateFilesEachXsd)
                        {
                            if (string.IsNullOrWhiteSpace(schema.SourceUri))
                            {
                                schema.SourceUri = options.DataContractFiles[fi];
                            }
                            ResolveIncludedSchemas(schemaSet, schema);
                        }
                        schemaSet.Add(schema);
                    }
                    finally
                    {
                        if (xmltextreader != null)
                        {
                            xmltextreader.Close();
                        }
                    }
                }
                else if (lowext == ".wsdl") // This is a WSDL file.
                {
                    AntDiscoveryClientProtocol dcp = new AntDiscoveryClientProtocol();
                    dcp.AllowAutoRedirect = true;
                    dcp.Credentials       = CredentialCache.DefaultCredentials;
                    dcp.DiscoverAny(options.DataContractFiles[fi]);
                    dcp.ResolveAll();
                    foreach (string url in dcp.Documents.Keys)
                    {
                        object document = dcp.Documents[url];
                        if (document is XmlSchema)
                        {
                            XmlSchema xmlSchema = (XmlSchema)document;
                            if (options.GenerateSeparateFilesEachXsd)
                            {
                                if (string.IsNullOrWhiteSpace(xmlSchema.SourceUri))
                                {
                                    xmlSchema.SourceUri = url;
                                }
                                ResolveIncludedSchemas(schemaSet, xmlSchema);
                            }
                            schemaSet.Add(xmlSchema);
                        }
                        if (document is WebServiceDescription)
                        {
                            foreach (XmlSchema schema in ((WebServiceDescription)document).Types.Schemas)
                            {
                                if (!IsEmptySchema(schema))
                                {
                                    if (options.GenerateSeparateFilesEachXsd)
                                    {
                                        if (string.IsNullOrWhiteSpace(schema.SourceUri))
                                        {
                                            schema.SourceUri = url;
                                        }
                                        ResolveIncludedSchemas(schemaSet, schema);
                                    }
                                    schemaSet.Add(schema);
                                }
                            }
                        }
                    }
                }
            }

            RemoveDuplicates(ref schemaSet);
            schemaSet.Compile();

            XmlSchemas schemas = new XmlSchemas();

            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                schemas.Add(schema);
            }

            return(schemas);
        }