Creating and Consuming .NET Web Services in 5 Easy Steps

Share this article

Step 3 – Build our Object

OK, now let’s invoke the .NET C# compiler (csc.exe) or VB compiler (vbc.exe) to convert your source file into an assembly or DLL. Create a new .bat file named makelib.bat and type in:

[C#]

csc /t:library /out:binGetSuppliers.dll binGetSuppliers.cs      
 /reference:System.dll,System.Data.dll,System.Web.dll,  
 System.Web.Services.dll,System.XML.dll /optimize

[VB]

vbc /t:library /out:binGetSuppliers.dll binGetSuppliers.vb      
 /reference:System.dll,System.Data.dll,System.Web.dll,  
 System.Web.Services.dll,System.XML.dll /optimize    
pause

All this does is compile the source file from the bin folder to a DLL of set name, to the bin folder.

The /t:library instructs the compiler to create a dll (dynamic link library), rather than an exe (executable) file, with /reference: importing the necessary dll’s libraries that will be used in our Web service. Finally, we /optimize our dll to produce smaller, faster, and more efficient output.

Step 4 – Put it All Together

Now copy and paste the code below to an .aspx .NET page and name it. This code is for the page that will access the assembly/DLL in your bin folder, and with all the Web server controls, pass the appropriate parameters to the Web Service. Go ahead and run it (http://localhost/Websrvce.aspx).

<%@ Page Language="C#" Explicit="true" Strict="true" Buffer="true"%>   
<%@ Import Namespace="System.Data" %>  
<%@ Import Namespace="System.Data.SqlClient" %>  
<%@ Import Namespace="WService" %>  
 
<html>  
<script language="C#" runat="server">  
 
void Page_Load(Object sender, EventArgs e)    
{    
 Response.Flush();  
}  
 
void SubmitBtn_Click (object src, EventArgs e)  
{  
 
 int RcdCount;  
 string Catg = DropDown1.SelectedItem.Text;  
 
 
 //Instantiate DLL  
 GetInfo supInfo = new GetInfo();  
 
 
 //Pass parameter into DLL function  
 DataSet MyData = supInfo.ShowSuppliers(Catg);  
 
 
 MyDataGrid.DataSource = MyData.Tables[0].DefaultView;  
 MyDataGrid.DataBind();  
   
 RcdCount = MyData.Tables[0].Rows.Count;  
 if (RcdCount <= 0)    
 {  
 
 Message.InnerHtml = "<b>No results were found for <FONT    
                    Color=Red><i>"+ Catg +"</i></font>";  
 MyDataGrid.Visible = false; //Hide Results until needed  
 
 }  
 else  
 {  
 
   Message.InnerHtml = "<b><FONT Color=Red><i>" + Catg +  
    "</i></font> has " + RcdCount + " local suppliers</b>";  
 MyDataGrid.Visible = true;  
 }  
}  
 
</script>  
<body style="font: 10pt verdana">  
 
<h4>Accessing Data with Web Services</h4>  
 
<form runat="server">  
 
<asp:DropDownList id=DropDown1 runat="server">  
<asp:ListItem>Australia</asp:ListItem>  
<asp:ListItem>Brazil</asp:ListItem>  
<asp:ListItem>Canada</asp:ListItem>  
<asp:ListItem>Denmark</asp:ListItem>  
<asp:ListItem>Finland</asp:ListItem>  
<asp:ListItem>France</asp:ListItem>  
<asp:ListItem>Germany</asp:ListItem>  
<asp:ListItem>Italy</asp:ListItem>  
<asp:ListItem>Japan</asp:ListItem>  
<asp:ListItem>Netherlands</asp:ListItem>  
<asp:ListItem>Norway</asp:ListItem>  
<asp:ListItem>Singapore</asp:ListItem>  
<asp:ListItem>Spain</asp:ListItem>  
<asp:ListItem>Sweden</asp:ListItem>  
<asp:ListItem>UK</asp:ListItem>  
<asp:ListItem>USA</asp:ListItem>  
</asp:DropDownList>  
<asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/>  
<p>  
<span id="Message" runat="server"/>  
 
<p>  
 
 
<ASP:DataGrid id="MyDataGrid" runat="server"  
AutoGenerateColumns="True"  
Width="100%"  
BackColor="White"  
Border="1"  
BorderWidth="1"    
CellPadding="1"  
CellSpacing="1"  
Font-Size="10pt"  
HeaderStyle-BackColor="White"  
HeaderStyle-ForeColor="Blue"  
AlternatingItemStyle-BackColor="White"  
AlternatingItemStyle-ForeColor="Black"  
ShowFooter="false"  
/>  
 
</form>  
 
</body>  
</html>

How It Works

Pretty cool, huh? Now, aside from the common data accessing and Web server control usage, I’ll outline some specifics. By virtue of creating our proxy class with a namespace in Step 2, we need to import it to the dll we created in Step 3, like this:

<%@ Import Namespace="WService" %>

We then instantiate it or reference our object:
[C#]

GetInfo supInfo = new GetInfo();

[VB]

Dim supInfo As New WService.GetInfo()

This is our Web Service class. We then retrieve our dropdown list parameter, and pass it to our ShowSuppliers constructor method like so:

[C#]
string Catg = DropDown1.SelectedItem.Text;  
 
DataSet MyData = supInfo.ShowSuppliers(Catg);

[VB]

Dim Catg As String = DropDown1.SelectedItem.Text   
 
Dim MyData As DataSet = supInfo.ShowSuppliers (Catg)

And the rest? Just common .NET stuff, which I assume you have some grip on. Nevertheless, it shouldn’t be too hard to follow or figure out. The .NET documentation and Quickstart Tutorials will help with plenty more information.
Well, guess what? We’re done!

If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Introductory Web Development Using ASP.NET.

Go to page: 1 | 2 | 3 | 4
Dimitrios MarkatosDimitrios Markatos
View Author

Dimitrios is an expert .NET Architect and has written over a dozen articles covering various topics on .NET, and has been published on 4 Guys from Rolla, Dot Net Junkies, MSDN Academic Alliance, The Official Microsoft ASP.NET Site, and here on SitePoint.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week