An ASP.NET Web Service enables you to access object properties and methods across any network, including the Internet. You can integrate a Web Service into a custom control. When you use the control, it can use one or more Web Services behind the scenes to communicate across the network. NOTE To learn more details about Web Services, see the two chapters in Part VI of this book, "Building ASP.NET Web Services." In the following sections, you create a custom control that displays a list of "featured products." The control displays a list of product names and prices. The FeaturedProduct control communicates with a Web Service to retrieve the list of products. The Web Service has a single method called GetFeatured that returns the records from a database of products. This control could be used, for example, in an affiliate program. If a company sells computer games , the company could convince affiliate Web sites to use the FeaturedProduct control to promote the company's games across the Internet. Creating the Featured Products Web ServiceBefore you can create the FeaturedProduct custom control, you need to create the FeaturedService Web Service; the complete code for it is contained in Listing 29.15. Listing 29.15 FeaturedService.asmx [View full width] <%@ WebService Language="VB" Class="FeaturedService" %> Imports System Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient <WebService( Namespace:="http://yourdomain.com/webservices" )> _ Public Class FeaturedService : Inherits WebService <WebMethod(CacheDuration:=60)> _ Public Function GetFeatured() As DataSet Dim dstDataSet As DataSet Dim conNorthwind As SqlConnection Dim dadProducts As SqlDataAdapter conNorthwind = New SqlConnection( "Server=Localhost;UID=sa;PWD=secret;Database=Northwind" ) dadProducts = New SqlDataAdapter( "Select * From Products", conNorthwind ) dstDataSet = New DataSet() dadProducts.Fill( dstDataSet, "Products" ) Return dstDataSet End Function End Class The C# version of this code can be found on the CD-ROM. The Web Service in Listing 29.15 has a single method named GetFeatured that returns a DataSet that represents a list of products. Notice that the CacheDuration property of the WebMethod attribute is set to cache the output of the method for 60 seconds. Before you can use the FeaturedService Web Service in a custom control, you must first create a proxy class for the service. To do so, execute the following statement from the command line: Wsdl /l:vb http://www.YourSite.com/FeaturedService.asmx?WSDL This statement generates a new file named FeaturedService.vb that contains the source code for a proxy class for the FeaturedService Web Service. CAUTION Except for the purposes of testing a Web Service locally, it is generally not a good idea to pass a URL that contains localhost to the Wsdl tool. If you generate a proxy class by using localhost , the proxy class will work only on the current machine. Before you can use the proxy class, you must compile the FeatureService.vb file. To do so, execute the following statement from the command line: [View full width]
This statement references the System , System.Web , System.XML , and System.Data assemblies when compiling the proxy class. After you create the compiled proxy class, the last step is to copy the class, the file named FeaturedService.dll , into the application's /bin directory. Creating the Featured Products Custom ControlNow that you have the FeaturedService Web Service, you can create a control that interacts with the service. The complete code for the control is contained in Listing 29.16. Listing 29.16 FeaturedControl.vbImports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Data Namespace myControls Public Class FeaturedControl Inherits WebControl Private Function GetDataSet() As DataSet Dim objService As FeaturedService objService = New FeaturedService Return objService.GetFeatured() End Function Protected Overrides Sub RenderContents( objTextWriter As HtmlTextWriter ) Dim dstDataSet As DataSet Dim dtblDataTable As DataTable Dim drowRow As DataRow dstDataSet = Context.Cache( "FeaturedProducts" ) If dstDataSet Is Nothing Then dstDataSet = GetDataSet() Context.Cache.Insert( "FeaturedProducts", dstDataSet, Nothing, _ DateTime.Now.AddMinutes(5), TimeSpan.Zero ) End If dtblDataTable = dstDataSet.Tables( "Products" ) For each drowRow in dtblDataTable.Rows objTextWriter.Write( "<li>" ) objTextWriter.Write( drowRow( "ProductName" ) ) objTextWriter.Write( " - " ) objTextWriter.Write( String.Format( "{0:c}", drowRow( "UnitPrice" ) ) ) objTextWRiter.WriteLine( "<p>" ) Next End Sub End Class End Namespace The C# version of this code can be found on the CD-ROM. For performance and reliability reasons, the control in Listing 29.16 does not access the Web Service every time it executes. Instead, it caches in memory the data it retrieves from the Web Service. The cache is automatically cleared every 5 minutes, and the control grabs the data from the Web Service again. NOTE To learn more information about data caching, see Chapter 17, "Caching ASP.NET Applications." Within the RenderContents method, the control attempts to retrieve the list of featured products from the cache. If the products are not present in the cache, the list of products is retrieved with the getDataSet function. The getDataSet function calls the Web Service's getFeatured method and returns the DataSet of products retrieved from the Web Service. The remainder of the RenderContents method is devoted to displaying each item in the list of products. A FOR...EACH loop steps through each row in the Products data table. The contents of the ProductName and UnitPrice columns are output with the HtmlTextWriter . To compile the control in Listing 29.16, you need to execute the following statement from the command line (within the same directory as the source for the control): [View full width]
Notice that you must include several references to assemblies. When you're compiling the control, to use the DataSet in it, you must refer to the System.Data assembly. You also must refer to the System.Web.Services , System.XML , and FeaturedService assemblies to use the FeaturedService Web Service. Displaying the Featured Products ControlAfter you compile and copy the FeaturedControl control to the application's /bin directory, you can use it in an ASP.NET page. The page in Listing 29.17 uses the control to list the featured products from the Web Service (see Figure 29.3). Listing 29.17 DisplayFeatured.aspx<%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="FeaturedControl"%> <html> <head><title>DisplayFeatured.aspx</title></head> <body> <h2>Featured Products:</h2> <myControls:FeaturedControl Font-Name="Arial" Font-Size="14pt" ForeColor="Blue" Runat="Server" /> </body> </html> The C# version of this code can be found on the CD-ROM. Figure 29.3. Output of the FeaturedControl .
Notice that you can format the output of the control by setting the value of the Font-Name , Font-Size , and ForeColor properties. Because the FeaturedControl control inherits from the WebControl class, you can use all the formatting properties of the WebControl class when displaying the control. |