Building an ASP.NET Web Service 来源: 时间:2002-09-12 11:34 作者:AMTeam.org Building an ASP.NET Web Service Srinivasa Sivakumar Web services allow you to call a function that exists on another machine on the Internet, using standard protocols. Srinivasa Sivakumar explains how to create Web services using the Microsoft .NET Framework and how to access the Web service via HTTP-Get, HTTP-Post, and SOAP protocols. Web services provide the ability for a server on the Internet to provide an interface to functionality there. Therefore, you can create an API that can be called from anywhere on the Web. There are sites that provide this kind of capability, but usually the technologies they use to implement them are proprietary. There are standards under development to make creating Web services easier—most notably, the Simple Object Access Protocol (SOAP). The new Microsoft .NET Framework has embraced SOAP and other standardized technologies and has provided an interface for creating Web services that's easy to use. In this article, I'll explain how to use ASP.NET to build your own Web service. Architecture of Web services Web services provide a very generic way for applications to work together. In fact, there are bound to be many, many Web services made available over the next few years. For example, a financial services vendor might provide Web services that would allow you to retrieve the current value of a stock, buy and sell stocks, retrieve stock performance data for a given period, maintain a customer stock portfolio, and so forth. Then, if you want to create a site that provides a stock look-up, you can accomplish it by subscribing to the financial services vendor's Web service. You don't have to write the code or maintain the data. You just provide the page for the user to enter his or her information and call the Web service behind the scenes. This provides new site owners with a wealth of capabilities to add to their Web site: stock trading, online world news coverage, and so on—all just by subscribing to the Web services. And the good news is that the technical side of implementing Web services is simple. Now you can build a customer portal in weeks by subscribing to the Web services. Now that you have good understanding of Web services, I'll show you how to build one with the .NET Framework. Using the Visual Basic .NET language Building a Web Service I'm going to build a Web service that has two functions: One converts kilograms into pounds; the other, vice versa. Fire up Notepad (or your favorite text editor) and type in the following code. Then save the file as Conversion.asmx. (The .ASMX extension is used by .NET Framework to indicate an ASP.NET page. You can also use this extension when creating a Web service.) <%@ WebService Language="VB" Class="Conversion" %>
'To create Web Methods, we need to import 'the namespace System.Web.Services
Imports System.Web.Services 'Create the Conversion class Class
Conversion 'Create the KilogramsToPounds Web
Method Public Function <WebMethod()>KilogramsToPounds
_ (ValueInKilograms As Double) As
String KilogramsToPounds =
cstr(ValueInKilograms * 2.2) End
Function 'Create the PoundsToKilograms Web
Method public Function <WebMethod()>PoundsToKilograms
_ (ValueInPounds As Double) As
String PoundsToKilograms =
cstr(ValueInPounds/2.2) End Function End Class
Now start IIS5, create a virtual directory called "WebServices," and copy the Conversion.asmx into the directory. Now it's time to test the Web service. Fire up Internet Explorer and connect to the Web services virtual directory (http://localhost/Webservices/Conversion.asmx). When you access the .ASMX file, the .NET Framework creates an HTML page including the information about the services offered by the .ASMX file that you're accessing. Figure 2 shows the Web service information. This HTML page also includes the code to access the Web services via the HTTP-Get method. Figure 3 shows how to access the KilogramsToPounds and Figure 4 shows how to access the PoundsToKilograms Web services via the HTTP-Get method. (You'll see a reference to something called an SDL Contract at the bottom of Figure 2. Don't worry about that right now. I'll cover SDL a little later in this article.) Accessing a Web service with HTTP-Get and
HTTP-Post Then, in the first form, set the attribute METHOD to Get and ACTION to http://localhost/WebServices/Conversion.asmx/KilogramsToPounds. Then name the first textbox ValueInKilograms. Here's what the form should look like: <FORM Method="Get"
action=http://localhost/WebServices/Conversion.asmx/ KilogramsToPounds>
<TD>Enter the Value in Kilograms:<BR> <INPUT size=50
name=ValueInKilograms> <INPUT type=submit value="Get Pounds">
</TD> </FORM> In the same way, change the second form's ACTION attribute as http://localhost/WebServices/Conversion.asmx/PoundsToKilograms and its textbox name to ValueInPounds. Notice that you're directly accessing the KilogramsToPounds and PoundsToKilograms Web methods in the HTML Form's action attribute. You'll also notice that you've named the textboxes as the name of the parameters of the Web methods. Save the HTML file and load it in the browser. Enter the number 2 in the first text box and click the Get Pounds button. You should see the results shown in Figure 6. Notice that you got the result in XML format. That's what you'll get when you access a Web service. In the same way, you can also access the Web services via the HTTP-Post method by changing the HTML form's action attribute to Post. What is SOAP? To address these problems, Microsoft, IBM, and others jointly submitted a proposal for a common, lightweight protocol called SOAP (Simple Object Access Protocol). SOAP is based on a simple XML-RPC protocol developed by Dave Winer of UserLand. Its implementation is very simple. It describes how procedures can be called and how results can be returned from procedures using XML sent over HTTP. Since SOAP is based on existing, simple technologies, it's highly efficient and easy to create and work with. In addition, since it uses the HTTP protocol, which is typically open in firewalls, it helps avoid some of the security holes introduced by other methods. So how does SOAP apply to Web services? Well, since Web services are nothing more than procedures made available to be called over the Internet, it makes sense that SOAP would be a good way to make those calls and receive the results. SOAP and SDL http://localhost/WebServices/Conversion.asmx?SDL
To access Web services (such as KilogramsToPounds) via SOAP, you must know how to marshal and serialize the SOAP request and response. To do this, you need to build a proxy to manage the marshalling and serializing. This involves two steps. First, you'll build a proxy for your Web service with the help of the WebServiceUtil.Exe file. Then you'll take the code generated by the WebServiceUtil.Exe file and compile it into a COM DLL. Building the proxy Webserviceutil /C:proxy
/pa:http://localhost/WebServices/Conversion.asmx?sdl /l:VB
/n:NameSpaceKgToILB The Proxy.Bat file generates Conversion.vb. When you look at Conversion.vb, you'll see the functions KilogramsToPounds and PoundsToKilograms. In addition, you'll also notice Begin and End functions for both the KilogramsToPounds and PoundsToKilograms functions. These functions are used to call the Web services asynchronously. Now you can use the generated VB code and build the DLL file. (I've also included the DLL.Bat file, which does it for you, in the Download file.) Call the VB.NET command line compiler to compile the Conversion.vb file into a DLL file. vbc /out:KGTOIBL.dll /t:library
/r:System.Xml.Serialization.dll /r:System.Web.Services.dll
Conversion.vb If you look at the Conversion.vb file, you'll see that you're importing the System.Xml.Serialization, System.Web.Services.Protocols, and System.Web.Services namespaces. The first namespace resides in the System.Xml.Serialization.DLL file. The next two namespaces reside in the System.Web.Services.DLL file. That's why you're including these two DLL files with the /r: compiler switch. Deploying the proxy That's it! You're done with DLL file deployment. No, I'm not joking. With the .NET Framework, you don't need to register the DLL with RegSvr32. How does the .NET framework know to create the object when a service asks for the namespace? Microsoft assumes that every Web application has a BIN folder. Under BIN, all of the custom objects, like this DLL file, are stored. Therefore, when you create a custom object for the Web application, .NET runtime searches for the object in the BIN directory and creates the object for you. If it doesn't find the object in the BIN directory, then you get an error. Consuming a Web service with SOAP from
ASP.NET <form runat="server"> <P><FONT
face="Verdana, Arial" size=5><B>Using Web Service Via
SOAP</B></FONT><HR></P> Web Method
KilogramsToPounds: <asp:Label id="lblKGs" runat="server"
/><BR><BR> Web Method PoundsToKilograms: <asp:Label
id="lblLBS" runat="server" /> </form> Let's add
some code in the Page_Load event. <script language="vb"
runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) Dim
objConv as new NameSpaceKgToILB.Conversion lblKGS.Text =
objConv.KilogramsToPounds(1) lblLBS.Text = objConv.PoundsToKilograms(5)
End Sub </script> Now copy the ASP.NET file in to the Web Services virtual directory and call the ASP.NET page from a browser. Figure 8 shows the result. Summary Until next time, happy programming! Download SIVAKUMA.ZIPSidebar: Getting it All up and
Running You can only run Beta 1 of the Framework on Windows 2000. However, you shouldn't install this beta product on any production machines. There's no guarantee that it won't cause other software to break or screw up your configuration in one way or another. Microsoft has, of course, worked to make sure that doesn't happen, but you should always be careful with beta software. Internet Explorer 5.5 may be included with Windows 2000. If you need to download it, you can get it from the Microsoft Web site at http://www.microsoft.com/windows/ie/. The .NET Framework can be downloaded from the Microsoft site, too. Go to http://msdn.microsoft.com/net/. Once you arrive at that page, click on .NET Downloads in the list along the left side of the page. Be aware that this download is very large—111M. If you prefer, you can order it on CD-ROM for a minimal shipping-and-handling fee. You can do that at http://developerstore.com/devstore/product.asp?productID=7597. Sidebar: Further Reading MSDN Download: .NET Framework SDK Technology
Preview MSDN Show: .NET Architecture
Overview Article: Simple Object Access Protocol (SOAP)
Specification, Version 1.1 Article: Discovery of Web Services
(DISCO) |
关键词:
|
相关文章 |