private static void ResolveImports(MetadataResolverOptions options, MetadataSet metadataSet) { // Resolve metadata using a DiscoveryClientProtocol. DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.Credentials = GetCredentials(options); dcp.AllowAutoRedirect = true; dcp.DiscoverAny(options.MetadataLocation); dcp.ResolveAll(); foreach (object osd in dcp.Documents.Values) { if (osd is System.Web.Services.Description.ServiceDescription) { MetadataSection mds = MetadataSection.CreateFromServiceDescription((System.Web.Services.Description.ServiceDescription)osd); metadataSet.MetadataSections.Add(mds); continue; } if (osd is XmlSchema) { MetadataSection mds = MetadataSection.CreateFromSchema((XmlSchema)osd); metadataSet.MetadataSections.Add(mds); continue; } } }
/// <summary> /// Retrieves and imports meta data for a given WSDL document specified by /// WsdlDocument property. /// </summary> /// <param name="options">The metadata resolving options.</param> /// <returns>A collection of service metadata in XML form.</returns> public static MetadataSet GetMetadataSet(MetadataResolverOptions options) { if (options == null) { throw new ArgumentException("options 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); // Try to run RPC2DocumentLiteral converter. TryTranslateRpc2DocumentLiteral(options); MetadataSet metadataSet = new MetadataSet(); XmlDocument doc = new XmlDocument(); doc.Load(options.MetadataLocation); MetadataSection ms = new MetadataSection(null, null, doc); metadataSet.MetadataSections.Add(ms); ResolveImports(options, metadataSet); return(metadataSet); } catch (Exception ex) { // TODO: Log exception. throw new MetadataResolveException("Could not resolve metadata", ex); } }
/// <summary> /// Retrieves and imports meta data for a given WSDL document specified by /// WsdlDocument property. /// </summary> /// <param name="options">The metadata resolving options.</param> /// <returns>A collection of service metadata in XML form.</returns> public static MetadataSet GetMetadataSet(MetadataResolverOptions options) { if (options == null) { throw new ArgumentException("options 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); // Try to run RPC2DocumentLiteral converter. TryTranslateRpc2DocumentLiteral(options); MetadataSet metadataSet = new MetadataSet(); XmlDocument doc = new XmlDocument(); doc.Load(options.MetadataLocation); MetadataSection ms = new MetadataSection(null, null, doc); metadataSet.MetadataSections.Add(ms); ResolveImports(options, metadataSet); return metadataSet; } catch (Exception ex) { // TODO: Log exception. throw new MetadataResolveException("Could not resolve metadata", ex); } }
public InternalCodeGenerationOptions(MetadataResolverOptions metadataResolverOptions, PrimaryCodeGenerationOptions primaryOptions, CustomCodeGenerationOptions customOptions, CodeWriterOptions writerOptions, CodeGenerationOptions allOptions) { this.metadataResolverOptions = metadataResolverOptions; this.primaryOptions = primaryOptions; this.customOptions = customOptions; this.writerOptions = writerOptions; this.allOptions = allOptions; }
// Filters the metadata resolver options. private static MetadataResolverOptions GetMetadataResolverOptions(CodeGenerationOptions options) { MetadataResolverOptions resolverOptions = new MetadataResolverOptions(); resolverOptions.MetadataLocation = options.MetadataLocation; resolverOptions.DataContractFiles = options.DataContractFiles; resolverOptions.Username = options.Username; resolverOptions.Password = options.Password; return resolverOptions; }
// Filters the metadata resolver options. private static MetadataResolverOptions GetMetadataResolverOptions(CodeGenerationOptions options) { MetadataResolverOptions resolverOptions = new MetadataResolverOptions(); resolverOptions.MetadataLocation = options.MetadataLocation; resolverOptions.DataContractFiles = options.DataContractFiles; resolverOptions.Username = options.Username; resolverOptions.Password = options.Password; return(resolverOptions); }
/// <summary> /// Parses the code generation options specified by options parameter and returns an instance of /// InternalCodeGenearationOptions type. /// </summary> public static InternalCodeGenerationOptions ParseCodeGenerationOptions(CodeGenerationOptions options) { MetadataResolverOptions resolverOptions = GetMetadataResolverOptions(options); PrimaryCodeGenerationOptions primaryOptions = GetPrimaryCodeGenerationOptions(options); CustomCodeGenerationOptions customOptions = GetCustomCodeGenerationOptions(options); CodeWriterOptions writerOptions = GetCodeWriterOptions(options); InternalCodeGenerationOptions icgo = new InternalCodeGenerationOptions(resolverOptions, primaryOptions, customOptions, writerOptions, options); return(icgo); }
/// <summary> /// Returns an object of ICredentials type according to the options. /// </summary> private static ICredentials GetCredentials(MetadataResolverOptions options) { if (string.IsNullOrEmpty(options.Username)) { return(CredentialCache.DefaultCredentials); } else { return(new NetworkCredential(options.Username, options.Password)); } }
private static void TryTranslateRpc2DocumentLiteral(MetadataResolverOptions options) { // TODO: This will not work properly for file names like this my.wsdl.wsdl. string translatedWsdlFilename = options.MetadataLocation.Replace(".wsdl", "_transformed.wsdl"); try { if (Rpc2DocumentLiteralTranslator.ContainsRpcLiteralBindings(options.MetadataLocation)) { // Execute the translation. Rpc2DocumentLiteralTranslator r2d = Rpc2DocumentLiteralTranslator.Translate(options.MetadataLocation, translatedWsdlFilename); options.MetadataLocation = translatedWsdlFilename; } } catch (Rpc2DocumentLiteralTranslationException) { // TODO: Log the exception details.c } }
private static void DownloadContract(MetadataResolverOptions options) { // Return if we don't have an http endpoint. if (!options.MetadataLocation.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase) && !options.MetadataLocation.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)) { return; } // Create a Uri for the specified metadata location. Uri uri = new Uri(options.MetadataLocation); string tempFilename = GetTempFilename(uri); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertValidation); WebRequest req = WebRequest.Create(options.MetadataLocation); WebResponse result = req.GetResponse(); Stream receiveStream = result.GetResponseStream(); WriteStream(receiveStream, tempFilename); options.MetadataLocation = tempFilename; }
/// <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 GetXmlSchemas(MetadataResolverOptions options) { if (options.DataContractFiles == null) { throw new ArgumentNullException("options"); } // Resolve the schemas. XmlSchemas schemas = new XmlSchemas(); 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); XmlSchemaSet schemaset = new XmlSchemaSet(); schemaset.Add(schema); RemoveDuplicates(ref schemaset); schemaset.Compile(); schemas.Add(schema); } finally { if (xmltextreader != null) { xmltextreader.Close(); } } } else if (lowext == ".wsdl") // This is a WSDL file. { XmlSchemaSet schemaset = new XmlSchemaSet(); XmlSchemaSet embeddedschemaset = new XmlSchemaSet(); DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.AllowAutoRedirect = true; dcp.Credentials = CredentialCache.DefaultCredentials; dcp.DiscoverAny(options.DataContractFiles[fi]); dcp.ResolveAll(); foreach (object document in dcp.Documents.Values) { if (document is XmlSchema) { schemaset.Add((XmlSchema)document); schemas.Add((XmlSchema)document); } if (document is WebServiceDescription) { schemas.Add(((WebServiceDescription)document).Types.Schemas); foreach (XmlSchema schema in ((WebServiceDescription)document).Types.Schemas) { embeddedschemaset.Add(schema); schemas.Add(schema); } } } RemoveDuplicates(ref schemaset); schemaset.Add(embeddedschemaset); schemaset.Compile(); } } return(schemas); }
private static void ResolveImports(MetadataResolverOptions options, MetadataSet metadataSet) { // Resolve metadata using a DiscoveryClientProtocol. DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.Credentials = GetCredentials(options); dcp.AllowAutoRedirect = true; dcp.DiscoverAny(options.MetadataLocation); dcp.ResolveAll(); foreach (object osd in dcp.Documents.Values) { if (osd is System.Web.Services.Description.ServiceDescription) { MetadataSection mds = MetadataSection.CreateFromServiceDescription((System.Web.Services.Description.ServiceDescription)osd); metadataSet.MetadataSections.Add(mds); continue; } if (osd is XmlSchema) { //XmlSchema schema = osd as XmlSchema; //if (schema != null) //{ // if ("urn:examples.com:techniques:iepd:commercialVehicleCollision:message:2.0" != schema.TargetNamespace) // { // schema.Compile(null); // } //} MetadataSection mds = MetadataSection.CreateFromSchema((XmlSchema)osd); metadataSet.MetadataSections.Add(mds); continue; } } }
/// <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 GetXmlSchemas(MetadataResolverOptions options) { if (options.DataContractFiles == null) throw new ArgumentNullException("options"); // Resolve the schemas. XmlSchemas schemas = new XmlSchemas(); 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); XmlSchemaSet schemaset = new XmlSchemaSet(); schemaset.Add(schema); RemoveDuplicates(ref schemaset); schemaset.Compile(); schemas.Add(schema); } finally { if (xmltextreader != null) { xmltextreader.Close(); } } } else if (lowext == ".wsdl") // This is a WSDL file. { XmlSchemaSet schemaset = new XmlSchemaSet(); XmlSchemaSet embeddedschemaset = new XmlSchemaSet(); DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.AllowAutoRedirect = true; dcp.Credentials = CredentialCache.DefaultCredentials; dcp.DiscoverAny(options.DataContractFiles[fi]); dcp.ResolveAll(); foreach (object document in dcp.Documents.Values) { if (document is XmlSchema) { schemaset.Add((XmlSchema)document); schemas.Add((XmlSchema)document); } if (document is WebServiceDescription) { schemas.Add(((WebServiceDescription)document).Types.Schemas); foreach (XmlSchema schema in ((WebServiceDescription)document).Types.Schemas) { embeddedschemaset.Add(schema); schemas.Add(schema); } } } RemoveDuplicates(ref schemaset); schemaset.Add(embeddedschemaset); schemaset.Compile(); } } return schemas; }
/// <summary> /// Returns an object of ICredentials type according to the options. /// </summary> private static ICredentials GetCredentials(MetadataResolverOptions options) { if (string.IsNullOrEmpty(options.Username)) { return CredentialCache.DefaultCredentials; } else { return new NetworkCredential(options.Username, options.Password); } }