Web Service Class

for RuBoard

As we have previously demonstrated, a Web Service is nothing but an HTTP request. As such, a Web Service can access the intrinsic objects associated with its HTTP request. These are the same intrinsic objects discussed in the section "State in ASP.NET Applications" in the previous chapter. The WebService class has properties that access these intrinsic objects.

You need not derive your Web Service class from the framework WebService class. You can derive your Web Service class from a different base class if necessary. In this case you can use the current HttpContext to access the intrinsic objects. The WebService class inherits from MarshalByRefObject , however, so if you want your Web Service class to be remotable, and you do inherit from a different base class, make sure that class also inherits from MarshalByRefObject . The HttpContext enables you to get information about an HTTP request. By using the static Current property, you can get access to the current request.

We will now build a Web Service inside Visual Studio.NET that will illustrate the use of these intrinsic objects inside a Web Service. As Figure 11-4 demonstrates , choose ASP.NET Web Service from the New Project dialog box in Visual Studio.NET.

Figure 11-4. Visual Studio.NET New Project dialog with ASP.NET Web Service project selected.

graphics/11fig04.gif

When you click the OK button, VS.NET will setup a Web Service project for you. By default, the Web Service files are placed in a subdirectory of the IIS directory on your hard drive. By default, projects are placed in a VSWebCache\MachineName subdirectory under the Documents and Settings directory for the logged in user . Figure 11-5 shows the resulting VS.NET project.

Figure 11-5. Visual Studio.NET Web Services project.

graphics/11fig05.gif

Our Web Service will have several methods that demonstrate how to use the intrinsic objects. As you will see, this is really no different from their use in ASP.NET. Two of the methods will illustrate the use of application and session state by calculating a cumulative sum of numbers .

In the global.asax file we initialize our sum to zero in the appropriate event handlers. Global.asax has the same function in Web Services as it does for ASP.NET, as discussed in the previous chapter in the section "ASP.NET Applications." Since the Global class inherits from System.Web.HttpApplication , it can access the Application and Session intrinsic objects.

 public class Global : System.Web.HttpApplication  {   protected void Application_Start(Object sender,                                              EventArgs e)  {    Application["TotalSum"] = 0.0;  }  protected void Session_Start(Object sender, EventArgs e)  {    Session["SessionSum"] = 0.0;  }  ... 

Renaming the Service1.asmx file to arithmetic.asmx . we define several Web methods. By setting the EnableSession argument to the WebMethod constructor to true , we turn on session state for the SessionSum method. Every time a new session is started, the sum is reset to zero. On the other hand, for the CumulativeSum Web method, EnableSession is set to its default value or false, so that the sum is reset to zero only when the Web Service application is restarted. The Application intrinsic object is used from the HttpContext object to show how that class is used.

It should be clear from this code that HttpApplication , WebService , and HttpContext all reference the same intrinsic objects. If you need to save state for the application or session of a Web Service, you can use the collections associated with HttpApplicationState and HttpSessionState to do so.

 ...  [WebMethod(EnableSession = true)]  public double SessionSum(double x)  {    Session["SessionSum"] = (double)Session["SessionSum"]+x;    return (double)Session["SessionSum"];  }  [WebMethod]  public double CumulativeSum(double x)  {    double sum = (double) Application["TotalSum"];    sum = sum + x;    Application["TotalSum"] = sum;    return (double)HttpContext.Current.Application                                               ["TotalSum"];  }  ... 

The GetUserAgent method show how to use the Context object to access information about the request. We return what kind of application is accessing the Web Service. The GetServerInfo method accesses the Server intrinsic object.

 [WebMethod]  public string GetUserAgent()  {    return Context.Request.UserAgent;  }  [WebMethod]  public string GetServerInfo()  {    string msg = "Timeout for " + Server.MachineName + " = "                 + Server.ScriptTimeout + "; Located at " +                 Server.MapPath("");    return msg;  } 

The ArithmeticClient console program demonstrates the use of the Web Service. We can create a proxy class from within VisualStudio.NET. On the Project Menu, select Add Web Reference and type in the address of the Web Service in the Address edit box, followed by a carriage return. Information about the Arithmetic Web Service will appear as in Figure 11-6.

Figure 11-6. Visual Studio.NET display of Arithmetic Web Service information.

graphics/11fig06.gif

Click on the Add Reference button to add the Web reference. This will add a WebReferences set of subdirectories below the current project that will contain the proxy class and the wsdl file for the Web Service. To the client program we will have to reference the proxy class's namespace:

 using ArithmeticClient.localhost; 

We then calculate a sum using the total held by the Application intrinsic object. Next we calculate a sum for the total held by the Session intrinsic object.

 Arithmetic a = new Arithmetic();  double sum;  for (int i = 0; i < 5; i++)  {    sum = a.CumulativeSum(i);    Console.WriteLine("Adding {0},                    Application sum is now {1}", i, sum);  }  double sessionSum;  for (int i = 0; i < 5; i++)  {    sessionSum = a.SessionSum(i);    Console.WriteLine("Adding {0},                    Session sum is now {1}", i, sessionSum);  } 

This will give us the following output. The exact numbers for the application-based sum will depend on how many times you have run the application.

 Adding 0, Application sum is now 90  Adding 1, Application sum is now 91  Adding 2, Application sum is now 93  Adding 3, Application sum is now 96  Adding 4, Application sum is now 100  Adding 0, Session sum is now 0  Adding 1, Session sum is now 1  Adding 2, Session sum is now 2  Adding 3, Session sum is now 3  Adding 4, Session sum is now 4 

We now create another instance of the proxy class and make the same method calls.

 Arithmetic a2 = new Arithmetic();  for (int i = 0; i < 5; i++)  {    sum = a2.CumulativeSum(i);    Console.WriteLine("Adding {0},                       Application sum is now {1}", i, sum);   }  for (int i = 0; i < 5; i++)  {    sum = a2.SessionSum(i);    Console.WriteLine("Adding {0},                           Session sum is now {1}", i, sum);  } 

We get the following output. Notice how the application sum continues to increase, while the session bases sum starts again from zero. A new browser session is not the only way to start a new Web Service session.

 Adding 0, Application sum is now 100  Adding 1, Application sum is now 101  Adding 2, Application sum is now 103  Adding 3, Application sum is now 106  Adding 4, Application sum is now 110  Adding 0, Session sum is now 0  Adding 1, Session sum is now 1  Adding 2, Session sum is now 2  Adding 3, Session sum is now 3  Adding 4, Session sum is now 4 

Finally we call the GetUserAgent and GetServerInfo Web methods.

 Console.WriteLine(a2.GetUserAgent());  Console.WriteLine(a2.GetServerInfo()); 

The output will look something like this:

 Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services                                Client Protocol 1.0.2914.16)  Timeout for MICAH = 90; Located at  f:\inetpub\wwwroot\Arithmetic 
for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

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