ASP.NET

Web Services in Visual Basic

For completeness, this tutorial includes a Visual Basic version of the QuoteService. It's in a separate project named QuoteServiceVB. The file name is QuoteServiceVB.asmx. Listing 19-2 shows the Visual Basic code for the service. Listing 19-3 shows the Visual Basic code for the QuotesCollection.VB utility class.

Listing 19-2
Imports
System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO

<Serializable()> _
Public Class QuotesCollection : Inherits DataTable

    Public Sub QuotesCollection()
    End Sub

    Public Sub Synthesize()

        Me.TableName = "Quotations"
        Dim dr As DataRow

        Columns.Add(New _
           DataColumn("Quote", GetType(String)))
        Columns.Add(New _
           DataColumn("OriginatorLastName", GetType(String)))
        Columns.Add(New _
            DataColumn("OriginatorFirstName", GetType(String)))

        dr = Me.NewRow()
        dr(0) = _
           "Imagination is more important than knowledge."
        dr(1) = "Einstein"
        dr(2) = "Albert"
        Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = _
           "Assume a virtue, if you have it not"
        dr(1) = "Shakespeare"
        dr(2) = "William"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
              dr(0) = _
           "A banker is a fellow who lends you his " + _
           "umbrella when the sun is shining, but wants " + _
           "it back the minute it begins to rain."
        dr(1) = "Twain"
        dr(2) = "Mark"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "A man cannot be comfortable without " + _
        "his own approval."
        dr(1) = "Twain"
        dr(2) = "Mark"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Beware the young doctor and the " + _
        "old barber"
        dr(1) = "Franklin"
        dr(2) = "Benjamin"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Reality is merely an illusion, " + _
        "albeit a very persistent one."
        dr(1) = "Einstein"
        dr(2) = "Albert"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Beer has food value, but food has " + _
        "no beer value"
        dr(1) = "Sticker"
        dr(2) = "Bumper"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Research is what I'm doing when I " + _
        "don() 't know what I'm doing"
        dr(1) = "Von Braun"
        dr(2) = "Wernher""
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Whatever is begun in anger ends in shame"
        dr(1) = "Franklin"
        dr(2) = "Benjamim"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "We think in generalities, but " + _
        "we live in details"
        dr(1) = "Whitehead"
        dr(2) = "Alfred North"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "Every really new idea looks crazy " + _
        "at first."
        dr(1) = "Whitehead"
        dr(2) = "Alfred North"
        Me.Rows.Add(dr)

        dr = Me.NewRow()
        dr(0) = "The illiterate of the 21st century will " + _
        "not be those who cannot read and write, but those " + _
        "who cannot learn, unlearn, and relearn."
        dr(1) = "Whitehead"
        dr(2) = "Alfred North"
        Me.Rows.Add(dr)

    End Sub
End Class
Listing 19-3
Imports
System
Imports System.Web
Imports System.Data
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Caching

Public Structure Quote

    Public _strQuote As String
    Public _strOriginatorLastName As String
    Public _strOriginatorFirstName As String

    Public Sub New(ByVal strQuote As String, _
            ByVal strOriginatorLastName As String, _
            ByVal strOriginatorFirstName As String)
        _strQuote = strQuote
        _strOriginatorLastName = strOriginatorLastName
        _strOriginatorFirstName = strOriginatorFirstName
    End Sub

End Structure

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class QuoteServiceVB
    Inherits System.Web.Services.WebService

    Function LoadQuotes() As QuotesCollection

        Dim quotesCollection As QuotesCollection

        Dim ctx As HttpContext
        ctx = HttpContext.Current

        Dim o As Object
        o = ctx.Cache("quotesCollection")

        quotesCollection = CType(o, QuotesCollection)
        If quotesCollection Is Nothing Then
            quotesCollection = New QuotesCollection()
            Dim strAppPath As String

            strAppPath = Server.MapPath("")

            Dim strFilePathXml As String
            strFilePathXml = _
               strAppPath ' _
               "\\app_data\\QuotesCollection.xml"

            Dim strFilePathSchema As String
            strFilePathSchema = _
               strAppPath ' _
               "\\app_data\\QuotesCollection.xsd"

            quotesCollection.ReadXmlSchema(strFilePathSchema)
            quotesCollection.ReadXml(strFilePathXml)

            Dim cacheDependency As CacheDependency
            cacheDependency = New CacheDependency(strFilePathXml)

            ctx.Cache.Insert("quotesCollection", _
                 quotesCollection, _
                 cacheDependency, _
                 Cache.NoAbsoluteExpiration, _
                 Cache.NoSlidingExpiration, _
                 CacheItemPriority.Default, _
               Nothing)
        End If
        Return quotesCollection
    End Function

    Public Sub QuoteService()

    End Sub

    <WebMethod()> _
    Public Function HelloWorld() As String

        Return "Hello World"
    End Function

    <WebMethod()> _
    Public Function GetAQuote() As Quote
        Dim quotesCollection As QuotesCollection
        quotesCollection = Me.LoadQuotes()
        Dim nNumQuotes As Integer
        nNumQuotes = quotesCollection.Rows.Count

        Dim random As Random
        random = New Random()

        Dim nQuote As Integer

        nQuote = random.Next(nNumQuotes)

        Dim dataRow As DataRow

        dataRow = quotesCollection.Rows(nQuote)

        Dim quote As Quote
        quote = New Quote(CType(dataRow("Quote"), String), _
            CType(dataRow("OriginatorLastName"), String), _
            CType(dataRow("OriginatorFirstName"), String))
        Return quote

    End Function

    <WebMethod()> _
    Public Sub AddQuote(ByVal quote As Quote)

        Dim quotesCollection As QuotesCollection
        quotesCollection = Me.LoadQuotes()

        Dim dr As DataRow
        dr = quotesCollection.NewRow()
        dr(0) = quote._strQuote
        dr(1) = quote._strOriginatorLastName
        dr(2) = quote._strOriginatorFirstName
        quotesCollection.Rows.Add(dr)
        Dim strAppPath As String
        strAppPath = Server.MapPath("")
        Dim strFilePathXml As String
        strFilePathXml = _
           strAppPath + "\\app_data\\QuotesCollection.xml"
        Dim strFilePathSchema As String
        strFilePathSchema = _
           strAppPath + "\\app_data\\QuotesCollection.xsd"

        quotesCollection.WriteXmlSchema(strFilePathSchema)
        quotesCollection.WriteXml(strFilePathXml)
    End Sub

    <WebMethod()> _
    Public Function GetAllQuotes() As DataSet
        Dim quotesCollection As QuotesCollection
        quotesCollection = LoadQuotes()
        Dim dataSet As DataSet
        dataSet = New DataSet()
        dataSet.Tables.Add(quotesCollection)
        Return dataSet
    End Function

End Class