Building and Deploying XML Web Services

The following sections explain how an XML Web service is built and deployed, a process that’s often called publishing an XML Web service. Although there seem to be a lot of steps to be performed, the process is simple and easily repeated.

Deployment Directory

A deployment directory must be created to house the XML Web service files. To make the XML Web service accessible, the deployment directory must be made available through IIS.

The easiest way to configure IIS is to use the IIS Server Management Wizard, which on Windows 2000 and Windows XP—consult the IIS documentation for other Windows operating systems—can be opened by right-clicking the My Computer icon, selecting Manage, expanding the Services And Applications tree, and selecting the IIS tree. This opens the IIS Server Management Wizard. To create a virtual directory, right-click the Default Web Site tree, select New, and then select Virtual Directory. For the working example, create a virtual directory named SumService that maps incoming requests to the underlying deployment directory—for example, C:SumService.

Remember that the name of the virtual directory and the name of the actual directory don’t need to match; we have made them match merely to keep things simple. The default settings for a virtual directory are suitable for an XML Web service; consult the IIS documentation for more details. Before continuing, a word of caution: using IIS safely and securely requires a thorough comprehension of the IIS documentation and vigilance in applying security patches.

Compiling

XML Web services are compiled in the same manner as normal C# source files; however, the target must be a library, not an application executable. To compile the example, save it using the filename Sum.cs. The following command will compile this example into a library that can be deployed to IIS:

csc /out:SumService.dll /target:library Sum.cs

More Info

For more information about using the C# compiler, see Chapter 4.

The assembly that the compiler creates must be placed in a subdirectory named bin contained within the XML Web service deployment directory created earlier with the IIS Wizard—for example, C:SumServicein.

Creating the Directive

To deploy an XML Web service, a directive file must be created, acting as a bridge between ASP.NET and the assembly containing the XML Web service code. The file should have the same name as the XML Web service but with the .asmx extension, which for our example would be SumService.asmx. The complete contents of the file for our example service are shown here:

<%@ WebService Class="SumService.Sum" %>

The Class attribute defines the C# class that will be used for the XML Web service. This class must be contained within an assembly in the bin subdirectory of the deployment directory; the common laguage runtime (CLR) will search for an assembly that contains the specified class.

It’s also possible to specify the assembly that the CLR should use, avoiding the need to perform a potentially time-consuming search; the assembly name is appended after the class name, as shown in the following example:

<%@ WebService Class="SumService.Sum, SumService" %>

The directive file must be placed in the root of the deployment directory.

The URL to access an XML Web service includes the name of the directive file, taking the following form:

http://<host name>/<virtual directory>/<directive file>.asmx

For our example, the URL would become:

http://localhost/SumService/SumService.asmx

Configuring XML Web Services

XML Web services can be configured using XML files, in much the same way that application configuration files are used; for more information, see Appendix C. XML Web service configuration is contained in the configuration/system.web/webservices element. The allowed subelements are listed in Table 19-4. XML Web services don’t require a configuration file to operate; if a file isn’t found, default values will be used.

Table 19-4. XML Web Service Configuration Subelements

Element

Description

<protocols>

See the Protocols section of this chapter for more information.

<serviceDescriptionFormatExtensionTypes>

Advanced SOAP settings. Consult the .NET documentation for more details.

<soapExtensionTypes>

 

<soapExtensionReflectorTypes>

 

<soapExtensionImporterTypes>

 

<wsdlHelpGenerator>

Specifies a Web Forms page to display when the XML Web service is queried by a Web browser. Consult the .NET documentation for more information.

XML Web service configuration files are stored in the root of the deployment directory and are named Web.config; the .NET runtime automatically loads the configuration file.

Protocols

The only configuration element relevant to this chapter is <protocols>. This setting defines how an XML Web service can be used and accessed. The supported protocols are listed in Table 19-5.

Table 19-5. Supported XML Web Service Protocols

Protocol

Description

Documentation

A special protocol that allows XML Web services to be tested with a browser. See the section Testing XML Web Services coming up for more details.

HttpSoap

SOAP over HTTP protocol.

HttpGet

HTTP GET requests, where parameters for XML Web service methods are appended to the URL after a question mark (?).

HttpPost

HTTP POST requests, where the parameters for XML Web service methods are passed in the free-format section of the request.

The following configuration file demonstrates how to enable all four protocols:

<configuration>
   <system.web>
      <webServices>
        <protocols>
            <add name="Documentation"/>
            <add name="HttpSoap"/>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
         </protocols>
      </webServices>
   </system.web>
</configuration>

Examples of how queries and responses for each of the protocols should be formatted can be obtained by using a Web browser to examine the XML Web service. See the Testing XML Web Services section coming up for more information.

The settings in the XML Web service configuration file are overridden by those in the machine configuration file. For more information, see Appendix C.

Testing XML Web Services

Once deployed, an XML Web service can be tested using a browser, such as Internet Explorer. The browser interface provides details of the SOAP messages that are required to invoke a Web method and allows the programmer to test Web methods by entering parameters and viewing the XML response.

To test an XML Web service, view the URL described in the preceding section. The XML Web service detects that the client is a browser, and an HTML interface for the XML Web service is displayed.

Appending the ?WDSL suffix to the URL returns the WDSL document for the XML Web service.

Summary

Building and deploying an XML Web service is a simple task. However, it’s important that the directive file and the code assembly be located correctly. For our example, the file locations are

<deployment directory>/SumService.asmx
<deployment directory>/bin/SumService.dll
..................Content has been hidden....................

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