Restricting a Web Service to Specific Hours of the Day

One way to offer programmers an opportunity to test a web service is to restrict the service’s use to specific hours of the day. For example, the 8to5 web service in Listing 15.14 limits the time of day that programmers can access a trial version of a web service to the hours of 8 to 5. If a program tries to access the web service during hours outside of 8 to 5, the web service will generate the InvalidHours exception. The service provides two methods—one that returns the number of days remaining in the year, and one that returns the number of days remaining in the current month.

To create the 8to5 web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name 8to5. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 15.14.

Listing 15.14 8to5.asmx.cs

start example
[WebMethod]    public int DaysRemainingInYear()    {       int Hour = DateTime.Now.Hour;       int DayOfYear = DateTime.Now.DayOfYear;       int Year = DateTime.Now.Year;       if ((Hour < 8) || (Hour > 17))         throw (new Exception("Invalid hours of operation"));       else         return ((DateTime.IsLeapYear(Year) ? 366: 365) - DayOfYear);    } [WebMethod]    public int DaysRemainingInMonth()    {       int Year = DateTime.Now.Year;       int Month = DateTime.Now.Month;       int Day = DateTime.Now.Day;       int Hour = DateTime.Now.Hour;       if ((Hour < 8) || (Hour > 17))            throw (new Exception("Invalid hours of operation"));       else          return (DateTime.DaysInMonth(Year, Month) - Day);    }
end example

As you can see, code simply uses a DateTime object to determine the current hour of the day. If the hour is between 8:00 a.m. and 5:00 p.m., the methods will execute. In this case, for simplicity, the web service does not require the user to provide a key. In a real-world implementation, each method would determine if the user was a paid or trial user. If the user is a paid user, the method will immediately perform its processing. If the user is a trial user, the method would then determine the current time of the day and restrict the method’s use to the hours of 8 to 5.

In a similar way, the OddHours web service in Listing 15.15 lets programmers test drive the trial version of your web service during odd hours. In so doing, the web service provides programmers with windows of time they can use to test your service while restricting users from accessing the service throughout the day. The source code in Listing 15.15 implements the web service methods.

Listing 15.15 OddHours.asmx.cs

start example
[WebMethod] public int DaysRemainingInYear()    {       int Hour = DateTime.Now.Hour;       int DayOfYear = DateTime.Now.DayOfYear;       int Year = DateTime.Now.Year;       if (Hour % 2 == 0)          throw (new Exception("Invalid hours of operation"));       else          return ((DateTime.IsLeapYear(Year) ? 366: 365) - DayOfYear);    } [WebMethod] public int DaysRemainingInMonth()    {          int Year = DateTime.Now.Year;          int Month = DateTime.Now.Month;          int Day = DateTime.Now.Day;          int Hour = DateTime.Now.Hour;          if (Hour % 2 == 0)          throw (new Exception("Invalid hours of operation"));          else          return (DateTime.DaysInMonth(Year, Month) - Day);    }
end example

As you can see, the web service methods determine if the hour is odd or even by dividing the current hour by two and examining the remainder (an even hour will have a remainder of 0 whereas an odd hour will have the remainder of 1).




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

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