Export (0) Print
Expand All

Figure 3

Figure 3 TimeService.asmx
<%@ WebService Language="VB" Class="TimeService"%>

' The previous header line tells ASP.NET that this file contains
' a Web Service written in the Visual Basic language
' and that the name of the class providing that service
' is TimeService

' Import the namespaces (think of them as references)
' required for a web service

Imports System
Imports System.Web.Services

' Declare a new class for our new service. It must inherit
' from the system-provided base class WebService

Public Class TimeService : Inherits WebService

' Place our functions in the class. 
' Mark them as WebMethods

   Public Function <WebMethod()> GetTime (ShowSeconds as Boolean) 
                                 As String

' Perform the business logic of our function
' Find current time, format as requested, and
' return the string

      Dim dt as DateTime

       If (ShowSeconds = TRUE) Then
         GetTime = dt.Now.ToLongTimeString
       Else
        GetTime = dt.Now.ToShortTimeString
       Endif 

   End Function

End Class
Figure 6 Excerpts from Sample Web Service SDL Contract
<?xml version="1.0"?>
<serviceDescription name="TimeService">
<httpget>  
<service>
       <requestResponse name="GetTime" 
           href="http://localhost/AspxTimeDemo/TimeService.asmx/GetTime">
                <request>
                  <param name="ShowSeconds"/>
                </request>
                <response>
                  <mimeXml ref="s1:string"/>
                </response>
          </requestResponse>
</service>
</httpget>

<!— descriptions of other protocols, httpget and soap, go here —>

<schema>
<element name="GetTime">
       <complexType>
              <element name="ShowSeconds" type="boolean"/>
          </complexType>
      </element>
     <element name="GetTimeResult">
       <complexType>
                <element name="result" type="string" nullable="true"/>
          </complexType>
  </element>
</schema>
</serviceDescription>
Figure 8 HTTP POST Support
<httppost>
    <service>
      <requestResponse name="GetTime" href="http://localhost/
          aspxtimedemo/timeservice.asmx/GetTime">
        <request>
          <form>
            <input name="ShowSeconds"/>
          </form>
        </request>
        <response>
          <mimeXml ref="s1:string"/>
        </response>
      </requestResponse>
    </service>
  </httppost>
Figure 10 HTML Code for the Form
<form METHOD="POST" ACTION="http://localhost/aspxtimedemo/
                            timeservice.asmx/GetTime">
  <p>Would you like to show the seconds digits ?</p>
  <blockquote>
    <p><input TYPE="RADIO" NAME="ShowSeconds" VALUE="True" CHECKED> True 
        <input TYPE="RADIO" NAME="ShowSeconds" VALUE="False"> False<br>
    </p>
  </blockquote>
  <input TYPE="SUBMIT" VALUE="Submit Form"> <input TYPE="RESET"
         VALUE="Reset Form">
</form>
Figure 11 Portion of SDL File
<soap xmlns="urn:schemas-xmlsoap-org:soap-sdl-2000-01-25">
    <service>
      <addresses>
        <address uri="http://localhost/aspxtimedemo/timeservice.asmx"/>
      </addresses>
      <requestResponse name="GetTime" soapAction="http://tempuri.org/     
          GetTime">
        <request ref="s0:GetTime"/>
        <response ref="s0:GetTimeResult"/>
      </requestResponse>
    </service>
  </soap>
Figure 13 SOAP Proxy Class Generated by WebServiceUtil
'———————————————————————————————————————
' <autogenerated>
'     This class was generated by a tool.
'     Runtime Version: 2000.14.1812.10
'
'     Changes to this file may cause incorrect behavior 
'     and will be lost if the code is regenerated.
' </autogenerated>
'———————————————————————————————————————

Imports System.Xml.Serialization
Imports System.Web.Services.Protocols
Imports System.Web.Services

