public WebServiceInvoker(Uri webServiceUri) { this.services = new List <string>(); this.availableTypes = new Dictionary <string, Type>(); XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + "?wsdl"); if (ServiceDescription.CanRead(xmlreader)) { this.webServiceAssembly = BuildAssemblyFromWSDL(xmlreader); if (this.webServiceAssembly != null) { Type[] types = this.webServiceAssembly.GetExportedTypes(); foreach (Type type in types) { services.Add(type.FullName); availableTypes.Add(type.FullName, type); } } } }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Usage: hsproxygen [web service uri] [output dir]"); } else { string wsdlUri = args[0]; string outputDir = args[1]; Stream wsdlStream = RetrieveWsdlXml(wsdlUri); XmlTextReader reader = new XmlTextReader(wsdlStream); if (ServiceDescription.CanRead(reader)) { ServiceDescription desc = ServiceDescription.Read(reader); if (wsdlUri.IndexOf("?WSDL") > 0) { desc.RetrievalUrl = wsdlUri.Substring(0, wsdlUri.IndexOf("?WSDL")); } ProxyModel proxyModel = new ProxyModel(desc); Generator.CreateSpecificModule(proxyModel, outputDir); } wsdlStream.Close(); } }
// <Snippet1> // <Snippet2> static void Main() { string myWsdlFileName = "MyWsdl_CS.wsdl"; XmlTextReader myReader = new XmlTextReader(myWsdlFileName); if (ServiceDescription.CanRead(myReader)) { ServiceDescription myDescription = ServiceDescription.Read(myWsdlFileName); // Remove the PortType at index 0 of the collection. PortTypeCollection myPortTypeCollection = myDescription.PortTypes; myPortTypeCollection.Remove(myDescription.PortTypes[0]); // Build a new PortType. PortType myPortType = new PortType(); myPortType.Name = "Service1Soap"; Operation myOperation = CreateOperation("Add", "s0:AddSoapIn", "s0:AddSoapOut", ""); myPortType.Operations.Add(myOperation); // Add a new PortType to the PortType collection of // the ServiceDescription. myDescription.PortTypes.Add(myPortType); myDescription.Write("MyOutWsdl.wsdl"); Console.WriteLine("New WSDL file generated successfully."); } else { Console.WriteLine("This file is not a WSDL file."); } }
public bool LoadServiceDescription(string fileName) { if (File.Exists(fileName)) { using (XmlReader reader = XmlReader.Create(fileName)) { if (ServiceDescription.CanRead(reader)) { serviceDescription = ServiceDescription.Read(reader); return(true); } } } return(false); }
/// <summary> /// Imports the WSDL into a web service description importer, which may /// then be used to generate a proxy class. /// </summary> /// <param name="xmlreader">The WSDL content, described by XML.</param> /// <returns>A ServiceDescriptionImporter that can be used to create a /// proxy class.</returns> private ServiceDescriptionImporter ImportWsdl(XmlTextReader xmlreader) { if (false == ServiceDescription.CanRead(xmlreader)) { throw new Exception("Invalid Web Service Description"); } ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader); ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter(); descriptionImporter.ProtocolName = "Soap"; descriptionImporter.AddServiceDescription(serviceDescription, null, null); descriptionImporter.Style = ServiceDescriptionImportStyle.Client; descriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; return(descriptionImporter); }
private ServiceDescriptionImporter BuildProxy(XmlTextReader reader) { if (!ServiceDescription.CanRead(reader)) { throw new Exception("Invalid webservice description"); } ServiceDescription wsdl = ServiceDescription.Read(reader); ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); importer.ProtocolName = "Soap"; importer.AddServiceDescription(wsdl, null, null); importer.Style = ServiceDescriptionImportStyle.Client; importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties; return(importer); }
/// <summary> /// Builds the web service description importer, which allows us to generate a proxy class based on the /// content of the WSDL described by the XmlTextReader. /// </summary> /// <param name="xmlreader">The WSDL content, described by XML.</param> /// <returns>A ServiceDescriptionImporter that can be used to create a proxy class.</returns> private ServiceDescriptionImporter BuildServiceDescriptionImporter(XmlTextReader xmlreader) { // make sure xml describes a valid wsdl if (!ServiceDescription.CanRead(xmlreader)) { throw new ArgumentException("Invalid Web Service Description", "xmlreader"); } // parse wsdl ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader); // build an importer, that assumes the SOAP protocol, client binding, and generates properties ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter(); descriptionImporter.ProtocolName = "Soap"; descriptionImporter.AddServiceDescription(serviceDescription, null, null); descriptionImporter.Style = ServiceDescriptionImportStyle.Client; descriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; return(descriptionImporter); }
/// <summary> /// Builds the service description. /// </summary> /// <param name="xmlreader">The xmlreader.</param> /// <returns></returns> private static ServiceDescriptionImporter BuildServiceDescription(XmlTextReader xmlreader) { if (!ServiceDescription.CanRead(xmlreader)) { throw new Exception("Invalid Web Service Description"); } ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader); var descriptionImporter = new ServiceDescriptionImporter { ProtocolName = "Soap" }; descriptionImporter.AddServiceDescription(serviceDescription, null, null); descriptionImporter.Style = ServiceDescriptionImportStyle.Client; descriptionImporter.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties; return(descriptionImporter); }
/// <summary> /// Builds the web service description importer, which allows us to generate a proxy class based on the /// content of the WSDL described by the XmlTextReader. /// </summary> /// <param name="xmlreader">The WSDL content, described by XML.</param> /// <returns>A ServiceDescriptionImporter that can be used to create a proxy class.</returns> private ServiceDescriptionImporter BuildServiceDescriptionImporter(XmlTextReader xmlreader) { // make sure xml describes a valid wsdl if (!ServiceDescription.CanRead(xmlreader)) { throw new Exception(ErrorResource.WebServiceDescriptionInvalid); } // parse wsdl ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader); // build an importer, that assumes the SOAP protocol, client binding, and generates properties var descriptionImporter = new ServiceDescriptionImporter { ProtocolName = "Soap" }; descriptionImporter.AddServiceDescription(serviceDescription, null, null); descriptionImporter.Style = ServiceDescriptionImportStyle.Client; descriptionImporter.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties; return(descriptionImporter); }
protected internal override void Resolve(string contentType, Stream stream) { if (ContentType.IsHtml(contentType)) { throw new InvalidContentTypeException(System.Web.Services.Res.GetString("WebInvalidContentType", new object[] { contentType }), contentType); } ServiceDescription description = base.ClientProtocol.Documents[this.Url] as ServiceDescription; if (description == null) { description = ServiceDescription.Read(stream, true); description.RetrievalUrl = this.Url; base.ClientProtocol.Documents[this.Url] = description; } base.ClientProtocol.References[this.Url] = this; ArrayList list = new ArrayList(); foreach (Import import in description.Imports) { if (import.Location != null) { list.Add(import.Location); } } foreach (XmlSchema schema in description.Types.Schemas) { foreach (XmlSchemaExternal external in schema.Includes) { if ((external.SchemaLocation != null) && (external.SchemaLocation.Length > 0)) { list.Add(external.SchemaLocation); } } } foreach (string str in list) { string url = DiscoveryReference.UriToString(this.Url, str); if (base.ClientProtocol.Documents[url] == null) { string str3 = url; try { stream = base.ClientProtocol.Download(ref url, ref contentType); try { if (base.ClientProtocol.Documents[url] == null) { XmlTextReader reader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType))) { WhitespaceHandling = WhitespaceHandling.Significant, XmlResolver = null, DtdProcessing = DtdProcessing.Prohibit }; if (ServiceDescription.CanRead(reader)) { ServiceDescription description2 = ServiceDescription.Read(reader, true); description2.RetrievalUrl = url; base.ClientProtocol.Documents[url] = description2; ContractReference reference = new ContractReference(url, null) { ClientProtocol = base.ClientProtocol }; try { reference.Resolve(contentType, stream); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } reference.Url = str3; if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Resolve", exception); } } } else if (reader.IsStartElement("schema", "http://www.w3.org/2001/XMLSchema")) { base.ClientProtocol.Documents[url] = XmlSchema.Read(reader, null); SchemaReference reference2 = new SchemaReference(url) { ClientProtocol = base.ClientProtocol }; try { reference2.Resolve(contentType, stream); } catch (Exception exception2) { if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException)) { throw; } reference2.Url = str3; if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Resolve", exception2); } } } } } finally { stream.Close(); } } catch (Exception exception3) { if (((exception3 is ThreadAbortException) || (exception3 is StackOverflowException)) || (exception3 is OutOfMemoryException)) { throw; } throw new InvalidDocumentContentsException(System.Web.Services.Res.GetString("TheWSDLDocumentContainsLinksThatCouldNotBeResolved", new object[] { url }), exception3); } } } }
public DiscoveryDocument DiscoverAny(string url) { try { string contentType = null; Stream stream = Download(ref url, ref contentType); if (contentType.IndexOf("text/html") != -1) { // Look for an alternate url StreamReader sr = new StreamReader(stream); string str = sr.ReadToEnd(); string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*"; rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))"; Regex rob = new Regex(rex, RegexOptions.IgnoreCase); Match m = rob.Match(str); if (!m.Success) { throw new InvalidOperationException("The HTML document does not contain Web service discovery information"); } if (url.StartsWith("/")) { Uri uri = new Uri(url); url = uri.GetLeftPart(UriPartial.Authority) + m.Groups[1]; } else { int i = url.LastIndexOf('/'); if (i == -1) { throw new InvalidOperationException("The HTML document does not contain Web service discovery information"); } Uri tmp = new Uri(url); tmp = new Uri(tmp, m.Groups [1].ToString()); url = tmp.ToString(); } stream = Download(ref url); } XmlTextReader reader = new XmlTextReader(url, stream); reader.XmlResolver = null; reader.MoveToContent(); DiscoveryDocument doc; DiscoveryReference refe = null; if (DiscoveryDocument.CanRead(reader)) { doc = DiscoveryDocument.Read(reader); documents.Add(url, doc); refe = new DiscoveryDocumentReference(); AddDiscoReferences(doc); } #if !MOBILE else if (ServiceDescription.CanRead(reader)) { ServiceDescription wsdl = ServiceDescription.Read(reader); documents.Add(url, wsdl); doc = new DiscoveryDocument(); refe = new ContractReference(); doc.References.Add(refe); refe.Url = url; ((ContractReference)refe).ResolveInternal(this, wsdl); } #endif else { XmlSchema schema = XmlSchema.Read(reader, null); documents.Add(url, schema); doc = new DiscoveryDocument(); refe = new SchemaReference(); refe.Url = url; ((SchemaReference)refe).ResolveInternal(this, schema); doc.References.Add(refe); } refe.ClientProtocol = this; refe.Url = url; references.Add(url, refe); reader.Close(); return(doc); } catch (DiscoveryException ex) { throw ex.Exception; } }
/// <include file='doc\ContractReference.uex' path='docs/doc[@for="ContractReference.Resolve"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> protected internal override void Resolve(string contentType, Stream stream) { if (ContentType.IsHtml(contentType)) { throw new InvalidContentTypeException(Res.GetString(Res.WebInvalidContentType, contentType), contentType); } ServiceDescription serviceDescription = ClientProtocol.Documents[Url] as ServiceDescription; if (serviceDescription == null) { serviceDescription = ServiceDescription.Read(stream, true); serviceDescription.RetrievalUrl = Url; ClientProtocol.Documents[Url] = serviceDescription; } ClientProtocol.References[Url] = this; ArrayList importUrls = new ArrayList(); foreach (Import import in serviceDescription.Imports) { if (import.Location != null) { importUrls.Add(import.Location); } } foreach (XmlSchema schema in serviceDescription.Types.Schemas) { foreach (XmlSchemaExternal external in schema.Includes) { if (external.SchemaLocation != null && external.SchemaLocation.Length > 0) { importUrls.Add(external.SchemaLocation); } } } foreach (string urlFromImport in importUrls) { // make the (possibly) relative Uri in the contract fully qualified with respect to the contract URL string importUrl = UriToString(Url, urlFromImport); if (ClientProtocol.Documents[importUrl] != null) { continue; } string oldUrl = importUrl; try { stream = ClientProtocol.Download(ref importUrl, ref contentType); try { //Proceed only if not been here before if (ClientProtocol.Documents[importUrl] == null) { XmlTextReader reader = new XmlTextReader(new StreamReader(stream, RequestResponseUtils.GetEncoding(contentType))); reader.WhitespaceHandling = WhitespaceHandling.Significant; reader.XmlResolver = null; reader.DtdProcessing = DtdProcessing.Prohibit; //Resolve on WSDL and XSD will go recursivelly if (ServiceDescription.CanRead(reader)) { ServiceDescription doc = ServiceDescription.Read(reader, true); doc.RetrievalUrl = importUrl; ClientProtocol.Documents[importUrl] = doc; ContractReference contractReference = new ContractReference(importUrl, null); contractReference.ClientProtocol = ClientProtocol; try { contractReference.Resolve(contentType, stream); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } contractReference.Url = oldUrl; if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Resolve", e); } } } else if (reader.IsStartElement("schema", XmlSchema.Namespace)) { ClientProtocol.Documents[importUrl] = XmlSchema.Read(reader, null); SchemaReference schemaReference = new SchemaReference(importUrl); schemaReference.ClientProtocol = ClientProtocol; try { schemaReference.Resolve(contentType, stream); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } schemaReference.Url = oldUrl; if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Resolve", e); } } } // If it's not XML, or we don't know what kind of XML it is, skip the file. The user // will have to download the dependent file(s) manually, but at least we will continue // to discover files instead of throwing an exception. } } finally { stream.Close(); } } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidDocumentContentsException(Res.GetString(Res.TheWSDLDocumentContainsLinksThatCouldNotBeResolved, importUrl), e); } } }
internal void ResolveInternal(DiscoveryClientProtocol prot, ServiceDescription wsdl) { if (wsdl.Imports == null) { return; } foreach (Import import in wsdl.Imports) { // Make relative uris to absoulte Uri uri = new Uri(BaseUri, import.Location); string url = uri.ToString(); if (prot.Documents.Contains(url)) // Already resolved { continue; } try { string contentType = null; Stream stream = prot.Download(ref url, ref contentType); XmlTextReader reader = new XmlTextReader(url, stream); reader.XmlResolver = null; reader.MoveToContent(); DiscoveryReference refe; if (ServiceDescription.CanRead(reader)) { ServiceDescription refWsdl = ServiceDescription.Read(reader); refe = new ContractReference(); refe.ClientProtocol = prot; refe.Url = url; ((ContractReference)refe).ResolveInternal(prot, refWsdl); prot.Documents.Add(url, refWsdl); } else { XmlSchema schema = XmlSchema.Read(reader, null); refe = new SchemaReference(); refe.ClientProtocol = prot; refe.Url = url; prot.Documents.Add(url, schema); } if (!prot.References.Contains(url)) { prot.References.Add(refe); } reader.Close(); } catch (Exception ex) { ReportError(url, ex); } } foreach (XmlSchema schema in wsdl.Types.Schemas) { // the schema itself is not added to the // references, but it has to resolve includes. Uri uri = BaseUri; string url = uri.ToString(); SchemaReference refe = new SchemaReference(); refe.ClientProtocol = prot; refe.Url = url; refe.ResolveInternal(prot, schema); } }