Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
ASPNET Web Service

ASPNET Web Service

Ratings: (0)|Views: 33 |Likes:
Published by api-27116966

More info:

Published by: api-27116966 on Oct 17, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Intel\u00ae Software Network - Build and Consume an ASP.NET Web S... http://www.intel.com/cd/ids/developer/asmo-na/eng/20068.htm?prn=Y
1 of 12
11/7/05 20:28
Abstract
By Patrick Barnes

Before the advent of Web services, other technologies and architectures existed that allowed applications to remotely
call procedures exposed by other applications, typically using the TCP/IP protocol. You have undoubtedly heard of
Microsoft's version of this technology, called Component Object Model* (COM*), and subsequently Distributed COM*

(DCOM*). Common Object Request Broker Architecture* (CORBA*) is a competitor of COM, developed by a consortium
of vendors under the aegis of the Object Management Group.
Although these technologies were a breakthrough in distributed computing and served such ends well for at least five
years, there were many limitations. Concerning DCOM, its two main disadvantages were:
Microsoft-only: DCOM works properly only with binaries written with Microsoft languages and running on Microsoft
platforms.
Not Suitable for the Internet: DCOM was designed to use the TCP/IP protocol over a LAN or WAN. Firewalls
present a serious problem, and performance is often poor and unreliable.

Web services solve many of the problems common to DCOM. A Web service is a unit of code that publicly exposes its
functionality as a URL-addressable resource. As such, any client application that conforms to open Web standards
such as HTTP and the Simple Object Access Protocol (SOAP)\u2014the main messagef o r m a t t i n g protocol, as opposed to
HTTP, which is a messaget r a n s m i s s i o n protocol\u2014can call a Web service, initiate a procedure, and even retrieve data
in the form of an XML document. Thus, Web services take care of the two problems with DCOM and other distributed

programming technologies:
Non-proprietary: Open standards are governed by the World Wide Web Consortium (www.W3C.org*) and other
grassroots organizations. All communication and data formatting protocols are non-proprietary, detaching this form
of remote procedure calling from specific languages and platforms.
Internet-savvy: Web services were designed specifically for use over the Internet via HTTP.

This article shows you how to build and consume a basic ASP .NET Web service. It focuses on the steps required to build the Web service and client application that consumes it. For more theoretical or in-depth information about Web services, see the list of resources at the end of this article.

Assumptions and Requirements

This article assumes no prior knowledge of Web services, Visual Studio .NET*, or Visual Basic .NET*. The Web
Service is built using Visual Studio .NET, although the IDE is not required to build .NET Web services. You could write
the code and compile it using the Visual Basic compiler from the command line. However, this article assumes you are
using Visual Studio .NET and thus makes no accommodations for other situations. Finally, you will need access to the
Northwind* database shipped with Microsoft SQL Server* and the Microsoft Data Engine* (MSDE).

Creating a Web Service Project in Visual Studio .NET
Follow these steps show to create an ASP .NET Web service project in Visual Studio .NET.
1.Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio
.NET.
2.C l i ck New Project.
3.Se l e ct Visual Basic Projects in the Project Typesp a n e .
Intel\u00ae Software Network - Build and Consume an ASP.NET Web S... http://www.intel.com/cd/ids/developer/asmo-na/eng/20068.htm?prn=Y
2 of 12
11/7/05 20:28
4.Select ASP.NET Web Service in the Templatesp a n e .
5. In the Location box, enter the name of the Web server with the name of the project, http://ServerName (or
Localhost)/intel_wsNWProducts. See Figure 1.
Figure 1. Creating the Project in Visual Studio .NET.
6.C l i ck OK. The intel_wsNWProducts project is created and added to a Solution by the same name. Service1.asmx is
open in design view.

7. Click the design surface and press F4 to access the Web service page's Propertiesw i n d o w .
8. Set the Name property of Service1 to ProductsService.
9. In Solution Explorer, right-click the Service1.asmx file, select Rename, and rename the file Products.asmxto

match the service name.
Exposing a Web Method
Functions and subroutines that you want to expose to the public over HTTP are essentially no different than non-Web
service methods. The only difference is the use of the< W e b M e t h o d > attribute, as you will see.
The method you will build for this article's Web service allows anyone to retrieve a list of products from the Northwind
database by product category. The user types in a CategoryID, which is forwarded to the Web service. The Web
service returns the products in that category. (If no category is entered by the user, all products are returned.)
Intel\u00ae Software Network - Build and Consume an ASP.NET Web S... http://www.intel.com/cd/ids/developer/asmo-na/eng/20068.htm?prn=Y
3 of 12
11/7/05 20:28
1. In the Code Editor window right-click on Products.asmx.vb and select View Code.
2. Replace the commented lines with the following shell of a function that returns a DataSet:
Public Function GetProducts(ByVal strCategoryID As Int32) As
DataSet
End Function

In the next step you will add anI m p o r t s statement that lets you use shorthand notation when accessing classes in
the imported namespace. A namespace is simply a logical grouping of related .NET types, such as classes, interfaces,
enums, structures, and so forth. This makes it easier to work with the Base Class Library and also prevents type name

conflicts.
You can always access the type using a fully qualified name like this:S y s t e m . D a t a . S q l C l i e n t . S q l C o n n e c t i o n. The
advantage of an Imports statement, however, is that you can simplify and lighten your code with the use of shorthand
dot notation. Thus, if you import theS y s t e m . D a t a . S q l C l i e n t namespace, in your code you need only type
SqlConnection.
3. At the top of the page of code add the following:
Imports System.Data.SqlClient
Classes in theS q l C l i e n t namespace classes are optimized for use with SQL Server. If you were working with another
database, you could use classes in theS y s t e m . D a t a . O l e D b.
4. Create a SqlConnection object. Add the following to the function (adjust the connection string for your setup):
Dim cn As New
SqlConnection("server=(localhost);uid=sa;pwd=;database=northwind")
5. Create a SqlCommand object. This object executes SQL statements against a SQL Server database. You are
passing in two arguments, the SQL statement that, at this point, returns all of the products, as well as the newly
created Connection object. Add this code:
Dim cmd As New SqlCommand("select * from products",cn)
6. To support filtering the products by CategoryID, add the next few lines of code. The SQL has been built this way so
that if the user doesn't enter a CategoryID, the Web service assumes that he or she wants to view all products:

If strCategoryID <> "" Then
cmd.CommandText &= " where
CategoryID = " & strCategoryID
End If

7. Create a SqlDataAdapter object, which takes the SqlCommand object as an argument and is responsible for filling
the DataSet that is returned by this method. Add this line of code:

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->