Calling the XML


Middle Tier Code

Now that we have the underlying XML code written, you can begin to write the code necessary to create the beginning of the middle tier. This section examines creating the code for the middle tier and then creating methods that make the most sense for utilizing Web Services.

Web Service with XML Access

The following code example contains C# code necessary to access the data in the XML file shown earlier in the chapter. The first method, validateId, compares the ID passed into the method to all the IDs in the XML file. If it finds a match using the string compare method, a 0 is returned—which seems opposite of what it should be.

The following method, ReturnEmail, returns the e-mail address of the person with the particular ID, and ReturnAll returns an array of all the values found in the XML file.

These methods in the Web Services are wrappers for the methods shown in the previous C# dll.

Note

The XML methods return a hash rather than an array, but in order for the Web Service to return values, it must use an array or another structure that can be represented by the SOAP standard. It is worthwhile to run some tests with return values that are more complex than char or int. Not all complex structures, such as hashes, can be represented by SOAP.

    using System;     using System.Collections;     using System.ComponentModel;     using System.Data;     using System.Diagnostics;     using System.Web;     using System.Web.Services;     using XmlSearchString;     namespace MiddleTier     {      [WebService(Description="XPlatform Middle Tier Example",       Namespace="http://www.advocatemedia.com/")]      public class Service1 : System.Web.Services.WebService      {           public Service1()           {               InitializeComponent();           }           #region Component Designer generated code           //Required by the Web Services Designer           private IContainer components = null;           private void InitializeComponent()           {           }           protected override void Dispose( bool disposing )           {                if(disposing && components != null)                {                     components.Dispose();                }                base.Dispose(disposing);           }           #endregion           [WebMethod(Description="check if id exists")]           public int ValidateId(string id)           {             GetXmlLibrary myGetXml = new GetXmlLibrary();             Hashtable myHashTable = new Hashtable();             myHashTable = myGetXml.getData(id);             string returnValue = (string)myHashTable["id"];             int result = string.Compare(id,returnValue);             //weird -- 0 if true             return(result);           }            [WebMethod (Description="Return Email for Given id")]           public string ReturnEmail(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml = new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             return((string)myHashTable["email"]);           }           [WebMethod(Description="Return Password for given id")]           public string ReturnPassword(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml = new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             return((string)myHashTable["password"]);           }           [WebMethod(Description="Return all data as an array")]           public string[] ReturnAll(string id)           {             Hashtable myHashTable = new Hashtable();             GetXmlLibrary myGetXml = new GetXmlLibrary();             myHashTable = myGetXml.getData(id);             string[] myStringArray = new string[6];               myStringArray[0] = (string)myHashTable["id"];               myStringArray[1] = (string)myHashTable["password"];             myStringArray[2] = (string)myHashTable["email"];             myStringArray[3] = (string)myHashTable["title"];               myStringArray[4] = (string)myHashTable["FirstName"];               myStringArray[5] = (string)myHashTable["LastName"];             return(myStringArray);           }        }      }

The Web Service acts like any other class. There are methods that return single important values and one method that returns everything. The question you need to ask yourself when deploying Web Services is if your design is efficient enough for the request and response model that HTTP and, therefore, Web Services are so dependent on.

Thinking about Efficient Web Services

This section examines how to make Web Services more efficient. Each method call requires a request and response to the server. Thus, if you are making several requests to a service for simple information, the response time of your application will be slower than for other technologies such as COM+.

For the previous example, take a look at the SOAP requests and responses to get an idea of how much traffic is really being generated.

First the application asks for the e-mail address for the particular ID.

    <?xml version="1.0" encoding="utf-8"?>   <soap:Envelope      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema">       <soap:Body>         <ReturnEmail xmlns="http://www.advocatemedia.com/">            <id>10112</id>         </ReturnEmail>       </soap:Body>    </soap:Envelope>

And the response is secret@hotmail.com.

    Content-Type:text/xml; charset=utf-8     <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema">       <soap:Body>         <ReturnEmailResponse           xmlns="http://www.advocatemedia.com/">            <ReturnEmailResult>               secret@hotmail.com            </ReturnEmailResult>         </ReturnEmailResponse>       </soap:Body>     </soap:Envelope>

Now the method calls the service with the ID to return the password.

    <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema">      <soap:Body>         <ReturnPassword              xmlns="http://www.advocatemedia.com/">            <id>10112</id>         </ReturnPassword>      </soap:Body>      </soap:Envelope> 

So the response comes back with the e-mail address.

    Content-Type:text/xml; charset=utf-8     <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema">       <soap:Body>         <ReturnPasswordResponse              xmlns="http://www.advocatemedia.com/">           <ReturnPasswordResult>              something           </ReturnPasswordResult>         </ReturnPasswordResponse>       </soap:Body>      </soap:Envelope>

Now that the information is confirmed for our individual user, let’s return all the information about the user.

    <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">       <soap:Body>         <ReturnAll xmlns="http://www.advocatemedia.com/">           <id>10112</id>         </ReturnAll>       </soap:Body>     </soap:Envelope> 

Now, the following is the long response containing all the information about this individual.

    Content-Type:text/xml; charset=utf-8     <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">       <soap:Body>         <ReturnAllResponse            xmlns="http://www.advocatemedia.com/">           <ReturnAllResult>             <id>10112</id>             <password>something</password>             <email>secret@hotmail.com</email>             <title>Senior Programmer</title>             <FirstName>David</FirstName>             <LastName>Pallai</LastName>             <returnAmountUs>0</returnAmountUs>             <returnAmountForeign>0</returnAmountForeign>           </ReturnAllResult>         </ReturnAllResponse>        </soap:Body>      </soap:Envelope>

The transactions shown in the previous SOAP messages demonstrate a typical exchange between a Web page and a database server, but for that type of connection these types of transactions make sense. Many times the connection between a Web server and a database is held open—making the transactions very fast. With Web Services, the exchanges take more time because the connection cannot be held open, because the HTTP standard doesn’t allow for that. So you need to examine the structure and efficiency of your methods and how you access them. For example, in the request and response shown previously, it would have been much faster just to return all the information first rather than confirming each piece.

Tip

Because a Web Service does generate so much Web Service traffic, it is better to design methods that contain everything you need to do within the Web Service. That may mean cramming several method calls to different objects within one Web method, and this probably violates good object oriented design. But it does cut down on the amount of traffic a Web Service generates.

Let’s make the Web Service generate even more traffic by adding one more layer to the middle tier by creating a proxy to the Web Service shown in Chapter 11.

Adding a Proxy to MoneyExchangeService

The following code snippet creates proxy methods to each of the methods in the Java Web Service ReturnCurrencyName. By adding a Web reference to this service in Visual Studio, we can then wrap each method as a C# Web method. Notice how each method creates an instance of the object, calls the method, and then returns the value. C# does very little work here.

    [WebMethod(Description="Get the name of the currency")]      public string ReturnCurrencyName(string countryName)      {       advocatemedia.MoneyExchangeService myExchange = new       advocatemedia.MoneyExchangeService();       string moneyName = myExchange.returnCurrencyName(countryName);      return(moneyName);      }     [WebMethod(Description="Foreign Equivalent of US Dollars")]      public double ReturnForeignEquiv(string country, float amount)      {      advocatemedia.MoneyExchangeService myExchange = new       advocatemedia.MoneyExchangeService();      double returnedAmount =       myExchange.returnForeignEquiv(country, amount);       return(returnedAmount);      }     [WebMethod(Description="Return the US Dollar Equiv ")]      public double ReturnUSDollarEquiv(string country, float amount)      {      advocatemedia.MoneyExchangeService myExchange = new       advocatemedia.MoneyExchangeService();      double returnedAmount =       myExchange.returnUSDollarEquiv(country, amount);       return(returnedAmount);      }

This adds even more traffic because every request to this service causes a request to be made to another service. So not only is there a request and response to this service but also one to MoneyExchangeService. Figure 12.4 demonstrates this.

click to expand
Figure 12.4: Proxy requests to another Web Service doubles the amount of traffic being generated.

The next section looks at ways of making Web Services more efficient.




Cross-Platform Web Services Using C# and Java
Cross-Platform Web Services Using C# & JAVA (Charles River Media Internet & Web Design)
ISBN: 1584502622
EAN: 2147483647
Year: 2005
Pages: 128

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