Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

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.