- •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 27—SOAP It Up! |
281 |
8.The proxy called on the www.soapitup.com/stockquote.jsp page now can accept the SOAP response packet above and pass the results back to the calling function.
9.The user then receives the HTML page formatted by the stockquote.jsp page.
SOAP Discovery (DISCO)
The SOAP discovery specification allows a consumer or client to automatically determine what services and methods a Web Service provides. Through SOAP discovery, the provider can report this information to the application consumer so that a dialogue can be established. Discovery of Web Services (DISCO) is a specification that defines a document format based on XML and a protocol for getting the discovery document through a known URL. Through DISCO, a developer can explore the published services available at a specific URL. In effect, it allows potential client applications of your Web Service to shop around and explore what is available.
Web Service Description Language (WSDL)
The Services Description Language provides developers of Web Service client applications the detailed information needed to create proxies for accessing the Web service. The WSDL will describe the public functions provided by the Web Service, the required and optional parameters, the ordering of those parameters, as well as the data types. The WSDL also will specify the format, type, and structure of the returned data so that the consumer can appropriately handle the response.
Let’s look at a portion of the WSDL for the StockQuote Service that describes those requests submitted via a HTTP POST request:
...
<httppost xmlns=”urn:schemas-xmlsoap-org:post-sdl-2000-01-25”> <service>
<requestResponse name=”GetQuote” href=”http://www.stockquoteserver.com/StockQuote.asmx/GetQuote”>
<request>
<form>
<input name=”Symbol”/> </form>
</request>
<response>
<mimeXml ref=”s0:double”/> </response>
</requestResponse>
</service>
</httppost>
...
In this example the <request> tags enclose a description of the input parameter <symbol> and the <response> tags enclose the format of the response that can be expected from this service, in this case a value type of double. There is little else required by a consumer of this service in order to begin using the provider.
282 |
Sunday Afternoon |
Using SOAP with ASP.NET
SOAP is the standard wire transmission protocol used when building Web Services with ASP.NET. This provides you as a developer the ability to ignore the details of implementing the specific XML code required to provide the required requests. Additionally, ASP.NET provides built-in facilities for exposing the WSDL of a document by simply appending an ?WSDL to the end of any *.asmx file.
In Session 28, we will examine ASP.NET Web Services in detail so that you can better understand how ASP.NET utilizes these published open standards to implement a distributed computing environment.
REVIEW
By using the power of the SOAP specification, you can easily build distributed applications that have the capability to marshal other resources regardless of location, platform, or programming language. Exposing public methods on your business objects in such a manner greatly enhances reusability, scalability, and integration effectiveness with other applications.
QUIZ YOURSELF
1.If you wanted to find the format of return values supported by a Web Service that was published by a provider what would you refer to? (See “Web Service Description Language (WSDL).”)
2.What existing standards and protocols are used in the SOAP specification? (See “Introducing SOAP.”)
3.What two types of messages are supported by the SOAP specification? (See “Accessing Remote Data with SOAP.”)
S E S S I O N
28
Web Services
Session Checklist
Understanding what Web Services are and what they do
Creating a Web Service
Describing Web Services
Accessing Web Services programmatically
The Internet is currently comprised of an enormous number of heterogeneous entities: servers, operating systems, databases, and so on. Diversity in and of itself isn’t bad, but when it comes to communication between these heterogeneous entities, diversity
can be a problem.
One way to solve these problems is to develop a set of standards that everyone can use to communicate. However, when everyone is attached to their own personal set of standards or protocols, agreeing upon a universal standard becomes nearly impossible. So, what to do?
Well, you take “something” (or “somethings”) that everyone uses, combine them, and turn them into a standard. These “somethings” are HTTP and XML. HTTP, Hypertext Transfer Protocol, is the protocol that essentially all Web browsers and servers use to request and serve Web pages, respectively. XML, Extensible Markup Language, is a cross-platform method for encoding information transferred over HTTP.
Microsoft has, in the .NET Framework, rolled HTTP and XML into something it calls Web Services. A Web Service is a seamless way for objects on a server to accept incoming requests from a client via HTTP and return an XML-based response. Because Web Services are built on HTTP and XML, they can be used by practically everyone on the Internet. In this session, we will demonstrate how to build a Web Service and how to discover and use someone else’s Web Service.
284 |
Sunday Afternoon |
Developing a Web Service
In this example, you will write a Web Service that exposes the data in the Music database, which you have used periodically throughout this book.
You can use any text editor or Visual Studio to build a Web Service. Although the process is simplified tremendously by using Visual Studio, we will use good ol’ Notepad to write our Music Web Service, because we know just about everyone has access to Notepad.
Unlike ASP.NET pages, which have a file extension of .aspx, Web Services have a file extension of .asmx. So, the first thing you need to do is create a file named music.asmx.
Now open the music.asmx file and start it off with the following line of code:
<%@ WebService Language=”VB” class=”Music” %>
This line of code defines your file as a Web Service and indicates that you will be using VB.NET to write the service. The class attribute represents the class that the Web Service will expose.
The next thing you need to do, as with an ASP.NET page, is import the namespaces necessary to implement your Web Service. Because you’ll be accessing the Music database you built in Session 4 with SQL Server, we’ll need to import the System.Data and System.Data.SqlClient namespaces. Additionally, when writing a Web Service you need to import the System.Web.Services namespace. Add the following Imports statements to the music.asmx file:
Imports System
Imports System.Web.Services
Imports System.Data
Imports System.Data.SqlClient
Now that you have imported the appropriate namespaces and thereby have access to the classes necessary for your implementation, you need to declare your class, Music. To define a VB.NET class, we use the following structure:
Class [Class Name]
‘ Properties and Methods go here
End Class
So our Music class declaration will look like this:
Class Music
‘ Properties and Method go here
End Class
Since we are using a Web Service-enabled class, the class should be derived from the Web Service class. In order to accomplish this, our class declaration should be modified as follows:
Class Music Inherits : WebService
‘ Properties and Method go here
End Class
Session 28—Web Services |
285 |
Deriving your class from the WebService class is optional, but we recommend that you do so to improve readability.
Note
Your Web Service has to actually do something, so you need to add a method to the Music class. Your method, call it GetBandInfo for now, will simply return a DataSet containing two DataTables, one with a band’s members and one with a band’s albums when passed the band’s name as a parameter. To add a public method to a class, use the following VB.NET syntax:
Public Function [Function Name]([Parameter Name] As [Data Type], . . .])
‘ Implementation Code
End Function
In order to make a method callable via a Web Service you need to add the <WebMethod()> attribute to your function definition. So, your GetBandInfo method will look like this before you add implementation code:
<WebMethod()> Public Function GetBandInfo(sBandName As String) As DataSet
‘ Implementation code
End Function
Listing 28-1 shows the complete code for the Music class.
Listing 28-1 A Music class example
<%@ WebService Language=”VB” class=”Music” %> Imports System
Imports System.Web.Services Imports System.Data
Imports System.Data.SqlClient
Class Music : Inherits WebService
<WebMethod()> Public Function GetBandInfo(sBandName As String) As
DataSet
Dim oConn As SqlConnection
Dim oCmd As SqlCommand
Dim oDA As SqlDataAdapter
Dim oParam As SqlParameter
Dim oDataSet As New DataSet
oConn = New SqlConnection
With oConn
.ConnectionString = “Data Source=jbutler014a; Initial Catalog=Music;
User ID=music; Password=music”
.Open
End With
oParam = New SqlParameter(“@sBandName”, SqlDbType.VarChar, 100)
Continued
286 |
Sunday Afternoon |
Listing 28-1 |
Continued |
oParam.Value = sBandName
oCmd = New SqlCommand With oCmd
.CommandType = CommandType.StoredProcedure
.CommandText = “prAlbumInfo”
.Parameters.Add(oParam)
.Connection = oConn End With
oDA = New SqlDataAdapter
With oDA
.SelectCommand = oCmd
.Fill(oDataSet, “Albums”)
End With
oCmd.CommandText = “prMemberInfo” oDA.Fill(oDataSet, “Members”)
oDA.Dispose oDA = Nothing oCmd = Nothing oConn.Dispose oConn = Nothing
return oDataSet End Function
End Class
At this point, you should understand all of the implementation code. What you’ve done here is add two tables to the DataSet your Web Service will return.
And that’s it! You have successfully written your first Web Service. Now you must deploy the Web Service. Deploying a Web Service is very simple. If you have your own Web server, create a virtual directory named “Music” and then copy the music.asmx file into this directory. That’s it! To call the Web Service, in your Web browser, simply type in the path to the Web Service. If you actually created the Music virtual directory, the path would look like this:
http://localhost/Music/music.asmx
Figure 28-1 shows the response you should get from the Web Service.