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 Senior Software Engineer with Sorvive Technologies, Inc which provides web application services for B2B. He concentrates on web application development with multiple development tools and is currently evaluating the functionality of .Net and the .Net Framework. Matt continuously looks at emerging technologies to provide customers with the best solutions for their business needs.

 

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:4.75 out of 5
 4 people have rated this page
Article Score21562
Article Series
Comments    Submit Comment

Comment #1  (Posted by RDD on 04/26/2002)

Great articles. I followed along and cut & pasted all code including the DOS commands. I get this error:

Compiler Error Message: BC30002: Type 'EmployeeWSDL.EmployeeSVC' is not defined.

Source Error:

Line 14:
Line 15: Dim myDS As New DataSet()
Line 16: Dim myService As New EmployeeWSDL.EmployeeSVC()
Line 17:
Line 18: myDS = myService.GetAllEmployees()

Source File: c:\inetpub\wwwroot\webforms\EmployeeFRM.aspx Line: 16

Any idea why?
 
Comment #2  (Posted by RDD on 04/26/2002)

I figured it out.

Like usual I skipped the very first steps... I didn't read the details about Configuring IIS. :)

The code works great. Thanks for taking the time to explain ALL of the steps needed to make it work.
 
Comment #3  (Posted by Alex Rotchev on 05/02/2002)


 
Comment #4  (Posted by an unknown user on 11/22/2002)

I tried to configuring IIS as stated on this article and follow all instructions but still i did not make it work, error is

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'EmployeeWSDL.EmployeeSVC' is not defined.

Source Error:

Line 14:
Line 15: Dim myDS As New DataSet()
Line 16: Dim myService As New EmployeeWSDL.EmployeeSVC()
Line 17:
Line 18: myDS = myService.GetAllEmployees()


what could have been wrong. any F1 will be appreciated. By the way i use Microsoft .NET Framework 1.0 Version 1.0.3705

Thanks
 
Comment #5  (Posted by Matt Frame on 11/22/2002)

Please send me all of your source code so I can review the namespace information. Also the article is misleading in the wsdl make section as the two lines listed are really supposed to be one. I apologize that this line is broken into two but HTML automatically wrapped it on the editor.

Let me know what else I can help you with.

Matt
 
Comment #6  (Posted by JRStone on 06/01/2003)

you forgot to create "bin" and moe your dll there
 
Comment #7  (Posted by Matt on 06/01/2003)

To Previous "bin" post: somehow you missed the paragraph following the compile steps that discuss the bin directory.
 
Comment #8  (Posted by Robin on 01/04/2004)

How would you consume it from a remote server?
 
Comment #9  (Posted by Matt Frame on 01/05/2004)

Robin,

If you are using the VS.Net IDE then you would simply add a reference in your project to the web service you have on a remote server. This will allow the IDE to create the proxy stub for you as it will pull the information in for you.

If you are performing the steps in the article then you need to modify the statement:

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

And change the "http://localhost..." to the host and path you have placed your web service on in the dos batch file.

If we placed the web service on www.servera.com then you should be able to test it by entering the following url in a browser from any other workstation within your network, http://www.servera.com/webservices/employeesvc.asmx?WSDL. You can always test a web service with the browser. If you need additional information about this review part 1 of this article.

Good Luck!

Matt

 
Comment #10  (Posted by Joey on 01/15/2004)

Dear all,

I got the following problem, do anyone have any ideas?

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 14:
Line 15: Dim myDS As New DataSet()
Line 16: Dim myService As New EmployeeWSDL.EmployeeSVC()
Line 17:
Line 18: myDS = myService.GetAllEmployees()


 
Comment #11  (Posted by Balachandran on 01/16/2004)

Hello Matt Frame

Superb article. The aricle is very good, clear.
Thanks Mr Matt Frame

Warm Wishes
Balachandran
balu@cascade.no
CatalystOne.Inc. India
 
Comment #12  (Posted by Matt on 01/16/2004)

Joey,

Did you download the code for the article or enter it yourself? I would recommend that you download the code and follow the compilation instructions to make sure you are actually getting a good compile on the DLL. Then move onto the usage of the web service within the aspx web page.

Matt
 
Comment #13  (Posted by Varun on 03/18/2004)

I have already created a web service proxy class's dll ...but how to include that in the web application that I developed using c#...I am using Notepad as editor..
 
Comment #14  (Posted by Varun on 03/18/2004)

I have already created a web service proxy class's dll ...but how to include that in the web application that I developed using c#...I am using Notepad as editor..
 
Comment #15  (Posted by Matt on 03/18/2004)

Varun,

I'm not very familiar with C# but I would assume that you just need to make the necessary changes to the sample for C#.

Sorry but I have not tried the process you are doing.

Matt
 
Comment #16  (Posted by Dmitry Klein on 07/24/2004)

I'm getting the following error when running your example:

System.Data.SqlClient.SqlException: SQL Server does not exist or access denied.
at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)
at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at EmployeeSVC.GetAllEmployees()

Thank you in advance for your help.
 
Comment #17  (Posted by Matt on 07/24/2004)

Dmitry,

Please make sure you change the settings in the SQL connection to match that of your server. For example the following connection is used in Part 1:

dbConn = New SqlConnection("Data Source=(local);Initial " _
& "Catalog=pubs;user id=usr;password=pwd")

If your Data Source is not local then change the Data Source to point to your SQL server location. If the server is named mySqlServer then the statement would look as follows:

dbConn = New SqlConnection("Data Source=mySqlServer;Initial " _
& "Catalog=pubs;user id=usr;password=pwd")

Also verify that you have the correct user id and password changed to your user id and password. If you find that these items are correct and you still get the message let me know and I will see what we can do next.

Thanks,

Matt
 
Comment #18  (Posted by Dmitry on 07/24/2004)

I've certainly changed everything according to my own settings. Is there a special SQL server setup for asp.net? Your help would be greatly appreciated.
 
Comment #19  (Posted by Matt on 07/25/2004)

Dmitry,

Did everything work correctly for you in Part 1 of this article? The first part in the series makes sure your web service successfully communicates with your SQL Server.

No, you do not have to do anything special to work with SQL and ASP.Net.

If you would zip up all your source code and send it to mdframe@msn.com I will take a look and see if I can see your problem.

Thanks,

Matt
 
Comment #20  (Posted by Dmitry on 07/27/2004)

Matt,

Everything actually worked great as far as Part 1. Altough I'm not able to run wsdl. I just can't find it on my system. I'm running Windows XP.

Any thoughts?

Thank you.
 
Comment #21  (Posted by Matt on 07/28/2004)

Dmitry,

The wsdl.exe is part of the .Net Framework. It should be in the area of your system that I described in paragraph 3 of this document. If you were unable to locate it through the file browser and through the search utility then you may need to install the .Net Framework again.

Do you have Visual Studio .Net or are you just trying to use the .Net Framework SDK for performing these tasks? When I wrote the article I just had the SDK on the workstation which was running Windows 2000. Some of the paths may be slightly different. Since my current workstation has Visual Studio .Net I found the wsdl.exe in the following path: C:\Program Files\Microsoft Visual Studio .Net 2003\v1.1\bin.

I do know that until you can find the wsdl.exe utility and perform the steps in part 2 you will have problems completing the sample application.

HTH,

Matt

 
Comment #22  (Posted by an unknown user on 09/09/2005)
Rating
This kind of articles are not always found in the books on the shelf in bookstore.
 
Comment #23  (Posted by an unknown user on 09/26/2006)
Rating
One of the best articles on the web. Thanks for all your time and effort on this.
 
Sponsored Links