Two questions for gurus about using strongly typed DataSet in Web Services.

D

Don C

1. Is there a 2nd way to declare a strongly typed DataSet (e.g.
CustomerDataSet) in WSDL instead of using the WSDL <import
namespace="CustomerDSnamespace" location="url to CustomerDS.xsd"/>?

Apparently, if I include CustomerDataSet schema definition in WSDL types, it
will not be recognized as a DataSet by .NET WSDL compiler when a Web
Reference is created. I have to use the WSDL import statement. What do I
need to do to make it recognize the CustomerDataSet definition in the WSDL
types. The CustomerDataSet has the correct definition (ie isDataSet=true)
and does work with the WSDL import.

2. How to send a strongly typed DataSet over SOAP without the XSD?

I found that when I am sending a strongly typed DataSet (e.g.
CustomerDataSet) instance over SOAP, I still need to send its XSD with the
CustomerDataSet every time. Otherwise, .NET will not parse attribute data
type right even though, the XSD is available as part of the strongly typed
DataSet (ie CustomerDataSet). This defeats the purpose of using the strongly
typed DataSet between Web service server and client. Is there a way not to
send the XSD?

Thanks,

Don
 
G

Guest

As I am not sure you are asking the proper questions, here is a run through
of setting up a web service and consuming it. If you are using Remoting, the
idea is roughly the same, but you will have to set up the serialization
yourself.

1. Create two web projects (I am using VB.NET right now; not out of desire
as much as it is what my current project is coded in)
2. In Project 1, use the following code to produce a strongly typed DataSet
called PubsDS (uses the pubs database)

Dim connString As String = "server=(local);Database=Pubs;UID=sa;PWD=sa;"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand("select * from stores where stor_id = 6380", conn)

Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet("Pubs")

da.TableMappings.Add("Table", "Stores")

Try
conn.Open()
da.Fill(ds)
Catch ex As Exception
'Nothing needed here
Finally
conn.Dispose()
End Try

ds.WriteXmlSchema("C:\PubsDS.xsd")

3. Create a new PubsDS.xsd in project 1 and copy the schema from the PubsDS
you created on the C drive. It will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="PubsDS" targetNamespace="http://tempuri.org/PubsDS.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/PubsDS.xsd"
xmlns:mstns="http://tempuri.org/PubsDS.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="PubsDS" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Stores">
<xs:complexType>
<xs:sequence>
<xs:element name="stor_id" type="xs:string" minOccurs="0" />
<xs:element name="stor_name" type="xs:string" minOccurs="0" />
<xs:element name="stor_address" type="xs:string" minOccurs="0"
/>
<xs:element name="city" type="xs:string" minOccurs="0" />
<xs:element name="state" type="xs:string" minOccurs="0" />
<xs:element name="zip" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

4. Write a service to pull stores by store ID

<WebMethod()> _
Public Function GetStoreByID(ByVal storeID As Int32) As PubsDS
Dim connString As String =
"server=(local);Database=Pubs;UID=sa;PWD=sa;"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand("select * from stores where stor_id = " & _
storeID.ToString(), conn)storeID.ToString(), conn)

Dim da As New SqlDataAdapter(cmd)
Dim ds As New PubsDS

da.TableMappings.Add("Table", "Stores")

Try
conn.Open()
da.Fill(ds)
Catch ex As Exception

Finally
conn.Dispose()
End Try

Return ds
End Function

5. Go to Project 2 and create a web reference called StoreWebService to
http://localhost/Project1/StoreService.asmx?WSDL

6. Add the following code to a web page with two labels

Dim service As New StoreWebService.StoreService
Dim ds As StoreWebService.PubsDS = service.GetStoreByID(6380)

lblStoreID.Text = ds.Stores(0).stor_id.ToString()
lblStoreName.Text = ds.Stores(0).stor_name

Serialization of the DataSet is done automatically and the strongly typed
DataSet is added to your project automatically. You do not have to do
anything with the XSD in the SOAP call.

If you are circumventing the automagic crud, you will have to secure the
plumbing yourself. Either way, unless you want to reflect everything, you
have to have proper dataset definitions on both sides of the service to
properly serialize objects. You can emit an object at Runtime, but I would
only go this route if absolutely necessary.

Hope this helps.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top