Public Class TimeService
    Inherits System.Web.Services.Protocols.SoapClientProtocol
    
    Public Sub New()
        MyBase.New
        Me.Path = "http://localhost/aspxtimedemo/timeservice.asmx"
    End Sub
    
    Public Function 
        "<System.Web.Services.Protocols.SoapMethodAttribute(" _ &
        "http://tempuri.org/ GetTime"), _ &      
        "System.Web.Services.Protocols.SoapParameterAttribute _
        ("showSeconds", ElementName:="ShowSeconds")> _
        GetTime(ByVal showSeconds As Boolean) As String
        Dim results As Object() = Me.Invoke(" _
        GetTime", New Object() {showSeconds})

        Return CType(results(0), String)
    End Function
    
    Public Function BeginGetTime(ByVal showSeconds As Boolean, _
        ByVal callback As System.AsyncCallback, _
        ByVal asyncState As Object) As System.IAsyncResult
        return Me.BeginInvoke("GetTime", New Object() {showSeconds},
                              callback, asyncState)
    End Function

    Public Function EndGetTime(ByVal asyncResult As System.IAsyncResult) 
                               As String
        Dim results As Object() = Me.EndInvoke(asyncResult)
        return CType(results(0),String)
    End Function
    
End Class
Figure 14 Accessing the SOAP Proxy
Public Class Form1
    Inherits System.WinForms.Form

    ' User clicked GetTime button

Protected Sub btnGetTimeSynch_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs)

    ' Create new object of proxy class

    Dim ThisTimeServiceProxy As New TimeService()

    ' Set the Path property to that specified by the user

    ThisTimeServiceProxy.Path = TextBox2.Text

    ' Actually call the function. Put returned result into text box 
    ' for user to see

            TextBox1.Text = 
                     ThisTimeServiceProxy.GetTime(CheckBox1.Checked)
End Sub

' Commence the asynchronous call to get the time

Dim AsyncTimeServiceProxy As TimeService
Dim AsyncResult As IAsyncResult

Protected Sub btn_BeginGetTimeAsync_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs)
    AsyncTimeServiceProxy = New TimeService
    AsyncResult = AsyncTimeServiceProxy.BeginGetTime(CheckBox1.Checked, 
                  Nothing, Nothing)
End Sub

' Check to see if the operation has completed

Protected Sub btnPollForComplete_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs)
    If (AsyncResult.IsCompleted) Then
        MsgBox("Complete")
        Else
        MsgBox("Not Complete")
        End If
End Sub

' Harvest the results of the asynchronous call for getting the time

Protected Sub btnEndGetTimeAsync_Click(ByVal sender As System.Object, 
    ByVal e As System.EventArgs)
    TextBox1.text = AsyncTimeServiceProxy.EndGetTime(AsyncResult)
    AsyncTimeServiceProxy = Nothing
End Sub

End Class
Figure 18 Web Service Code File
Imports System
Imports System.Collections
Imports System.Core
Imports System.ComponentModel
Imports System.Configuration
Imports System.Web.Services
Imports System.Diagnostics
Imports System.Data

Namespace VS7DemoTimeService

    Public Class WebService1
        Inherits System.Web.Services.WebService

        Public Function  <WebMethod()> GetTime(ByVal ShowSeconds As 
                                               Boolean) As String
            

            ' Perform the business logic of our function
            ' Find current time, format as requested, and
            ' return the string

            Dim dt As DateTime
            Dim out As String
  
            out = "The time is now: "
           
            If (ShowSeconds = True) Then
                out = out + dt.Now.ToLongTimeString
            Else
                out = out + dt.Now.ToShortTimeString
            End If

            ' If CallersToday application property is nothing, then it's
            ' the first time
            ' we've run this app. Put in a value of '1', because the caller 
            ' counts

            If (Application("TotalCallers") = Nothing) Then
                Application("TotalCallers") = 1
            Else
                ' Update application state to tell user how many callers 
                ' have asked
                Application("TotalCallers") =  
                Convert.ToInt32(Application("TotalCallers")) + 1
            End If

            ' Format string to tell caller about it

            out = out + "and you're the "
            out = out + Convert.ToString(Application("TotalCallers"))
            out = out + "th person to ask me"

            ' If LastTimeYouCalled property is nothing, then tell use

            If (Session("LastTimeThisGuyAsked") = Nothing) Then
                out = out + ", but you haven't asked before"
            Else
                out = out + " and you last asked at " + _
                      Convert.ToString(Session("LastTimeThisGuyAsked"))
            End If
            Session("LastTimeThisGuyAsked") = dt.Now.ToLongTimeString

            GetTime = out
        End Function

    End Class

End Namespace
Show:
© 2015 Microsoft