< IIS Web Log, ASP .NET Config & Temp Files | Main | Adding New VB .NET Module (DLL) >


 

 

Chapter 11 Part 12:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 12?

 

  1. VB .NET/ASP .NET Asynchronous Web Service Program Example

  2. Creating the ASP .NET/VB .NET code Web Service

  3. Build and Run the Web Service Project

  4. ASP .NET Development Server

  5. Testing the Exposed Web Service Methods

VB .NET/ASP .NET Asynchronous Web Service Program Example

 

In this exercise we will create two projects in the same solution. The first project is web service application (DLL) and the second project is VB .NET module (DLL). The project startup will be web page from the web service application.

 

Creating the ASP .NET/VB .NET code Web Service

 

Create a new ASP .NET/VB .NET web service application.

 

The VB .NET Asynchronous Web Service Program Example: creating a new ASP .NET web service application

 

You can use the AuthServiceASPVB as the solution and project name if you want.

 

VB .NET Asynchronous Web Service Program Example: choosing the ASP .NET web service application project template

 

The ASP web service project template was given for us to customize it. Add/edit the following code.

 

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.ComponentModel

 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

' <System.Web.Script.Services.ScriptService()> _

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _

<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<ToolboxItem(False)> _

Public Class Service1

    Inherits System.Web.Services.WebService

    'a simple addition operation which return the sum

    <WebMethod()> _

    Public Function Sum(ByVal lhs As Integer, ByVal rhs As Integer) As Integer

        Return lhs + rhs

    End Function

 

    <WebMethod()> _

    Public Function IsAuthorized(ByVal userName As String) As Boolean

        'shun John and let everyone else in

        Dim r As New Regex("john", RegexOptions.IgnoreCase)

        Dim m As Match = r.Match(userName)

        'if john

        If (m.Success) Then

            Return False

        Else ' others

            Return True

        End If

    End Function

End Class

 

Build and Run the Web Service Project

 

Next, build the project and make sure there is no error.

 

VB .NET Asynchronous Web Service Program Example: building the web service project solution

 

VB .NET Asynchronous Web Service Program Example: the generated web service DLL file seen in the Output window

 

Then run the project. Make sure there is no error.

 

VB .NET Asynchronous Web Service Program Example: running the web service program without debugging

 

The following is the web service test page run by the ASP .NET development server.

 

VB .NET Asynchronous Web Service Program Example: the ASP .NET/VB .NET web service test page seen in browser

 

 

 

 

ASP .NET Development Server

 

The ASP .NET development server short cut can be seen in the icon tray.

 

VB .NET Asynchronous Web Service Program Example: the ASP .NET development server icon tray

 

You can right-click the icon and we have three menus as shown below. Click the Show Details menu.

 

VB .NET Asynchronous Web Service Program Example: invoking the detail ingo of the ASP .NET development server

 

The following is the ASP .NET development server brief settings.

 

VB .NET Asynchronous Web Service Program Example: the ASP .NET development server brief info

 

Testing the Exposed Web Service Methods

 

Next, let do some testing on the exposed methods. Click the IsAuthorized method, type John as the userName value and click the Invoke button.

 

VB .NET Asynchronous Web Service Program Example: testing one of the exposed web service method, ISAuthorized

 

The XML page will be displayed with the false value.

 

VB .NET Asynchronous Web Service Program Example: the web service result displayed in the XML page

 

Next, test the Sum method. Key-in integer values and click the Invoke button.

 

VB .NET Asynchronous Web Service Program Example: testing another web service method, Sum

 

The XML page shows the sum result.

 

VB .NET Asynchronous Web Service Program Example: the web service Sum result displayed in the XML page

 

Then, stop the ASP development server using the icon tray.

 

VB .NET Asynchronous Web Service Program Example: stopping the ASP .NET development server

 

In the real development, to use the ASP web service, we need to create another HTML or asp page and you can try it by using the following code. Add new HTML page named index.

 

VB .NET Asynchronous Web Service Program Example: adding a new item to the existing project in Visual Studio 2008

------------------------------------------------------------------

-------------------------------------------------------------------

VB .NET Asynchronous Web Service Program Example: selesting and adding the HTML page to the existing project

 

Add/edit the code as shown below. It is just a simple form with a text box that receives an input.

 

<head>

    <title>The ASP web service test page</title>

</head>

<body>

<form method="post" action='http://localhost:1707/Service1.asmx/IsAuthorized'>

       <input type="text" size="5" name="userName" style="width: 223px" />

       <input type="submit" value="IsAuthorized?" />

   </form>

</body>

</html>

 

The VB .NET Asynchronous Web Service Program Example: the HTML code

 

You can see the design by switching the editor to the design mode.

 

The VB .NET Asynchronous Web Service Program Example: the HTML form sample

 

Next, test the page directly from the editor. Right-click mouse anywhere in the editor > Select View in Browser context menu.

 

The VB .NET Asynchronous Web Service Program Example: testing the HTML page from Visual Studio IDE

 

Our HTML page with a simple form that receives an input looks fine. Type "John" and click the IsAuthorized button.

 

The VB .NET Asynchronous Web Service Program Example: the HTML form opened in browser

 

Well, all looks OK. Close the web and XML page.

 

The VB .NET Asynchronous Web Service Program Example: the XML result page

 

Next let add the access denied page for the denied access user which is for John. In this case we are using asp page and name it as accessdenied.

 

The VB .NET Asynchronous Web Service Program Example: adding a new item into existing project

 

The VB .NET Asynchronous Web Service Program Example: adding a web form into existing project

 

Add/edit the following code.

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>The access denied page</title>

</head>

<body>

    <center><h1>Access Denied from AsyncModule</h1></center>

</body>

</html>

 

 

 

 

The VB .NET Asynchronous Web Service Program Example: the asp page using vb .net code

 

Test the asp page directly from the editor.

 

The VB .NET Asynchronous Web Service Program Example: testing the asp page directly from Visual Studio IDE

 

The result is as intended. Close the page.

 

The VB .NET Asynchronous Web Service Program Example: the asp page opened in browser

 

Then add the default page using asp which is for user other than John.

 

The VB .NET Asynchronous Web Service Program Example: adding a new item into the existing Visual Studio project

 

The VB .NET Asynchronous Web Service Program Example: adding a web form into existing project

 

Add/edit the code as shown below.

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>The default asp page</title>

</head>

<body>

<center>

<%

  Response.Write("Hello, " & Server.HtmlEncode(User.Identity.Name))

%>

</center>

</body>

</html>

 

The following screenshot shows the code.

 

The VB .NET Asynchronous Web Service Program Example: the asp page with vb code

 

Test the page directly from the editor.

 

The VB .NET Asynchronous Web Service Program Example: testing the asp page directly from Visual Studio IDE

 

The page looks fine. Close the page.

 

The VB .NET Asynchronous Web Service Program Example: the asp page opened in the browser

 

We can set this page as the start page (though default.xxx page normally will be treated as the default web page by default).

 

The VB .NET Asynchronous Web Service Program Example: setting the web page as a start page

 

 


< IIS Web Log, ASP .NET Config & Temp Files | Main | Adding New VB .NET Module (DLL) >