XML Web services are based on a cluster of open standards. Table 5-1 lists the key players.
So how do all these standards fit together? We'll begin with the process of creating the XML Web service, which can be summarized in three steps:
The second step is for the client to find and use your XML Web service:
This process is similar to .NET Remoting in several respects. First of all, the client communicates using a proxy class. The proxy class mimics the XML Web service and handles all the low-level details. Many programming platforms, including .NET, provide tools that create this proxy automatically. The key difference is in how the proxy class is created. With .NET Remoting, the proxy object is generated dynamically. As soon as a .NET client instantiates a remote object, the runtime springs into action, creating the proxy based on the information in the assembly that describes the remote class. With XML Web services, the proxy class is created using the information found in the WSDL document. This is similar to the type of information contained in the assembly metadata, but it's stored in a cross-platform, human-readable XML format. Another difference is the fact that the XML Web service proxy class is created during the development cycle. If the XML Web service changes, the proxy class must be manually regenerated. This process sidesteps the headaches required with .NET Remoting, where the client always needs a reference to the assembly that describes the remote component (or a supported interface). The WSDL document also contains enough information for non-.NET clients to communicate with the XML Web service on their own, without requiring a proprietary tool. We'll look at this issue at the end of the chapter. The Role of IISWith XML Web services, you don't worry about creating a server program that listens for requests and creates the required object. Instead, ASP.NET and IIS work together to perform this service for you. IIS is the software that allows your computer to become a Web server and allows remote clients to download HTML pages (or run ASP.NET pages). IIS is included with Microsoft Windows 2000, Windows XP, and Windows .NET Server, but it isn't necessarily installed by default. To ensure that your computer has IIS installed, try typing the following into your Web browser: http://localhost/localstart.asp. Here, localhost is the special "loopback" alias that always refers to the current computer. (Alternatively, you can use the specific server name or IP address.) localstart.asp is a traditional ASP file that is stored in the root directory of your computer's Web site home directory. If you receive an error when you attempt this request, you should check to make sure you have IIS installed. To install IIS, follow these steps:
You must install IIS on your development computer even if the XML Web service will ultimately be hosted on a different Web server. For those who aren't familiar with IIS, here are the key fundamentals you need to understand before you can create XML Web services:
Creating a Virtual DirectoryBefore you create an XML Web service project in Visual Studio .NET, you should create a dedicated virtual directory. Otherwise, all your code files will be automatically located in a subdirectory of C:\Inetpub\wwwroot, which is the default home directory for IIS. This can cause a great deal of confusion, particularly when it comes time to move your XML Web service project to another computer. Luckily, creating your own virtual directory is easy. Follow these steps:
The XML Web Service File FormatAs mentioned earlier, XML Web services are special files with the extension .asmx. The ASP.NET worker process is registered to handle Web requests for this type of file. When it receives a request, it loads the .asmx file, compiles the appropriate class, executes the relevant Web method, and returns the result. This process is similar to the process used for ASP.NET pages (.aspx files). However, .aspx files contain significant information about the controls used on a Web page. The .asmx file is really just a text document that tells ASP.NET where it can find the relevant XML Web service class. Technically, you can create an .asmx file that contains the code itself, but it's almost always easier (and better in terms of organization) to place the XML Web service class in a separate file or dedicated assembly. This is also the model that Visual Studio .NET enforces. The following sample .asmx file indicates that the XML Web service class called MyWebService can be found in a separate MyWebService.vb code file. ASP.NET is gracious enough to compile this file automatically when needed (a service that ordinary .NET components don't provide). <%@ WebService Language="VB" Codebehind="MyWebService.vb" %> And here's a more typical example that indicates that the XML Web service class is named MyWebService and is already compiled into a DLL assembly called MyAssembly: <%@ WebService Language="VB" %> Most developers will program XML Web services with Visual Studio .NET. In this case, the .asmx file is created automatically. (In fact, Visual Studio .NET doesn't even display the contents of .asmx files in the integrated development environment [IDE], but if you use Notepad to examine these files, you'll see content similar to what's shown here.) Visual Studio .NET also compiles all the XML Web service classes in the project into a single DLL assembly. Therefore, when it comes time to deploy the completed project to a production Web server, all you need to do is copy the individual .asmx files and a single DLL assembly. You can safely leave the Visual Basic source code files behind. Note Every .asmx file contains a reference to one XML Web service. However, a given virtual directory can contain any number of .asmx files, and a Web server can contain any number of virtual directories. The important fact to remember is that each virtual directory acts like a separate ASP.NET application that runs in a dedicated process space and does not share any memory. This becomes important if you use the ASP.NET platform services. XML Web services in the same virtual directory use a common data cache and state collection, which XML Web services in different directories can't access. |