Home / Articles / Programming Languages / Web Services / A Web Services Primer (7/10): Show Me The Money!

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

Weekly Programming Series - A Web Services Primer

A Web Services Primer (7/10): Show Me The Money!

Yashavant Kanetkar and Asang Dani
Yashavant Kanetkar and Asang Dani

Every inventor dreams of creating something that will change people's lives. When he/she finally gets there, one important issue remains – does he benefit from the discovery or not? Web Service developers strive to do something similar. After creating something good, you should be able to benefit from it financially. Thus it is important to be able to build a revenue model around your Web Service. This article will show you how.

Show Me The Money!

Let us see how simple it is to create a revenue model around our CurrencyService. Out of the five methods that are provided by this service, we can charge users for using the most important APIs—GetRateBySymbol( ) and GetRateByCountry( ). To convince users to pay for the service, it would be a nice idea to return some more useful information to our subscribers. This information can be the exact time at which conversion rate was obtained from the marketplace. This will be of great importance to currency traders whose business depends on the "liveness" of data.

To achieve this, we will use ability of ASP.NET web services to exchange SOAP headers between client and server. Figure 1 shows the workflow that we propose to use for our revenue based implementation.

Figure 1: Web Service Revenue Model Workflow

Each subscriber will be assigned a unique 128-bit identifier. These identifiers would be stored in a specially created subscriber database. Each time the subscriber calls either GetRateBySymbol( ) or GetRateByCountry( ) his unique id would be passed to them. Note that the id would be passed through a SOAP header and not as a method parameter. In the method it would be verified from the database whether the id is valid and the subscription has not expired. If any of these conditions fail then an error is returned. Otherwise, the method would be executed and the conversion rate would be returned. In the response the time at which the conversion rate was obtained would also be returned via the SOAP header.

Let us now see the steps involved in implementing this model in CurrencyService. These are described below.

1) Create a public class SubscriberHeader that inherits from SoapHeader.

public class SubscriberHeader : SoapHeader
{
  public string id ;
  public DateTime serverTime ;
}

 

2) Add a public variable subscriberKey of type KeyHeader in CurrencyService class.

public class CurrencyService : System.Web.Services.WebService
{
  public SubscriberHeader sHeader ;
  // …
}

 

3) Create Subscriber database using MS SQL Server 2005 Express Edition. Figure 2 shows the schema for this database.

Figure 2: Subscriber Database

As you will notice, SubId is the unique identifier assigned to each developer. This 128-bit ID is generated using SQL server function NEWID( ) while inserting a new subscriber in the database. The Status table maintains mapping from numeric status value to its string representation – e.g. 1 – Disabled, 2 – Enabled etc. We can easily create a simple ASP.NET Web Application to manage this Subscriber database. However, that's hardly the point here. So we recommend that you directly use Visual Studio 2005 Data Designer to insert some initial entries in this database. We have created two subscribers – Jayant and Hirohito. One of them is disabled while other is enabled. Figures 3 & 4 show the details.

Figure 3: Subscriber Table Data

 

Figure 4: SubscriberStatus Table Data

 

4) To programmatically access the database through our web service we need to create a database adapter. This is done by adding a dataset to our web service. For this right click on App_code node in Solution Explorer and choose 'Add New Item'. In the dialog that appears, select Dataset as the item type and choose SubscriberDS.xsd as the name of this dataset.

Figure 5: Subscriber DataSet

 

5) Now we need to add a simply query GetStatusById to this dataset. Figure 6 shows this query. This query will allow us to validate subscriber id and his status.

Figure 6: Query for Subscriber validation

 

6) Now we need to attach a SoapHeader attribute to methods GetRateBySymbol( ) and GetRateByCountry( ) and validate the call to these methods. SoapHeader attribute specifies the name of the variable (sHeader) in which the received header will be copied. We can specify the direction for SOAP header. Direction can be one of the following:

  • Input: Client sends the header to the web service
  • Output: Web service constructs the header and returns it to the client
  • InputOutput: The header is first sent by the client. Web service may manipulate one or more fields in it, after which it would be returned to the client.

In our case, SOAP header has two fields—SubscriberId and currency conversion time. Of these, former is the input parameter and later is the output parameter. The modified methods with SoapHeader attribute applied to them are shown below:

  [WebMethod]
  [SoapHeader ( "sHeader", Direction = SoapHeaderDirection.InOut )]
  public float GetRateByCountry ( String from, String to )
  {
    try
    {
      return GetRateBySymbol ( GetCurrency ( from ).symbol,
        GetCurrency ( to ).symbol );
    }
    catch ( Exception ex )
    {
      return -1.0f ;
    }
  }

  [WebMethod (CacheDuration = 60) ]
  [SoapHeader ( "sHeader", Direction = SoapHeaderDirection.InOut )]
  public float GetRateBySymbol ( String from, String to )
  {
    if ( ! ValidateSubscriber ( sHeader ) )
      return -1.0f ;
 
    String URL = String.Format (
      "http://www.xe.com/ucc/convert.cgi?Amount=1&From={0}&To={1}", 
       from, to ) ;
    WebRequest req = WebRequest.Create ( URL ) ;
    WebResponse resp = req.GetResponse ( ) ;
    StreamReader sr = new StreamReader ( resp.GetResponseStream ( ) ) ;
    String respStr = sr.ReadToEnd ( ) ;
    // Live rates at 2007.12.19 05:10:33 UTC 
    Match m = Regex.Match ( respStr, @"Live\srates\sat\s(.+)UTC" );
    sHeader.serverTime =
      DateTime.Parse ( m.Groups [ 1 ].Value ).ToLocalTime ( );
    String reExp = 
String.Format ( @"[0-9.]+\s*{0}\s*=\s*([0-9.,]+)\s{1}", 
from, to ) ;
    m = Regex.Match ( respStr, reExp ) ;
    String x = m.Groups [ 1 ].Value ;
    return float.Parse ( x ) ;
  }

The GetRateByCountry( ) method is same as before except for the addition of SoapHeader attribute. In GetRateBySymbol( ) we have added the code for:

  • Validating the user by calling ValidateSubscriber( ).
  • Obtaining the currency conversion time and assigning it to the SOAP header field serverTime.

7) The ValidateSubscriber( ) function uses the database adapter class created earlier using the SubscriberDS dataset and calls GetStatusById( ) to get current status of the subscriber. Only if it is enabled, it returns true. The VaildateSubscriber( ) function is shown below.

  private bool ValidateSubscriber ( SubscriberHeader key )
  {
    if ( key == null )
      return false ;

    SubscriberTableAdapter a = new SubscriberTableAdapter ( );
    try
    {
      string status ;
      status = ( string ) a.GetStatusById ( 
                       new System.Guid ( key.id ) );
      if ( status == null )
        return false ;
      if ( status != "Enabled" )
        return false ;
    }
    catch ( FormatException fex )
    {
      // Invalid GUID format
      return false ;
    }
    return true ;
  }

Phew! You can now heave a sigh of relief. That was one long explanation. But we hope that it was worth the trouble. After all, this is going to help you monetize your web services.

You would appreciate that to use such a web service we need to make some changes in the client code. You have two choices—either do them yourselves, or wait till the next article to where we would unravel this for you. Choice of course, is entirely yours!

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts