- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
Session 28—Web Services |
287 |
Figure 28-1 music.asmx response
The Music Web Service’s response represented in Figure 28-1 is automatically generated by the Web Services runtime. You’ll notice that this response contains several valuable pieces of information including the name of the Web Service, the methods implemented by the Web Service, the required parameters, the return data type and the protocols (such as SOAP, HTTP GET, or HTTP POST) you can use to invoke the Web Service’s methods. You are also provided a facility with which to test the Web Service. Type the name of your favorite band in the sBandName textbox and click the Invoke button. A new browser should open for you with the Web Service’s XML response to your request. Pretty cool, huh?
Consuming a Web Service
There are basically two ways to use a Web Service. You can either call the Web Service directly from your browser, as you just did, or you can use some application to programmatically call the service. Making a direct browser request is easy, but programmatically accessing a Web Service can be a little more difficult especially if you don’t know anything about the particular Web Service you’d like to use. In order to communicate with a Web Service, you need to know what methods it supports, what the input parameters are, and what each method returns. In other words, you need to establish a communication contract with Web Service. So how do you get this information? Luckily, Web Services are able to describe themselves. .NET Web Services automatically produce an XML-formatted Web Service Description Language (WSDL) document that describes the Service. Appending ?WSDL
to a Web Service’s URL returns an WSDL document that a client application can use to discover a Web Service. To obtain the WSDL document for your Music Web Service, use the following URL:
http://localhost/music/music.asmx?SDL
288 |
Sunday Afternoon |
You should get an XML document in your browser window. This XML-formatted WSDL document is effectively your “communication contract” that describes the Web Service. The WSDL, as you’ll see, details the protocols supported by the Web Service — for example, HTTP GET, HTTP POST, or SOAP — as well as the semantics for calling the services and returning values. Here is a small sample of the music.asmx WSDL document:
<soap xmlns=”urn:schemas-xmlsoap-org:soap-sdl-2000-01-25”> <service>
<addresses>
<address uri=”http://localhost/music/music.asmx” />
</addresses>
<requestResponse name=”GetBandInfo” soapAction=”http://tempuri.org/GetBandInfo”>
<request ref=”s0:GetBandInfo” />
<response ref=”s0:GetBandInfoResult” /> </requestResponse>
</service>
</soap>
This section of the WSDL document defines how SOAP (refer to the first line) calls should be made to the Music Web Service. The WSDL also gives the URI (line 4) to use in order to access the Web Service. Perhaps most importantly, the Music SDL described the method it implements. On line 7, you can see that we can request the GetBand Info method.
OK, so now that you know where the Web Service is located and what method(s) it supports, you’re ready to go, right? Well not quite. You now need to build a proxy class through which to access the Web Service from an application, in this case an ASP.NET page.
A proxy is essentially a class that behaves like a local object, but it is actually just a mechanism for communicating with the Web Service. The proxy serializes, sends, receives, and deserializes the method requests. (These activities are commonly referred to as marshaling and transport activities.) There are three ways to create a Web Service proxy:
Using Visual Studio.NET
Using command line tools
Using Internet Explorer 5.5 behaviors
Creating a proxy via VS.NET is simple. It’s essentially the same as adding a reference to a Visual Basic project. IE behaviors are neat, but they’re outside the scope of this book. So, you’re going to build a proxy using a command line tool, WSDL.exe. WSDL.exe is a command line utility that creates a stub or proxy class based on a WSDL document. Once you have created your proxy class, you can then compile the proxy class into an assembly and use it to call a Web Service without needing to write any of the aforementioned marshaling or transport logic.
To create a proxy class based on your Music Web Service, go to your computer’s command prompt and type the following:
WSDL http://localhost/Music/music.asmx?sdl /l:VB /n:myMusic
If all went well, you should have received a message that looks like this:
.\Music.vb
Session 28—Web Services |
289 |
The WebServiceUtil utility accepts many different parameters. The /c[ommand]: switch indicates that you want to create a proxy class. The /pa[th]: switch denotes the location of the SDL upon which you want to base your proxy class. The /l[anguage]: switch denotes in which language the proxy class will be created. You can just as easily create a C# proxy class by changing the language switch to /l:CSharp. Finally, the /n[amespace]: switch indicates the namespace to create the code in. The default is the global namespace. We used myMusic for demonstration purposes.
Note
If you can’t find the proxy class Music.vb, it was created in the directory from which WSDL.exe was executed. This is the default behavior. You could have specified a physical path in which to create the proxy using the /o[ut]: switch. For example, /o:C:\.
Now that you have the proxy class, let’s compile it into an assembly. If you haven’t already done so, create a physical directory named bin in your Music virtual directory’s physical path. OK, now run the following command:
vbc /out:bin\Music.dll /t:library /r:System.dll /r:System.Xml.dll /r:System.Web.Services.dll /r:System.Data.dll music.vb
This command simply creates an assembly, music.dll, based on the Music.vb proxy class and puts it in our /bin directory. The /r[eference]: switch tells the compiler to include a specified assembly. When compiling a Web Service, you must always include
System.XML.dll and System.Web.Services.dll. The System.Data.dll is included because you used ADO.NET in the Music Web Service.
That’s it! You’re done. Now that the music.dll component has been deployed to your /bin directory, you can call the Music Web Service from an ASP.NET page. Here’s an example:
<%@ Page Language=”VB” debug=”true” %> <%@ Import Namespace=”System.Data” %> <script language=”VB” runat=”server”>
Sub Page_Load(Sender As Object, E As EventArgs) Dim oDS As DataSet
Dim wsMusic As New myMusic.Music
oDS = wsMusic.GetBandInfo(“Hootie & The Blowfish”)
dgMembers.DataSource = oDS.Tables(0).DefaultView dgMembers.DataBind
End Sub </script> <html> <body>
<asp:DataGrid id=”dgMembers” BorderWidth=”1” GridLines=”both” runat=”server”/>
</body>
</html>
This is a simple example, but you get the point. You’ll notice that when you declared the variable wsMusic, you used the following line of code:
Dim wsMusic As New myMusic.Music
290 |
Sunday Afternoon |
In this line, you are creating a new object based on the Music class in the myMusic namespace. If you hadn’t specified myMusic as the namespace in which to create the Music proxy class when you executed the WebServiceUtil utility, but rather used the default, you could write:
Dim wsMusic As Music
Once you create the wsMusic object, you can call its methods. In the following line of code, you call the GetBandInfo method and pass it a string, Hootie & The Blowfish:
oDS = wsMusic.GetBandInfo(“Hootie & The Blowfish”)
Because we know the method returns a DataSet, we bound its first table to a DataGrid control, dgMembers.
REVIEW
Web Services are an exciting piece of the .NET Framework that, with the help of XML and HTTP, enable you to easily encapsulate your business logic in .NET components and provide a service to any Web clients that speak HTTP and XML. An XML-based WSDL file, which is generated by the Web Service’s infrastructure, provides a machine-readable description of the functionality available through the Web Service.
QUIZ YOURSELF
1.What is Web Service? (See session introduction.)
2.On which two Internet protocols are Web Services based? (See session introduction.)
3.What methods can be used to access a Web Service? (See “Consuming a Web Service.”)