Writing Web Services That Use DataSet Objects

Writing Web Services That Use DataSet Objects

When you define a Web Service to return a DataSet , you actually get a DataSet and not a proxy DataSet . You would agree that a DataSet is a powerful object. The listing in the subsection below demonstrates a Web Service that returns the CommissionsData object as a DataSet , which in a database application you might find more useful than a user -defined object. (And, as my smart friend Eric Cotter said, just think of a DataSet as a convenient way to store data, meaning that you can use a DataSet even with no database, as I will demonstrate next .)

Serializing an Object to an XML DataSet

Listing 14.7 provides a revised version of the GetData method from Listing 14.4. This version returns a complex type, an ADO.NET DataSet . The code also shows how to serialize objects into an XML DataSet .

Listing 14.7 Returning a DataSet from an XML Web Service
 1:  Imports System.Web.Services 2:  Imports Commission = CommissionsData.CommissionsData 3:  Imports System.IO 4:  Imports System.Xml.Serialization 5: 6: 7:  <WebService(Namespace:="http://tempuri.org/")> _ 8:  Public Class Service1 9:    Inherits System.Web.Services.WebService 10: 11: [ Web Services Designer generated code ] 12: 13:   <WebMethod()> _ 14:   Public Function GetData() As DataSet 15:     Dim Data As Commission = New Commission() 16:     Data.SystemCode = "F" 17:     Data.RecordType = "30" 18:     Data.ClearingSettlingFirmNumber = "0" 19:     Data.FundProcessingDate = DateTime.Now 20:     Data.CommissionType = "01" 21:     Data.DebitCreditIndicator = "1" 22:     Data.DebitReasonCode = "1" 23:     Data.SettlementIndicator = "1" 24:     Data.RecordDate = DateTime.Now 25: 26:     Dim Serializer As XmlSerializer = _ 27:       New XmlSerializer(Data.GetType()) 28:     Dim Stream As Stream = New MemoryStream() 29:     Serializer.Serialize(Stream, Data) 30:     Stream.Position = 0 31: 32:     Dim DataSet As DataSet = New DataSet() 33:     DataSet.ReadXml(Stream) 34: 35:     Return DataSet 36:   End Function 37: 38: End Class 

Let's take a moment to explore the code in Listing 14.7. If you have been skipping around in this book, this is a good place to see how a great framework comes together. In this one code example you will find streams, XML, ADO.NET DataSet objects, XML Web Services, serialization, and Reflection.

NOTE

You may have been programming in other languages, including VB6, prior to VB .NET. However, if you have previously programmed only in VB6, some parts of Listing 14.7 might surprise you. This listing demonstrates some of the capabilities that have been available in other languages like C++ and Delphi's Object Pascal, illustrating why some programmers have a bit of contempt for VB6. VB .NET provides us with the best of both worlds : a powerfully expressive yet easy-to-use language.

The Web method GetData serializes an object into an XML DataSet . The implication is that you can turn ordinary objects into DataSet objects with some very basic code. The Imports statements in lines 3 and 4 import the namespaces for streams ( System.IO ) and an XmlSerializer object ( System.Xml.Serialization ). The code in lines 15 through 24 simply provide some data for the serialization process to bite into. The real action begins in line 26.

Lines 26 and 27 declare an XmlSerializer object. We provide the type information of the kind of object we want to serialize. This can include classes, DataSet objects, or just about anything marked with the SerializableAttribute . Line 28 creates a new MemoryStream object. Polymorphism supports assigning a MemoryStream object (child class) to a Stream object (parent class). We could substitute FileStream for MemoryStream in line 28, and the same code would serialize the object to an XML file. (Try it as an exercise.) Line 29 serializes the instance of the CommissionsData object declared in line 15 into the MemoryStream object. Line 30 resets the stream position to the start position, and lines 32 and 33 create a DataSet object and read the XML from the MemoryStream object (lines 32 and 33, respectively).

