Use the .NET framework to provide existing code as a web service

zhaozj2021-02-08  206

Use the .NET framework to provide existing code as a web service

Built distributed applications using .NET

Steve Kirk and Priya Dhawan

Microsoft Developer Network

Summary: This article describes data conversions that use ASP.NET to provide existing Microsoft Visual Basic 6.0 code as a web service. Applies to the Beta 1 version of Microsoft .NET SDK and Microsoft Visual Studio.net.

table of Contents

Introduction Data types provided by existing code

ADO 2X Command Object ADO 2X RecordSet Object Stream Objects XMLDOM Objects XML String Summary

Introduction The .NET framework simplifies the task that will be supplied by the .NET code as a Web service. The ASP.NET Web Services in the .NET Framework Developer's Guide The ASP.NET Web Server (English) describes this. The reason why it is possible to achieve this, one for the reason is that the .NET framework provides a range of rules for converting complex .NET data types to XML (serialization) and reverse conversion (reverse sequencing). The existing code written as .NET passes the data by a language-specific data type or COM object, so it cannot be serially serially served as XML depending on the same standard rule. This article describes the data conversion required to provide an existing Microsoft® Visual Basic 6.0 code as an ASP.NET Web service. Assess whether the existing code is suitable as a Web service, the data conversion problem discussed in this article is not the only problem that needs to be considered. Other factors that should be considered include objects and state models, returned data size, how to indicate that it has been successful, how to return to error messages, security models (including access control, authentication, and encryption), execute models (synchronous or asynchronous), how to distribute code , As well as transaction models (COM transactions or statements), and so on. These issues will discuss in the upcoming architectural topic (English) article. The data type provided by the existing code discussion of all data types of existing code passes will be a considerable project, so this article only introduces some of the most common data types, and XML (as a string), because if XML pair is used Existing code is expanded, then XML can cover almost all other data types. This article discusses the following data type conversion method:

ADO 2X Command Objects ADO2X Recordset Objects Stream Objects XMLDOM Objects XML ADO 2x Command Objects Direct access Database Existing code often provides a Command object of Microsoft ActiveX® Data Objects (ADO). Although a Command object cannot be passed between applications running in different processes, the object can be passed within the same process. For a single line of data entities, the output parameter of the Command object returns a higher data efficiency than returning through the ADO recordset. Therefore, ADO Command object is useful for returning a single line entity data. Reading 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 web service: 'Existing code returns an ADO Command object

CMD = ctype (ec.example1 (), adoDb.command)

'Use XMLTextWriter and StringWriter to convert the Parameters collection of the Command object

'Initializing StringWriter and XMLWRITER to return to XML string strwriter = new stringWriter ()

Xmlwriter = New XMLTextWriter (Strwriter)

'Circulation in parameters collection, write name and value

For i = 0 to cmd.parameters.count - 1

Xmlwriter.writeElementString (cmd.Parameters (i) .name.substring (1), _

Cmd.parameters (i) .value.toString)

NEXT

'Return XML as a string

Example1 = strwriter.getstringbuilder.tostring () Write data Transfer data as a parameter of the Command object is a very efficient data transfer method. It can also expand and provide some type of check function. Unfortunately, due to the defects in Beta 1, the data generated by the Command object cannot be passed to existing code. This problem is solved in Beta 2. The solution to Beta 1 is to extend the existing VB 6 code, accept XML. The ADO2X RecordSet object ADO 2x disconnected record set is usually used to pass data between the layers of the multi-layer application. The data can be a single line, multi-line or hierarchy. In this example, the existing code returns an ADO RecordSet object, which contains the hierarchical line data to be converted to XML, then return by the web service: 'Existing code return record set

RS = ctype (ec.example3 (), adoDb.recordset)

'Instantiate a stream of receiving records data

Stream = new adodb.stream ()

'Write the XML of the record

Rs.save (stream, adodb.persistFormatenum.adpersistxml)

'Return XML as a string

Example3 = stream.readText Write Data The following examples, using XML that represents the hierarchical row data, the object will be passed to the existing code: 'Instantiate a recordset object

RS = new adodb.recordset ()

'Instantiate a stream object

Stream = new adodb.stream ()

'Open the flow object

Stream.open ()

'Write XML into the stream

Stream.writeText (RSXML)

'Locate the pointer to the beginning of the stream

Stream.position = 0

'Opening the XML data in the stream

Rs.open (stream)

'Pass the record set to the existing code

The EC.Example4 (RS) STREAM object stream provides an effective way to pass data between the local layers of the application. It is the main method of reading XML from Microsoft SQL ServerTM 2000. In the following example, existing code returns an XML stream representing the hierarchical line data, which 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 object XMLDOM object is a good way to pass data between the local layers of the multi-layer application. It provides interface scalability, type check, and architectural authentication. Read the data below, existing code returns an XML Document Object Model (XMLDOM), which is converted to an XML string and returned by the web service: DIM DOC AS MSXML2.DOMDocument 'existing code Return XMLDOM object

DOC = ctype (Ec.example6 (), msxml2.domdocument)

'Return to XML from DOM

Example6 = doc.xml Write Data The following examples, use XMLs that represent hierarchical row data, passed the object to existing code: DIM DOC AS MSXML2.DOMDOCUMENT

'Instantiate an XMLDOMDocument object

DOC = new msxml2.domdocument ()

'Load XML into DOM

Doc.loadxml (OrderXML)

'Pass the 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" end of the COM interoperability boundary. This may be more effective than the interface to XML than the COM interoperability boundary. Read the data below, existing code returns a string containing XML data, then the string is passed by the web service to the customer: EC = New EXCODE.EXCLASS ()

'Pass the XML string directly from the existing code to the customer

Example8 = ec.example8 () Write data The following example, the XML representation of the hierarchical line data is passed as a string to the existing code: EC = New EXCODE.EXCLASS ()

'Pass XML strings directly from customers to existing code

Ec.example9 (OrderXML)

Summary This article and the included examples describe information about data conversion. With data conversion, you can use ASP.NET to provide existing code as a web service. This article discusses some common interface objects, including XML strings, if existing code is expanded using the appropriate interface, it can cover most of the data. These solutions have different performance and are affected by the size of the data transmitted. In this series, we will compare these implementations. When assessing whether existing code is suitable as a web service, the interface is only one of the many factors that should be considered. Other factors that should be considered include security (including authorization, authentication, and encryption), transaction model, status model, return errors, and results, and code is synchronized or asynchronous, and so on.

转载请注明原文地址:https://www.9cbs.com/read-3028.html

New Post(0)