Sei sulla pagina 1di 8

Original URL: http://www.dotnetjunkies.com/Tutorial/4D13CEFA-D0FD-44BE-8749-8D17B5757564.

dcik

Creating and Consuming .NET Web Services in Five Easy


Steps
By Dimitrios Markatos
Tell a Friend
Published: 11/19/2002
Reader Level: Beginner Rate this Article
Rated: 2.50 by 2 member(s).
Printable Version
Discuss in the
Forums

Introduction

The Internet has a new player on the scene that has received a great deal of hype and even some television
commercials. They imply that this new next generation technology will change the way business is done on
the Web. They demonstrate that companies, their applications or software and any Internet-enabled devices
will easily be able to communicate with one another regardless of platform or language, and provide services
to each other. Sounds revolutionary! It really is quite exciting. So, what exactly is this, that opens such lines
of communication among such diversity? Two words: Web Services, that's what!

Without further ado, let me start off by briefly describing some methods and standards.

Web services give developers the ability to utilize four open web standards:

1. HTTP - Hypertext Transfer Protocol - the standard protocol used over Port 80 that traverses firewalls,
which is responsible for requesting and transmitting data over the Internet.
2. SOAP - Simple Object Access Protocol - an XML inherent protocol enclosing a set of rules for data
description and process that is the center piece paralleling and complementing the other three mentioned
here, with which .NET utilizes nicely with Web Services.
3. XML - Extensible Markup Language - the prominent markup language that all this commonly
understood information is written in
4. WSDL - Web Services Description Language - an XML based method use in identifying Web Services and
their access at runtime. .NET provides a tool called WSDL.exe which essentially makes it quite easy in
generating an XML web service, rather an XML file containing all the methods and instructions the Web
Service has, using SOAP as its default btw.

This article will have you creating and consuming a data driven .NET XML Web service in no time flat, with
our example providing a list of suppliers to a given country, all in five quick and easy steps!

Incidentally, this article assumes that the reader has a decent grasp in common .NET data access, Web
server controls, such a datagrid and some object-oriented programming concepts. If not, don't worry. By
doing the examples, and viewing the results, you should have no difficulty in keeping up and observing cause
and effect.

Prior to .NET there were other alternatives in accessing a Web service, such as Microsoft's MSXML component,
that enabled you to communicate with the given Web Service over HTTP POST. This process however, while
acceptable, is just not .NET.

At any rate, let's begin!

Step 1 - Create the Web Service

Begin now by creating the Web service function or method that you'll be exposing, rather calling over the
Internet, as you would any object-oriented class. Except incorporate and import all the necessary Web
Services namespaces, syntax and attributes, as well as our data namespaces in this case. Since this article in
shown in C#, any important differences regarding VB will be shown as well.

So go ahead and copy the code below to suppliers.asmx. Save it to your Inetpub/wwwroot folder, and
then run it in your browser, like so: http://localhost/suppliers.asmx. What you'll see is the Web Service
Descriptions listed, the Web service name and the exposed method.

By clicking the exposed method link, you'll be presented with the three main protocols available for use. Just
so you know, the file with the .asmx extension is the actual Web Service file, that enables the ASP.NET
runtime to return the all pertinent exposed web service methods and information.

Code:
<%@ WebService Language="C#" Class="GetInfo" %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

[WebService(Description="My Suppliers List Web Service")]

