The System.Web.Services Namespace

本文介绍了.NET平台下XML Web Services技术。它能通过Web执行对象方法调用,以XML格式传输数据,可跨平台使用。文中还给出创建简单Web Service的示例,包括在Visual Studio.NET中创建项目、添加方法、测试服务等步骤,展示了其强大的编程和分布式应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值