
64
|
Chapter 4: Web Services
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Web Service Using Code-Behind
The following snippet is all that’s required for the .asmx file for our code-behind
version of the
Qotd web service (Qotd_cb.asmx):
<%@ WebService Language="VB" Class="aspnetian.Qotd_cb" %>
Note that instead of providing the class name, Qotd_cb, we’ve also added a
namespace name, “aspnetian,” to reduce the likelihood of naming conflicts.
Example 4-3, which contains the code-behind class that implements the web
service, defines this namespace.
Example 4-3. Qotd_cb.vb
Imports System
Imports System.Data
Imports System.Web
Imports System.Web.Services
Namespace aspnetian
<WebService(Namespace:="http://www.aspnetian.com/webservices/")> _
Public Class Qotd_cb
Inherits WebService
<WebMethod( )> _
Public Function GetQotd( ) As String
Dim QuoteDS As New DataSet( )
Dim QuoteXML As String = Server.MapPath("qotd.xml")
Dim QuoteCount As Integer
Dim QuoteNumber As Integer
Dim Randomizer As New Random( )
QuoteDS.ReadXml(QuoteXML)
QuoteCount = QuoteDS.Tables(0).Rows.Count
QuoteNumber = Randomizer.Next(0, QuoteCount)
Return QuoteDS.Tables(0).Rows(QuoteNumber)(0) & "<br /><br />" _
& QuoteDS.Tables(0).Rows(QuoteNumber)(1)
End Function
<WebMethod( )> _
Public Function GetQuoteNumber( ) As Integer
Dim QuoteDS As New DataSet( )
Dim QuoteXML As String ...