Home   Cover Cover Cover Cover
 

Fail-safe web service usage

Question: see book

Answer:

The web page source at the server can look like this:
XRate.aspx
<% @Page Language="C#" Inherits="XRatePage" Src="XRatePage.cs" Culture="en-US" Trace="false" %>

<html>
<head> <title>Exchange Rate</title> </head>
<body>

<p> <% GetXRate(); %> </p>

</body>
</html>

and the code behind in C# like this:
XRatePage.cs
using System;
using System.IO;
using System.Net;
using System.Web.UI;

public class XRatePage : Page {
  protected readonly string appdir;
  
  protected XRatePage () { appdir = MapPathSecure(TemplateSourceDirectory); }
  
  public void GetXRate () {
    string rateFormat = "On {1:D} at {1:t} one US Dollar bought class=\"2\">{0:f4} Euros.";
    
    CurrencyExchangeService xrateService = new CurrencyExchangeService();

    double rate = -1.0;
    try { rate = xrateService0.getRate("usa", "euro"); }
    catch (WebException we) { /* could not retrieve data from WebService0 */ }

    DateTime date = DateTime.Now;
    
    if (rate >= 0.0) WriteXRate(rate, date);  // write new rate to file
    else ReadXRate(out rate, out date);       // read last rate from file
        
    if (rate >= 0.0) Response.Write(String.Format(rateFormat, rate, date));
    else Response.Write("Sorry, exchange rate information unavailable.");
  }
  
  void WriteXRate (double rate, DateTime date) {
    BinaryWriter writer = new BinaryWriter(File.OpenWrite(appdir + "\\xrate.data"));

    writer.Write(date.ToString());
    writer.Write(rate);
    writer.Close();
  }
  
  void ReadXRate (out double rate, out DateTime date) {
    BinaryReader reader = new BinaryReader(File.OpenRead(appdir + "\\xrate.data"));

    date = DateTime.Parse(reader.ReadString());
    rate = reader.ReadDouble();
    reader.Close();
  }
}


With the following command line:

wsdl /out:XRateService.cs http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl

we generated the following source code for the service proxy at the server:
XRateService.cs
//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// 
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by wsdl, Version=1.1.4322.573.
// 
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


/// 
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="CurrencyExchangeBinding", Namespace="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl")]
public class CurrencyExchangeService : System.Web.Services.Protocols.SoapHttpClientProtocol {
    
    /// 
    public CurrencyExchangeService() {
        this.Url = "http://services.xmethods.net:80/soap";
    }
    
    /// 
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:xmethods-CurrencyExchange", ResponseNamespace="urn:xmethods-CurrencyExchange")]
    [return: System.Xml.Serialization.SoapElementAttribute("Result")]
    public System.Single getRate(string country1, string country2) {
        object[] results = this.Invoke("getRate", new object[] {
                    country1,
                    country2});
        return ((System.Single)(results[0]));
    }
    
    /// 
    public System.IAsyncResult BegingetRate(string country1, string country2, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("getRate", new object[] {
                    country1,
                    country2}, callback, asyncState);
    }
    
    /// 
    public System.Single EndgetRate(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((System.Single)(results[0]));
    }
}

and then compiled it with the command line:
csc /out:bin\XRateService.dll /target:library XRateService.cs