The System.Web.Services Namespace

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

The System.Web.Services Namespace

XML Web Services provide one of the newest and most exciting technologies

for development, as far as we're concerned. Although the .NET platform, and

Visual Studio .NET in particular, makes it easy to get started creating XML

Web Services, Web Services have been available for some time on the

Microsoft platform, thanks to the SOAP Toolkit. (Stop by

http://msdn.microsoft.com/xml for more information on the SOAP Toolkit and

how it can enable you to consume Web Services in non-.NET client

applications.)

XML Web Services make it possible to execute method calls provided by

objects via the Web. Distributed components have been available for years,

in the form of ActiveX components accessed via DCOM, but Web Services make

components available over HTTP using SOAP as the underlying messaging

protocol. DCOM is a binary standard that doesn't transmit well through

firewalls or proxy servers. In addition, DCOM is only supported on

platforms running COM, which has basically limited COM/DCOM to Windows

platforms.

With Web Services, however, the landscape has changed. The call and

response to a Web Service are in XML format and are generally transported

over HTTP. This means that the component can be called using simple text

and by any client on any platform. The result comes back in XML format,

meaning that any client that understands XML can consume the results.

Finally, you can code your components using your favorite Microsoft tools

and allow those components to be used by people running on Unix or an AS/400

梠r any other platform that can make HTTP calls and consume XML. Realize

that you can consume a Web Service from a Windows application, a Web

application, or any other client you can imagine, including the growing set

of wireless devices, such as PDAs and Web-enabled phones. This opens up a

new world of interoperability and means you can distribute your application

on servers around the world and tie them together using just HTTP.

Creating XML Web Services is fairly straightforward, as you will see in the

next section. In Part IV of this book, you will learn how to create more

fully-functional Web Services. In this chapter, you will just learn to

create a simple Web Service, and you'll see a simple way to access that Web

Service.

NOTE

We use the term Web Services to refer to the technology that allows you to

communicate with objects using specially formatted XML streams, usually

over HTTP. The term we ought to be using is XML Web Services, because there

are other emerging technologies that provide services over the Web. This

terminology becomes distracting, however, and we'll simply refer to it as

Web Services throughout the book.

A System.Web.Services Example

In order to create a new Web Service, start by creating a new project in

Visual Studio .NET and selecting the ASP.NET Web Service template. Name the

new project TestWebService (accepting the default location,

http://localhost . Select Service1.asmx in the Solution Explorer window,

and select the View, Code menu item to display the associated class. As you

can see, Visual Studio .NET has created the shell of the service for you,

including commented lines of code showing how to expose public Web methods.

Add the following procedure to the sample class:

<WebMethod()> _

Public Function Hello( _

ByVal Name As String) As String

Return "Hello, " & Name

End Function

The one public function here, Hello, simply returns a greeting to the name

you pass in as a parameter.

NOTE

What's the <WebMethod()> text you see in the procedure declaration for?

This procedure attribute indicates to the compiler that the procedure

should be serialized using the SOAP serializer, for use as part of a Web

Service. Without this, you'd simply be creating a public method of a class,

just like normal.

Choose the Build, Build Solution menu item to rebuild the project. (This

step creates a DLL and places it in a location on your Web server within

the project folder.)

It's simple to test the Web Service: Open your Web browser and type in the

following URL, replacing the server name and project name if necessary:

http://localhost/TestWebService/Service1.asmx/

Hello?Name=Paul

The result that will appear in your browser is the XML response from the

Web Service. It will look like this:

<?xml version="1.0" encoding="utf-8" ?>

<string xmlns="http://tempuri.org/ >Hello, Paul</string>

If you're not excited yet, think about this: You called a method in a

component over standard HTTP and retrieved the result, all over the

Internet! (Okay, you're on your local machine, but it could just as easily

have been over the Internet!) This is a very powerful technique, and it

opens up a whole new way of programming and creating distributed

applications.

Normally, you'd call this Web Service from a client application. In Visual

Studio .NET, you can simply add a Web reference to your Web Service. This

is just like adding a reference to a COM component in previous versions of

Visual Studio. After adding the Web reference, you refer to the Web Service

as you would any other component.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航