Solved

WSDL to c#

Posted on 2011-03-11
3
3,973 Views
Last Modified: 2013-11-19
Hello Experts,

I build a winforms application and I would like to parse a wsdl from a wcf service and request for an element's attribute's inner text.
I found two possible solutions, but both of them have some difficulties yet.

A sample WSDL (I left out some details for better readability):


  <?xml version="1.0" encoding="utf-8" ?> 
- <wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" >
+ <wsdl:types>
- <xsd:schema targetNamespace="http://tempuri.org/Imports">
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/" /> 
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
  <xsd:import schemaLocation="http://localhost:6857/Service1.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/WcfService1" /> 
  </xsd:schema>
  </wsdl:types>
+ <wsdl:message name="IService1_GetData_InputMessage">
  <wsdl:part name="parameters" element="tns:GetData" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetData_OutputMessage">
  <wsdl:part name="parameters" element="tns:GetDataResponse" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetDataUsingDataContract_InputMessage">
  <wsdl:part name="parameters" element="tns:GetDataUsingDataContract" /> 
  </wsdl:message>
+ <wsdl:message name="IService1_GetDataUsingDataContract_OutputMessage">
  <wsdl:part name="parameters" element="tns:GetDataUsingDataContractResponse" /> 
  </wsdl:message>
- <wsdl:portType name="IService1">
- <wsdl:operation name="GetData">
  <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetData" message="tns:IService1_GetData_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataResponse" message="tns:IService1_GetData_OutputMessage" /> 
  </wsdl:operation>
- <wsdl:operation name="GetDataUsingDataContract">
  <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContract" message="tns:IService1_GetDataUsingDataContract_InputMessage" /> 
  <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContractResponse" message="tns:IService1_GetDataUsingDataContract_OutputMessage" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="CustomBinding_IService1" type="tns:IService1">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="GetData">
  <soap:operation [b]soapAction="http://tempuri.org/IService1/GetData" style="document"[/b] /> 
+ <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
+ <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="GetDataUsingDataContract">
  <soap:operation [b]soapAction="http://tempuri.org/IService1/GetDataUsingDataContract"[/b] style="document" /> 
+ <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
+ <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
+ <wsdl:service name="Service1">
- <wsdl:port name="CustomBinding_IService1" binding="tns:CustomBinding_IService1">
  <soap:address location="http://localhost:6857/Service1.svc" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

Open in new window


What I am looking for is the soapAction attribute's inner text, highlighted bold, all of them.
The services are hosted on different machines, most of them with "https" address. That is the main problem for the first solution I found with the XmlDocument class.

The next code snippet works fine, until I don't try to load a wsdl on an https address:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("http://localhost:6857/Service1.svc?wsdl");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
            nsmgr.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
            nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");

            XmlNodeList xNodes = xmlDocument.SelectNodes("//wsdl:definitions/wsdl:binding/wsdl:operation/soap:operation", nsmgr);
            if (xNodes != null)
            {
                StringBuilder builder = new StringBuilder();
                foreach (XmlNode node in xNodes)
                {
                    builder.AppendLine(node.Attributes["soapAction"].InnerText);
                }
                MessageBox.Show(builder.ToString());
            }

Open in new window


As soon as I try to load a WSDL that is hosted with https, I get an "System.Net.WebException: The remote server returned an error: (401) Unauthorized." exception.
I know I should authenticate myself, but I don't know how.

The second solution works with the System.Net.HttpWebRequest and System.Web.Services.Description.ServiceDescription classes
UriBuilder uriBuilder = new UriBuilder("[b]https[/b]://example.org/Service.svc");
            uriBuilder.Query = "WSDL";

            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriBuilder.Uri);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Method = "GET";
            webRequest.Accept = "text/xml";        
            webRequest.Credentials = new NetworkCredential("userName", "password");

            System.Web.Services.Description.ServiceDescription serviceDescription;

            using (System.Net.WebResponse response = webRequest.GetResponse())
            using (System.IO.Stream stream = response.GetResponseStream())
            {
                serviceDescription = System.Web.Services.Description.ServiceDescription.Read(stream);
            }

            StringBuilder sb = new StringBuilder();          

            foreach (System.Web.Services.Description.Binding binding in serviceDescription.Bindings)
            {
                foreach (System.Web.Services.Description.OperationBinding operation in binding.Operations)
                {
                    sb.AppendLine(operation.Name);
                }
            }

            MessageBox.Show(sb.ToString());

Open in new window


Here I face the problem that I can't go deep enough in the wsdl. I reach the node <wsdl:operation name="GetData"> but don't reach the <soap:operation soapAction="http://tempuri.org/IService1/GetData" style="document" />  part, so I can only gain the name of the Action, but I need the complete address.

I would be satisfied with any of the solutions or any other if exists.
Thank you for reading so far! :)
0
Comment
Question by:develc
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35108725
XmlDocument.Load() is overloaded to take a stream as an argument, so why not combine the two solutions into one:
UriBuilder uriBuilder = new UriBuilder("[b]https[/b]://example.org/Service.svc");
uriBuilder.Query = "WSDL";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriBuilder.Uri);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Method = "GET";
webRequest.Accept = "text/xml";
webRequest.Credentials = new NetworkCredential("userName", "password");

XmlDocument xmlDocument = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");

using (System.Net.WebResponse response = webRequest.GetResponse())
using (System.IO.Stream stream = response.GetResponseStream())
{
    xmlDocument.Load(stream);

    XmlNodeList xNodes = xmlDocument.SelectNodes("//wsdl:definitions/wsdl:binding/wsdl:operation/soap:operation", nsmgr);
    if (xNodes != null)
    {
        StringBuilder builder = new StringBuilder();
        foreach (XmlNode node in xNodes)
        {
            builder.AppendLine(node.Attributes["soapAction"].InnerText);
        }

        MessageBox.Show(builder.ToString());
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:develc
ID: 35108885
You have absolutely right. It works that way.
Next time I should use my brain. :)
Thank you very much kaufmed!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35108999
Don't sweat it. We all go through brain-block now and again  : )


Glad to help!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question