Consuming Web Service That Exposes a Typed DataSet


Consuming Web Service That Exposes a Typed DataSet

The .NET Compact Framework does not support typed DataSet s. If a Web service uses a typed DataSet , .NET Compact Framework clients will not be able to consume the Web service. Specifically, the client will fail when trying to compile the proxy into the application.

There are work-arounds for consuming a typed DataSet :

  • Modify the XML Web service to use only regular DataSet s.

  • Modify the XML Web Service to expose only regular DataSet s.

  • Create a new XML Web service that consumes the original typed DataSet and converts it to a regular DataSet .

These first two options may seem similar, but they are quite different. The first option suggests that you remove all instances of the typed DataSet from the Web service and use only the regular DataSet while coding. The second option suggests that you modify the Web service to expose (send/receive) only regular DataSet s. This means that the internal code can create and utilize the benefits of a typed DataSet , but the Web method uses only regular DataSet s as parameters and return values.

It is simple to write an XML Web service to expose the regular DataSet but internally use a typed DataSet . Go back to the QuoteableQuotes XML Web service project, and go to the XML Web Service designer. Locate the Quotes table in the Server Explorer. It will be under <Server Name>\SQL Servers\<Server Instance Name >\QuotableQuotes\Tables . Now drag the Quotes table onto the designer. This creates a SqlDataAdapter that will create a typed DataSet for the Quotes table. Rename the SqlDataAdapter to daQuotesDS .

Now click the SqlDataAdapter , go to the Data menu, and select the Generate DataSet . . . menu item. This will bring up the Generate DataSet dialog. Change the name of the new DataSet to QuotesDataSet , and click OK. This will generate a typed DataSet for the Quotes table.

Create a Web method named GetTypeQuotes . The Web method will return a regular DataSet , but internally it will use the QuotesDataSet typed DataSet that was just created. Listing 9.11 contains a method that will convert the typed DataSet to a regular DataSet before returning it to the user .

Listing 9.11
 C# [WebMethod] public DataSet GetTypedQuotes() {   quoteConnection.Open();   try   {     QuotesDataSet quotesDS = new QuotesDataSet();     this.daQuotesDS.Fill(quotesDS);     return quotesDS;   }   finally   {     quoteConnection.Close();   } } VB <WebMethod()> _ Public Function GeTypedQuotes() As DataSet   Me.quoteConnection.Open()   Try     Dim quotesDS As New QuotesDataSet     Me.daQuotesDS.Fill(quotesDS)     Return quotesDS   Finally     Me.quoteConnection.Close()   End Try End Function 

Notice there is no manual conversion going on here. The code relies on polymorphism. Since a typed DataSet is a subclass of the DataSet class, it supports the WriteXml method of the IXmlSerializable interface. The XML written from the method will create the same data, even if it is read by a plain DataSet .

To finish this example the Quotable Quotes DataSet client need to be modified to use the GetTypedQuotes method. Only the button-clicked handler needs to be updated. The handler will now call GetTypeQuotes instead of GetQuotes . Also, the offset parameter of the UpdateQuoteUI method will be 1 instead of 0 in order to compensate for the ID column in the DataRow . Listing 9.12 contains the new code for this method.

Listing 9.12
 C# private void btnQuote_Click(object sender, System.EventArgs e) {   if(null == quotesDataSet)   {     QuoteService qs = new QuoteService();     // Set the proxy's url property to the correct url of the server     quotesDataSet =  qs.GetTypedQuotes();     curQuoteRowNdx = 0;   }   if(quotesDataSet.Tables.Count >= 0)   {     MessageBox.Show("Could not retreive the quotes dataset.");     return;   }   if(curQuoteRowNdx >= quotesDataSet.Tables[0].Rows.Count)     curQuoteRowNdx = 0;   DataRow quote = quotesDataSet.Tables[0].Rows[curQuoteRowNdx];   curQuoteRowNdx++;   UpdateQuoteUI(quote, 1); } VB Private Sub Button1_Click(ByVal sender As System.Object,         ByVal e As System.EventArgs) Handles Button1.Click   If quotesDataSet Is Nothing Then     Dim qs As New QuoteService     ' Set the proxy's url property to the correct url of the server     Me.quotesDataSet = qs.GeTypedQuotes()     Me.curQuoteRowNdx = 0   End If   If quotesDataSet.Tables.Count <= 0 Then     MessageBox.Show("Could not retreive the quotes dataset.")     Return   End If   If Me.curQuoteRowNdx >= quotesDataSet.Tables(0).Rows.Count Then     Me.curQuoteRowNdx = 0   End If   Dim quote As DataRow   quote = quotesDataSet.Tables(0).Rows(curQuoteRowNdx)   curQuoteRowNdx = curQuoteRowNdx + 1   UpdateQuoteUI(quote, 1) End Sub 

Compile and run this example and the application will run as it did before. The server side will be reaping the benefits of using typed DataSet while interoperating with the .Net Compact Framework client.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

Similar book on Amazon

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