August, 2000

 A First Look at ASP+



Why Web Services?

One of the coolest new features in ASP+ is the ease with which you can create Web Services.

by Chris Kinsman

  When Microsoft introduced Remote Automation, Visual Basic developers were introduced to the idea of separating their applications into multiple tiers, and then communicating between the tiers using Remote Automation. This process was formalized and later included in Windows as DCOM. Both of these technologies essentially allowed you to connect to a process on a remote machine, and communicate with that remote process.

However, the downside of both implementations is that the process for determining the remote machine and the communication mechanism was primarily Windows-centric. When you attempted to make middle-tier business services available over the Internet, you ran into problems with protocols, authentication, and other issues. Web Services extends this multiple tier paradigm, but opens it up considerably to other platforms and operating systems.

Web Services uses the already familiar URI to perform endpoint mapping. At a minimum, a URI identifies the host and the service you would like to invoke. The method you wish to invoke and the parameters you pass to the method can either be expressed as part of the URL or encapsulated in an XML fragment using the proposed SOAP standard. Results are returned to the caller in the form of XML.

This scheme has a myriad of advantages. First, any platform capable of requesting a Web page and reading the results can interact with this service. Second, all of the infrastructure that has been built up for quickly serving up reliable Web content can be used to scale, load balance, and manage the Web Service.

ASP+ Implementation
ASP+ provides a simplified model for creating Web Services. Instead of writing a .aspx file you write a .asmx file. This file will contain a class with whatever methods the developer wishes to expose. ASP+ will compile the .asmx file on demand, generate the required SDL contract, and create a default page that documents the service and provides a facility for testing it.

Let's look at an example. I decided to take a sample that I frequently use in my VBITS Workshops, Vote, and rewrite it as a Web Service. Vote is currently running on DevX as a COM object written in VB6. The COM object in production takes arguments and returns HTML snippets. In hindsight, this is not the best approach because every user interface change requires a recompile and redeploy of the COM object to our cluster. The rewritten version that I will present here returns its data as XML so that further manipulation may be done via XSL. See Listing 1 for the source code of the ASP+ Web Service. You may run the source code online at aspplus.devx.com/vote.asmx.

Vote Web Service
The Vote Web Service, vote.asmx, consists of a single class, Vote. It interacts with a SQL Server database with the schema shown in Figure 1.

 
Figure 1
Schema Used by the Vote Class


The vtQuestion table holds the actual question text as well as a flag indicating whether or not the question is multiple choice. The vtResponse table also holds the text of the responses as a count of how many times each has been voted for. The vtTotal table holds the total number of responses.

The Vote class exposes multiple methods using the [WebMethod] tag. This tag indicates that the function/subroutine that follows should be exported to the public. Any function without this decoration is private to the Web Service. If you call the Web Service by just accessing vote.asmx, it generates a test harness for you to test each of the methods in the Web Service. See aspplus.devx.com/vote.asmx or Figure 2 for an example of what this looks like.

 
Figure 2
Test Harness Generated by Default When Service Called


In addition to the default test harness, a Web Service exposes a Service Description Language (SDL) contract. The SDL Contract is an XML document that describes each of the methods, the parameters they take, and what they return. This is very similar to a type library in the COM world, except it is expressed as XML. See aspplus.devx.com/vote.asmx?SDL or Figure 3 for an example of what this looks like.

The first method, GetQuestion, accepts a questionid representing a question in the vtQuestion table. It immediately creates an XMLDocument to hold the results in. The namespace for the XMLDocument is imported into the Web Service at the top with the using System.NewXml statement. Note the name of this namespace may change in the future. One of the first problems I ran into when I first tried to run this page were compilation errors complaining that NewXml was not part of the system namespace. To get around this you must reference the assembly within which NewXml is contained inside of a config.web file. The config.web file for this Web Service is shown in Listing 2. Note the add assembly entry which adds the assembly containing System.NewXml. This config.web file should reside in the same directory as vote.asmx.

 
Figure 3
Automatically Generated SDL Contract


After creating an XMLDocument, GetQuestion connects to SQL Server to retrieve information about the question and the available responses. This is where I ran into my second problem. For some reason the framework System.Data.ADO refused to populate a datareader. I switched to System.Data.SQL and with a small change to my connect string, removing the provider, I was off and running. First, a connection is created and opened, then a command is created, and finally a command is executed populating a datareader. If the datareader contains data, several XMLElements are created and appended to the root of the XMLDocument. The datareader is then closed, a new command is created to retrieve the responses, the command is executed, and a datareader is populated. We then loop through the responses using a datareader. The datareader is similar to a read-only, forward-only recordset. In GetQuestion, the datareader is used to iterate through the results of queries using a while construct:

while(dr.Read())
	{
	szText += dr["ResponseText"].ToString();
	}
The above code would loop through the datareader concatenating the contents of each response onto the variable szText. There is no MoveNext to forget! You may also notice that I use the keyword "out" when populating the data reader in this line of code:
cmdResponse.Execute(out dr);
The out keyword indicates that dr is being passed to the Execute method as an output parameter. This is somewhat analogous to the ByRef keyword in Visual Basic. Finally, we close the datareader and database connection, and return the OuterXml of the XMLDocument.

 
Figure 4
Response to Invoking the GetQuestion Method with a QuestionID of 8


Similar techniques are used in the other two methods, RecordVote and GetResults. If we invoke the GetQuestion method using an ID of 8 (aspplus.devx.com/vote.asmx/GetQuestion?iQuestion=8), we will get the results shown in Figure 4.

That's All, Folks
That's all there is to creating a Web Service with ASP+. The new .asmx page type makes it very quick and easy to create new Web Services that can be consumed by anyone on any platform. The output as demonstrated is pure XML, so it could easily be consumed by a Linux or Solaris platform.







 
Resources
• "Introduction to ASP+" article by Chris Kinsman

• "ASP to ASP+ Migration" article by Chris Kinsman

Get the code!

FAQ for ASP+ on ASP Free.com

An Overview of ASP+ on ASP 101

Learn More about ASP+! on 4GuysFromRolla.com

DevX Links
ASP Zone

Ask the ASP Pro

ASP Discussion Group

.NET Resources Page
Start preparing yourself for the impact this next version of ASP and Visual Studio will have on your application development process.




Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers