SOAP Differences

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 15.  Web Services


Before we finish our basic examination of SOAP and WSDL, a more detailed look at the relationship of SOAP, WSDL, and the XML Schema specification is in order. As mentioned earlier, the SOAP encodings used by .NET remoting differ from those used by Web services and the XML serializer.

To illustrate the differences between the two, we will take the same program and serialize it to disk and use it as a Web service. The program builds a circular list of two customer items. The two programs are found in the SOAP Differences directory.

The first program, Formatter , creates a circular list and then serializes it to disk using the .NET SOAP formatter.

 graphics/codeexample.gif ' Formatter.vb Imports System.Web.Services Imports System.IO Imports System.Runtime.Serialization.Formatters.Soap <Serializable()> Public Class Customer    Public name As String    Public id As Long    Public nextc As Customer End Class Module Formatter    Sub Main()       Dim list As Customer = GetList()       Dim s As New FileStream("cust.xml", FileMode.Create)       Dim f As New SoapFormatter()       f.Serialize(s, list)       s.Close()       Console.WriteLine("File written: cust.xml")    End Sub    Function GetList() As Customer       Dim cust1 As New Customer()       cust1.name = "John Smith"       cust1.id = 1       Dim cust2 As New Customer()       cust2.name = "Mary Smith"       cust2.id = 2       cust2.nextc = cust1       cust1.nextc = cust2       Return cust1    End Function End Module 

This program produces the file cust.xml that has the following SOAP encoding. Note the use of the id attribute to identify objects and fields, and the href attribute that serves as an object reference.

 ... <SOAP-ENV:Body> <a1:Customer id="ref-1" ... <name id="ref-3">John Smith</name> <id>1</id> <nextc href="#ref-4"/> </a1:Customer> <a1:Customer id="ref-4" ... <name id="ref-5">Mary Smith</name> <id>2</id> <nextc href="#ref-1"/> </a1:Customer> </SOAP-ENV:Body> ... 

The second program, WebService , as its name suggests, is a Web service. The first version CircularList.asmx creates a circular list, like the previous example. The second version, NotCircularList.asmx , comments out the line that makes the list circular.

 <%@ WebService language="VB" class="Test" %> Imports System.Web.Services Imports System.IO Public Class Customer    Public name As String    Public id As Long    Public nextc As Customer End Class <WebService(Namespace:="http://www.oi.com/netvb")> _ Public Class Test    Inherits WebService    <WebMethod()> Function GetList() As Customer       Dim cust1 As New Customer()       cust1.name = "John Smith"       cust1.id = 1       Dim cust2 As New Customer()       cust2.name = "Mary Smith"       cust2.id = 2       cust2.nextc = cust1       ' Comment out next line to make not circular       cust1.nextc = cust2       Return cust1    End Function End Class 

You may run this Web service from the home page for this chapter or with the URL http://localhost/Chap15/SOAP%20Differences/WebService/CircularList.asmx . Internet Explorer will indeed recognize it as a Web service. See Figure 15-4.

Figure 15-4. Internet Explorer recognizes CircularList.asmx as a Web service.

graphics/15fig04.jpg

However, if you go on to invoke the Web service, you will get the following error:

 ... System.InvalidOperationException: A circular reference was detected while serializing an object of type Customer. ... 

The XML Serializer used to produce the SOAP for Web services cannot handle the circular reference. If you comment out the line of code cust2.nextc = cust1 , the Web service will be able to respond, as shown in Figure 15-5, although the whole circular list is not returned.

Figure 15-5. NonCircularList.asmx behaves as a legal Web service.

graphics/15fig05.jpg

 ...   <name>John Smith</name>   <id>1</id>   <next>     <name>Mary Smith</name>     <id>2</id>     <next xsi:nil="true" />   </next> ... 

There is no notion, however, of any real relationship between the items, as there was in the remoting case. Why can the SOAP in .NET remoting handle the relationships while the SOAP in Web services cannot?

SOAP handles the complicated relationships (multiple parents, graphs, etc.) that exist in an object model. XML Schema still reflects the XML heritage of document processing where you can model a document as a tree with a single root, each node having one parent. Since SOAP was being developed before XML Schema was finished, SOAP has some extensions to handle those cases. Since they are in Section 5 of the SOAP specification, they are often referred to as the Section 5 encoding rules.

Those parts of the Section 5 encoding rules that are extensions cannot be incorporated in any XML document that has to be validated against a schema. Hence, the .NET XML serialization classes do not use them. On the other hand, the .NET remoting serializer does not care about schema validation; it cares about the ability to remote full object fidelity and hence uses all the Section 5 rules. In order to maximize interoperability, Web services implementations tend to use only XML Schema-compliant forms that can be validated against a schema. [12] The counterargument can be made that schema validation is not as important when machines are generating the XML, but the industry has not yet taken that approach. [13]

[12] If you have a COM background, think of the work the proxy has to do to handle pointer aliasing if the pointer_default(unique) attribute is not used.

[13] Although we will not discuss them here, there are attributes you can set on your Web service class and methods to have them use the Section 5 rules.

If you want applications and Web services that reside on different operating system platforms to interoperate , define your Web services with XML Schema first, then develop the associated WSDL. You can then create an abstract class that can be the basis for an .asmx file by using the /server option on the wsdl tool.

Starting with an object model and then modeling it with XML Schema might result in incompatible systems. Of course, if only simple types and structures are involved, you are not going to have problems. If you have existing object models, you may need a wrapper layer that translates the Web services layer and moves it into your existing object model. This is the major technological challenge of Web servicesgetting the object models on different platforms to work together. [14]

[14] There is no intent here to slight the security issues associated with Web services, but if you cannot get the object models to work together in some fashion, security becomes irrelevant because there is nothing to make secure.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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