When the code is finished, the DataSet contains an unnamed DataTable with one row. The columns are the CommissionsData property names , and each row of data contains the property values. In this example there is only one row of data. At this pointline 35the DataSet can be returned from the Web method. (What you don't see is that XML serialization is used to get the DataSet across the wire as XML text.)

Looking at the Proxy Class for Web Methods That Return a DataSet

We can generate the Reference.vb proxy class by using the Add Web Reference dialog or the wsdl.exe utility. If you just want to generate the proxy class (shown in Listing 14.8), open the Visual Studio .NET Command Prompt (Figure 14.4). The Visual Studio .NET Command Prompt adds environment variables , making it easier to run command-line programs that ship with .NET like wsdl.exe . To generate the proxy class using wsdl.exe , follow these steps.

  1. After opening a command prompt by using the Visual Studio .NET Command Prompt menu item (Figure 14.4), change directories to the folder to which you want the output proxy file to be written.

  2. Enter wsdl http: //host/service.asmx /language:VB , where host is the name of the computer hosting the Web Service (on your PC, this is localhost ) and service.asmx is the name of the actual .asmx file containing the Web Service. The switch will write the proxy file in the same directory in Visual Basic .NET.

Figure 14.4. Open the Visual Studio .NET Command Prompt to run console utilities like wsdl.exe .

graphics/14fig04.gif

By default the proxy file is written in C# and the output filename will be the same name as the .asmx file but with a .vb extension. For example, if your .asmx file is named Service1.asmx , your WSDL-generated proxy file will be named Service1.vb . Listing 14.8 shows the source code for a proxy file that has a Web method that returns a DataSet .

Listing 14.8 A Proxy File That Has a Web Method That Returns a DataSet
 '----------------------------------------------------------------------' <autogenerated> '   This code was generated by a tool. '   Runtime Version: 1.0.3705.288 ' '   Changes to this file may cause incorrect behavior '   and will be lost if the code is regenerated. ' </autogenerated> '---------------------------------------------------------------------- Option Strict Off Option Explicit On Imports System Imports System.ComponentModel Imports System.Diagnostics Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Xml.Serialization ' 'This source code was auto-generated by 'Microsoft.VSDesigner, Version 1.0.3705.288. ' Namespace localhost   '<remarks/>   <System.Diagnostics.DebuggerStepThroughAttribute(), _   System.ComponentModel.DesignerCategoryAttribute("code"), _     System.Web.Services.WebServiceBindingAttribute( _     Name:="Service1Soap", [Namespace]:="http://tempuri.org/")> _   Public Class Service1     Inherits System.Web.Services.Protocols.SoapHttpClientProtocol     '<remarks/>     Public Sub New()       MyBase.New       Me.Url = "http://localhost/" + _                "CommissionsDataSetWebService/Service1.asmx"     End Sub     '<remarks/>     <System.Web.Services.Protocols.SoapDocumentMethodAttribute( _       "http://tempuri.org/GetData",       RequestNamespace:="http://tempuri.org/",       ResponseNamespace:="http://tempuri.org/",       Use:=System.Web.Services.Description.SoapBindingUse.Literal,       ParameterStyle:=System.Web.Services.Protocols. _       SoapParameterStyle.Wrapped)> _  Public Function GetData() As System.Data.DataSet  Dim results() As Object = Me.Invoke("GetData", New Object(-1) {})  Return CType(results(0),System.Data.DataSet)  End Function     '<remarks/>     Public Function BeginGetData( _       ByVal callback As System.AsyncCallback, _       ByVal asyncState As Object) As System.IAsyncResult       Return Me.BeginInvoke("GetData",         New Object(-1) {}, callback, asyncState)     End Function     '<remarks/>     Public Function EndGetData( _  ByVal asyncResult As System.IAsyncResult) As System.Data.DataSet  Dim results() As Object = Me.EndInvoke(asyncResult)  Return CType(results(0),System.Data.DataSet)  End Function   End Class End Namespace 

As we have seen already, there is a lot of metadata associated with Web Services. We can ignore that information for the purposes of our discussion and simply focus on the lines of text set in bold.

Recall that when we returned a complex data type, we got a proxy class for the custom data type (see Listing 14.4). In Listing 14.8 you will note that no such proxy class is defined for the System.Data.DataSet object returned by the Web Service. Instead when the proxy class is generated, it is known that you have the .NET Framework installed (how else did you get the wsdl.exe utility?), and the return type is cast to the real type, the System.Data.DataSet class. As a result, what we get from Web Services that return XML DataSet objects is a fat DataSet with methods and everything else.



Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net