Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Web Services  »  Create Web Services and Web Forms Programmatically with the .NET Framework SDK. Part 2
Create Web Services and Web Forms Programmatically with the .NET Framework SDK. Part 2
by Matt Frame | Published  03/19/2002 | Web Services | Rating:
Matt Frame

Matt Frame is a Team Technical Architect - AVP with Wells Fargo Advisors. He concentrates his efforts on large scale web applications that are multi-tier and multi-technology. Matt continuously looks at emerging technologies to provide customers with the best solutions for their business needs.

When Matt finds spare time he spends it with his wife and their triplets.

He occasionally finds some personal time to enjoy his hobby of aviation. Matt holds a Private Pilot license and has been flying for more than 18 years. He has flown many different types of aircraft including helicopters.

 

View all articles by Matt Frame...
Create Web Services and Web Forms Programmatically with the .NET Framework SDK. Part 2

Article source code: websvc_prog.zip

In Part 1 of the article we have shown how you can develop a Web Service programmatically. This part will cover the Web Form.

Creating the Web Form

Microsoft basically rewrote ASP and extended it to allow web form development to be as easy as Window Form development with Visual Basic. These new extensions are what have thrusted us into this whole new way of developing web applications.

All Microsoft web services utilize Simple Object Access Protocol (SOAP) for communications. You have two options for using your web service in your web form - 1) You can code all of the SOAP calls yourself or 2) Use Microsoft's Web Service Description Language (WSDL) and their wsdl.exe command line utility to create the code for you. The wsdl.exe utility can create either a Visual Basic or C# proxy module that will then be compiled into DLL for your web form to reference. We will be focusing on the wsdl.exe utility.

Microsoft included a utility with .NET called wsdl.exe, which is command line based. When you installed your .NET Framework SDK a directory path should have been added to your system PATH variable. To test this, open a DOS window and type wsdl. If you receive a group of information about command line options then your environment is setup correctly. If not, you need to add the following path to your environment PATH variable, C:\WINNT\Microsoft.NET\Framework\v1.0.3705. Close and reopen your DOS window and try it again.

While in your DOS window change directories to C:\inetpub\wwwroot\webforms. The command line to generate the VB source for our SOAP proxy is:

wsdl /language:VB /namespace:EmployeeWSDL /out:EmployeeWSDL.vb http://localhost/webservices/employeesvc.asmx?WSDL

This statement tells the wsdl utility to create the proxy in Visual Basic and assign the namespace of EmployeeWSDL. You then just pass the same http information that you can type in your browser. I recommend doing this just to see what the WSDL description looks like. You should now see a new module in the webforms directory called EmployeeWSDL.vb. This module must now be compiled into a DLL.

To compile the Visual Basic proxy into a DLL requires the use of the command line Visual Basic compiler. Again open a DOS window and change directories to webforms where you generated your Visual Basic proxy code from using the wsdl utility. The command this time is as follows:

vbc.exe /r:system.web.services.dll /r:system.dll /r:system.data.dll
/r:system.xml.dll EmployeeWSDL.vb /target:library /out:EmployeeWSDL.dll

This DOS command tells the Visual Basic command line compiler to create a DLL called EmployeeWSDL.dll. Just as a regular Visual Basic 6.0 project would have references to libraries that must be resolved our Imports in the generated proxy EmployeeWSDL.vb must be resolved and this is done by the command line parameters:

/r:system.web.services.dll
/r:system.dll
/r:system.data.dll
/r:system.xml.dll
.

These DLL's can be found in the Microsoft.NET\Framework path mentioned above. We then need to create a /target:library which is command line for DLL and finally what to name the new DLL with the option of /out:EmployeeWSDL.dll. A DOS batch file has been included with the source of this tutorial for compiling the generated WSDL proxy.

Now the final secret to using this newly created DLL. .NET Framework and IIS require that a bin directory exist under the webforms directory where the application is running. So in this case you will need to create a directory under the webforms directory called bin and move the EmployeeWSDL.dll into this directory. Now when your web form references this web service IIS knows where to find the proxy to execute it.

