Creating a Server Based on a WSDL File

Many different businesses and industries are embracing SOAP, including investigating the possibility of creating standardized SOAP interfaces. The first large-scale instance of WSDL being used to define an industry-wide interface is UDDI itself. All operators must implement a common set of interfaces. If they did not, operator sites could not easily share data. The developers on the Visual Studio team have anticipated this and have included the capability to generate a server based on a WSDL file. So, what would you do and what would you get?

In an effort to standardize simple SOAP examples, let's create a standard AddService WSDL. This WSDL file exposes one method that adds two integers and returns the result. Listing 4.26 shows the WSDL file.

Listing 4.26. AddService WSDL File
 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 3:      xmlns:tns="http://mathguys.org/MathService"
 4:      xmlns:s="http://www.w3.org/2001/XMLSchema"
 5:      xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
 6:      targetNamespace="http://mathguys.org/MathService"
 7:      xmlns="http://schemas.xmlsoap.org/wsdl/">
 8:    <message name="AddSoapIn">
 9:      <part name="x" type="s:int" />
10:      <part name="y" type="s:int" />
11:    </message>
12:    <message name="AddSoapOut">
13:      <part name="AddResult" type="s:int" />
14:    </message>
15:    <portType name="AddServiceSoap">
16:      <operation name="Add">
17:        <input message="tns:AddSoapIn" />
18:        <output message="tns:AddSoapOut" />
19:      </operation>
20:    </portType>
21:    <binding name="AddServiceSoap" type="tns:AddServiceSoap">
22:      <soap:binding
23:          transport="http://schemas.xmlsoap.org/soap/http"
24:          style="rpc" />
25:      <operation name="Add">
26:        <soap:operation
27:            soapAction="http://mathguys.org/MathService/Add"
28:            style="rpc" />
29:        <input>
30:          <soap:body use="encoded"
31:              namespace="http://mathguys.org/MathService"
32:              encodingStyle=
33:                  "http://schemas.xmlsoap.org/soap/encoding/" />
34:        </input>
35:        <output>
36:          <soap:body use="encoded"
37:              namespace="http://mathguys.org/MathService"
38:              encodingStyle=
39:                  "http://schemas.xmlsoap.org/soap/encoding/" />
40:        </output>
41:      </operation>
42:    </binding>
43:  </definitions>
					

Notice that the WSDL file does not have a service element. The file should not include one because this WSDL defines the interface only. The WSDL has no knowledge of any existing endpoints. Using this WSDL file, we want to generate a server that implements the interface. That way, any clients that know how to use this predefined interface will know how to use our implementation. To do this, we need to use the WSDL.EXE command line tool.

Open up a Visual Studio .NET command prompt by choosing Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt. This makes sure that all the tools are accessible via the command line. To create the skeleton for the service, type in the following command:

WSDL.EXE /server /language:VB /out:AddService.vb AddService.WSDL

This line tells WSDL.EXE to create a server skeleton in Visual Basic .NET. The skeleton must be based on AddService.WSDL and written to AddService.vb. Listing 4.27 shows the result of this command.

Listing 4.27. Resulting Skeleton from WSDL Command
 1:  Option Strict Off
 2:  Option Explicit On
 3:
 4:  Imports System
 5:  Imports System.ComponentModel
 6:  Imports System.Diagnostics
 7:  Imports System.Web.Services
 8:  Imports System.Web.Services.Protocols
 9:  Imports System.Xml.Serialization
10:
11:  '
12:  'This source code was auto-generated by wsdl, Version=1.0.3427.0.
13:  '
14:
15:  <System.Web.Services.WebServiceBindingAttribute( _
16:      Name:="AddServiceSoap", _
17:      [Namespace]:="http://mathguys.org/MathService")>  _
18:  Public MustInherit Class AddServiceSoap
19:      Inherits System.Web.Services.WebService
20:
21:      <System.Web.Services.WebMethodAttribute(),  _
22:       System.Web.Services.Protocols.SoapRpcMethodAttribute( _
23:          "http://mathguys.org/MathService/Add", _
24:          RequestNamespace:="http://mathguys.org/MathService", _
25:          ResponseNamespace:="http://mathguys.org/MathService")>  _
26:      Public MustOverride Function Add(ByVal x As Integer, _
27:          ByVal y As Integer) As Integer
28:  End Class
					

Using this interface, your implementation simply has to implement the Add function and you are done. The interface is included by adding the interface file to the Web Service implementing the interface. Listing 4.28 contains one sample implementation.

Listing 4.28. An Actual Implementation Using the AddServiceSoap Abstract Base Class
 1:  Imports System.Web.Services
 2:
 3:  <System.Web.Services.WebServiceBindingAttribute( _
 4:      Name:="AddServiceSoap", _
 5:      [Namespace]:="http://mathguys.org/MathService")> _
 6:  Public Class MyAddService
 7:      Inherits AddServiceSoap
 8:
 9:      <System.Web.Services.WebMethodAttribute(), _
10:          System.Web.Services.Protocols.SoapRpcMethodAttribute( _
11:          "http://mathguys.org/MathService/Add", _
12:          RequestNamespace:="http://mathguys.org/MathService", _
13:          ResponseNamespace:="http://mathguys.org/MathService")> _
14:      Public Overrides Function Add( _
15:          ByVal x As Integer, ByVal y As Integer) As Integer
16:          Add = x + y
17:      End Function
18:  End Class

Most of the attribution is handled by Visual Studio .NET when you override the functions in the base class. If special types had been included in this WSDL file, we would have had those appear in the generated code as well. This tool will become more useful as Web Service interfaces become standardized.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182