public class GetInfo : WebService {

[WebMethod(BufferResponse=true)]

public DataSet ShowSuppliers (string str) {

SqlConnection dbConnection = new


SqlConnection("server=(local);uid=sa;pwd=;database=Northwind;");

SqlDataAdapter objCommand = new SqlDataAdapter("select ContactName, CompanyName, City,


Phone from Suppliers where Country = '" + str + "' order by ContactName asc",
dbConnection);

DataSet DS = new DataSet();

objCommand.Fill(DS);

return DS;

dbConnection.Close();
dbConnection = null;

}
Quick Explanation:

The <%@ WebService Language="C#" Class="GetInfo" %> directive sets up the file as a Web Service, gives
it a name and specifies the language its using.

Aside from your typical data namespace importing, you also add the Web Services namespace.

using System.Web.Services; and in VB it would be - Imports System.Web.Services

Here I've added the [WebService(Description="My Suppliers List Web Service")] which just gives a
custom description. Next we create our class which has to be a public class for it to be exposed, and inherit
the WebService base class.

public class GetInfo : WebService {


Now we mark the method to be exposed via the WebMethod attribute. Every class enclosed within a Web
method get's exposed as a service. Also I add some performance boost by setting the BufferResponse to
true.

[WebMethod (BufferResponse=true)]

If you prefer to have the description right below the exposed WebMethod link then you can write this in C#:

[WebMethod (Description="My Suppliers List Web Service",BufferResponse=true)]

The VB version would incorporate the description and BufferResponse in one statement namely just
WebMethod, using angle brackets instead. Moreover, this is placed after the class creation and Web service
inheriting line, and on the same line with and before the function, like so:

<WebMethod(Description:="My Suppliers List Web Service",BufferResponse:=True)> Public


Function _
ShowSuppliers (ByVal str As String) As DataSet

Next, we set up our method or VB function in question, that will accept a string parameter from our drop
down list and pass this to our SQL query (you'll see all this in detail in Step 4). If you're really savvy you can
pass this and other parameters with the help of C# structs (scaled down classes) to enumerate your data
value types, and provide you with better memory allocation. But for now we'll stick to basics like so:

public DataSet ShowSuppliers (string str)

Above we set up our dataset method to return us exactly that. Lastly, we perform typical data connection
and access and return our results. And we're done! So far so good? After viewing this in your browser, the
above code should to start to make some more sense by now. Let's go to Step 2.

Step 2 - Consume the Web Service Source file

Next append ?WSDL to the web services URI (Uniform Resource Identifier) like so -
http://localhost/suppliers.asmx?WSDL. What's this you ask? This is the WSDL document that is exactly
what the client will use to access this service.

Even so, you don't have to know much about this unreadable code to produce results, which is where the
WSDL.exe command-line tool comes to the rescue. Due to Web services open protocol nature, this tool
further enables you to consume non-.NET Web Services as well.

You can bypass WSDL and test the XML results instantly through HTTP GET protocol, by typing in your
browser - http://localhost/suppliers.asmx/ShowSuppliers?str=USA which passes USA as a parameter
to the ShowSuppliers class method. Note: If you're using .NET SDK Beta 1.1 (v.1.1.4322), it seems to prefer
HTTP POST protocol, so invoke the Web Service through its exposed method link. The XML results? A little
crazy huh? Nevertheless, scroll down a little and you'll see the query results of our class call. Later in Step 4,
all this gooblygook will make much more readable sense.

So to create our proxy class sourcefile, make a batch file named makeWS.bat and type in:
[C#]

wsdl.exe /l:CS /n:WService /out:bin/GetSuppliers.cs http://localhost/suppliers.asmx?WSDL

[VB]

wsdl.exe /l:VB /n:WService /out:bin/GetSuppliers.vb http://localhost/suppliers.asmx?WSDL

pause
This tells our WSDL tool:

/l: is the language your using. /n: creates the WService namespace so you could reference it from your
.aspx page, and finally send the C#/VB source file to the bin folder from which we'll create our dll assembly
to use in Step 3. Also add pause, so after this is all said and done, you'll be prompted to press any key to
continue, rather more importantly knowing for sure all went according to plan.

So, now locate your new batch file and double-click on it to run it. Once you have done this, you will have
created, rather consumed, the proxy class or source file GetSuppliers.cs right from the .asmx file. Have a
look in your bin folder.

Note: You could run these commands via the command-line prompt, but for convenience sake, I prefer the
good old batch file. Just for blanks and giggles, you may be wondering where the phrase consuming came
about? In this regard, it simply means you're presenting the Web Service to the "consume"r. Get it?

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 by creating a new .bat file named makelib.bat and typing in:

[C#]

csc /t:library /out:bin\GetSuppliers.dll bin\GetSuppliers.cs


/reference:System.dll,System.Data.dll,System.Web.dll,System.Web.Services.dll,System.XML.dll
/optimize

[VB]

vbc /t:library /out:bin\GetSuppliers.dll bin\GetSuppliers.vb


/reference:System.dll,System.Data.dll,System.Web.dll,System.Web.Services.dll,System.XML.dll
/optimize

pause
All this says is to 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 used in our Web service. Finally, we /optimize our dll
to produce smaller, faster, more efficient output.
Step 4 - Put it All Together

Now copy and paste the code below to an .aspx .NET page and name it. The code below is the page that will
access the assembly/DLL in your bin folder, and with all the web server controls to pass the appropriate
parameters to the Web Service. Go ahead and run it (ex. http://localhost/websrvce.aspx), and I'll break
it down for you in a sec.

The Code:
<%@ 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>

Potrebbero piacerti anche