It's important to note that when using Visual Studio.NET and making a web reference to a web service, the above steps are done for you automatically by the IDE. Also if you move your web service to another directory or IIS server you must perform the proxy generation again.

It's time to fire up that ASCII editor again and finally create a web form to consume the web service. Either enter or cut-and-paste the following code into your editor and save it as EmployeeFRM.aspx in your webforms directory.

<% @Page Language="vb" Inherits="System.Web.UI.Page" %>

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="EmployeeWSDL" %>

<HTML>
<HEAD>
<TITLE>Employee Form</TITLE>

<SCRIPT Language="VB" runat="Server">

Sub Page_Load(Src As ObjectE As EventArgs)

     Dim myDS As New DataSet()
     Dim myService As New EmployeeWSDL.EmployeeSVC()

     myDS = myService.GetAllEmployees()
     grdEmployee.DataSource = myDS
     grdEmployee.DataBind()

End Sub

</SCRIPT>
    
</HEAD>

<BODY>
<FORM id="EmployeeFRM" method="post" runat="server">

    <asp:DataGrid id="grdEmployee" runat="server">
        <AlternatingItemStyle BackColor="#DCDCDC"></AlternatingItemStyle>
        <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
        <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084">
        </HeaderStyle>
    </asp:DataGrid>
 
</FORM>
</BODY>
</HTML>
Generated using PrettyCode.Encoder

After you save the EmployeeFRM.aspx file open your internet browser and enter http://localhost/webforms/employeefrm.aspx. You should receive a screen like the one shown in Figure 4.

Just as with the web service the web page is compiled by the Just-in-Time compiler when it is requested.

Now let's review the code to see how these pieces come together.

<% @Page Language="vb" Inherits="System.Web.UI.Page" %>

Just as with web services, web pages have directives. The @Page directive defines page specific attributes used by the ASP.NET compiler. Again we see that the Language is Visual Basic and this page Inherits the functionality of the System.Web.UI.Page.

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="EmployeeWSDL" %>

Here we provide information about which Namespaces, libraries, will be used. The last Import is our reference to the WSDL module created above. If you notice we use the Namespace that was provided on the command line of the wsdl.exe utility.

<HTML>
<HEAD>
<TITLE>Employee Form</TITLE>

<SCRIPT Language="VB" runat="Server">

Sub Page_Load(Src As ObjectE As EventArgs)

     Dim myDS As New DataSet()
     Dim myService As New EmployeeWSDL.EmployeeSVC()

     myDS = myService.GetAllEmployees()
     grdEmployee.DataSource = myDS
     grdEmployee.DataBind()

End Sub

</SCRIPT>

Anyone that has created ASP pages definitely recognizes this code. Here we define what statements should be executed during the Page_Load event, on the server, when this web form is requested. We define our variables, call our web service and then bind the information retrieved from the web service to a DataGrid. A DataGrid is a new ASP.NET control to ease web form development.

</HEAD>

<BODY>
<FORM id="EmployeeFRM" method="post" runat="server">

    <asp:DataGrid id="grdEmployee" runat="server">
        <AlternatingItemStyle BackColor="#DCDCDC"></AlternatingItemStyle>
        <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
        <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084">
        </HeaderStyle>
    </asp:DataGrid>
 
</FORM>
</BODY>
</HTML>

Here we complete the page with the HTML required for formatting and our new web control for the asp:DataGrid. As you can tell by the id of the asp:DataGrid it is bound to the statements of our Page_Load event during the request on the server. I also added a couple of properties to the asp:DataGrid to snaz it up a bit.

I hope this information helps you to understand how all of the pieces fit together in the .NET Framework. If you are using the Visual Studio .NET IDE then this shows you what it is doing under the covers. As you can see, fully functional web applications can be created without the IDE, but many features are valuable tools for the programmer. These tools include intellisense, web form formatting and layout, automated proxy generation for web services, on-line and dynamic help, and of course the capabilities of debugging every part of your web application.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.21052631578948 out of 5
 19 people have rated this page
Article Score32544
Sponsored Links