The web uses the. NET framework to provide existing code as a WEB service
Building a distributed application using. NET
Steve Kirk and Priya Dhawan
Microsoft Developer Network
Summary: This article describes the data transformations that are made using ASP.net to provide the existing Microsoft Visual Basic 6.0 code as a Web service. Beta version 1 for the Microsoft. NET SDK and Microsoft Visual Studio.NET.
Directory
Brief introduction
Data types provided by existing code
ADO 2x Command Object
ADO 2x Recordset Object
Stream Object
XMLDOM objects
XML string
Summarize
Brief introduction
The. NET Framework simplifies the task of providing. NET code as a Web service. The asp.net Web services and asp.net Web service clients in the. NET Framework Developer's Guide are described in this article. One reason for this simplification is that the. NET Framework provides a series of rules for converting complex. NET data types to XML (serialization) and reverse conversion (deserialization).
Existing code that is written as a class before. NET passes data through language-specific data types or COM objects, so you cannot rely on the same standard rules to serialize these data types to XML. This article describes the data transformations required to provide existing Microsoft®visual Basic 6.0 code as a asp.net Web service.
The data transformation issues discussed in this article are not the only issues that need to be considered when evaluating whether existing code is appropriate for WEB service delivery. Other factors that should be considered include object and state models, the size of the data returned, how to indicate success, how to return an error message, the security model (including access control, authentication, and encryption), the execution model (synchronous or asynchronous), how to distribute the code, and the transaction model (COM + transaction or declarative transaction), and so on. These issues will be discussed in an upcoming architecture topic (English) article.
Data types provided by existing code
The conversion of all data types passed by existing code will be a considerable project, so this article only describes some of the most commonly used data types, as well as XML (as a string), because if you use XML to extend existing code, then XML can cover almost all other data types. This article discusses the conversion methods for the following data types:
ADO 2x Command Object
Ado2x Recordset Object
Stream Object
XMLDOM objects
Xml
ADO 2x Command Object
Existing code that accesses the database directly often provides Command objects for Microsoft ActiveX® Data Objects (ADO). Although you cannot pass a Command object between application tiers running in different processes, you can pass the object within the same process. For Single-line data entities, it is more efficient to return data through the Command object's output parameters than to return data through an ADO recordset. Therefore, the ADO Command object is useful for returning single-line entity data.
Reading data
The existing code in the following example returns an ADO Command object that contains data as an output parameter. The Parameters collection of the Command object is converted to XML and returned to the customer of the Web service:
' Existing code returns the ADO Command object
CMD = CType (EC. Example1 (), Adodb.command)
' Use XmlTextWriter and StringWriter to convert the Parameters collection of Command objects
' Initialize StringWriter and XmlWriter to return an XML string
Strwriter = New StringWriter ()
XmlWriter = New XmlTextWriter (strwriter)
' Looping within the Parameters collection, writing names and values
For i = 0 to Cmd.parameters.count-1
XmlWriter. WriteElementString (Cmd.parameters (i). Name.substring (1), _
Cmd.parameters (i). value.tostring)
Next
' Returns the XML as a string
Example1 = strWriter.GetStringBuilder.ToString ()
Write Data
Passing data as a parameter of a Command object is a very effective method of data transfer. It can also be extended and provides some type-checking functionality. Unfortunately, the data generated by the Command object cannot be passed to existing code because of a flaw in Beta 1. This problem was solved in Beta 2. The solution for Beta 1 is to extend the existing VB 6 code to accept XML.
Ado2x Recordset Object
An ADO 2x disconnected recordset is typically used to pass data between tiers of multi-tier applications. The data can be single, multiline, or layered rows.
Reading data
In this example, the existing code returns an ADO Recordset object that contains hierarchical row data that is converted to XML and then returned by the WEB service:
' Existing code returns the recordset
RS = CType (EC. Example3 (), ADODB. Recordset)
' Instantiate a stream that receives the recordset data
Stream = New ADODB. Stream ()
' Writes the XML representation of a recordset to the stream
Rs. Save (Stream, ADODB. Persistformatenum.adpersistxml)
' Returns XML as a string from the stream
Example3 = Stream.readtext
Write Data
In the following example, the ADO Recordset object is populated with XML that represents the hierarchical row data, and the object is passed to the existing code:
' Instantiate a Recordset object
RS = New ADODB. Recordset ()
' Instantiate a Stream object
Stream = New ADODB. Stream ()
' Open the Stream object
Stream.open ()
' Write XML as an inflow
Stream.writetext (Rsxml)
' Position the pointer at the beginning of the stream
stream.position = 0
' Open a recordset with XML data from the stream
Rs. Open (Stream)
' Passing recordsets to existing code
EC. Example4 (RS)
Stream Object
Streams provide an efficient way to pass data between the local layers of an application. It is the primary way to read XML from Microsoft SQL server™2000.
Reading data
In the following example, the existing code returns an XML stream that represents a hierarchical row of data that is read as a string and returned by the WEB service:
Dim Stream as ADODB. Stream
Stream = CType (EC. Example5 (), ADODB.stream)
Example5 = Stream.readtext
XMLDOM objects
The Xmldom object is a good way to pass data between the local layers of a multi-tier application. It provides interface scalability, type checking, and schema validation capabilities.
Reading data
In the following example, the existing code returns an XML Document Object Model (XMLDOM) that is converted to an XML string and returned by the WEB service:
Dim Doc as MSXML2. DOMDocument
' Existing code returns the Xmldom object
Doc = CType (EC. Example6 (), MSXML2. DOMDocument)
' Return XML from DOM object
Example6 = Doc.xml
Write Data
In the following example, the Xmldom object is populated with XML that represents the hierarchical row data, and the object is passed to the existing code:
Dim Doc as MSXML2. DOMDocument
' Instantiate a XMLDOMDocument object
Doc = New MSXML2. DOMDocument ()
' Loading XML into the DOM
Doc.loadxml (Orderxml)
' Pass DOM to existing code
EC. Example7 (DOC)
XML string
XML is a simple way to pass data between layers and layers. It also pushes the XML conversion process of the data to the "existing code" side of the COM interop boundary, which may be more efficient than converting the data to XML at the COM interop boundary, depending on the interface.
Reading data
In the following example, the existing code returns a string containing the XML data, which is then passed to the customer by the Web service:
EC = New Excode.exclass ()
' Pass the XML string directly from the existing code to the customer
Example8 = EC. Example8 ()
Write Data
In the following example, the XML representation of the hierarchical row data is passed to the existing code as a string:
EC = New Excode.exclass ()
' Pass the XML string directly from the customer to the existing code
EC. Example9 (Orderxml)
Summarize
This article and the accompanying examples introduce information about data transformations. With Data transformation, you can use ASP.net to provide existing code as a WEB service. This article discusses some common interface objects, including XML strings, which can cover most data if you extend existing code using the appropriate interface.
The performance of these solutions varies and is affected by the size of the data being passed. In later articles in this series, we will compare these implementations.
When evaluating the suitability of existing code as a WEB service, interfaces are but one of many factors that should be considered. Other factors to consider include security (including authorization, authentication and encryption), transaction model, state model, ways to return errors and results, and whether the code is synchronized or asynchronous, and so on.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.