Sunday, December 19, 2010

Convert Vs Parse Vs TryParse

What’s the difference between all three:

Covert:
Contains a series of functions to convert one base class into another. It just checks for a null string (if the string is null it returns zero unlike the Parse) then just calls Int32.Parse(string).

Parse:
This function will take a string and attempt to extract an integer out of it and then return the integer. If it runs into something that it can’t parse due to incorrect or null value then it throws a FormatException or if the number is too large an OverflowException.

TryParse:
If TryParse is unable to parse the string it does not throw an exception like Parse does. Instead, it returns a Boolean indicating if it was able to successfully parse a number.

In general it’s a best practice to use TryParse and do precondition checks before using the output value. Simply because:
1.       Return Boolean value: No exceptions which could slow down performance. Instead Boolean return value which gives indication if the value  is successfully converted or not.
2.       Precondition check: Before using the actual value you are sure it’s actually converted

Saturday, November 15, 2008

WS Response: XML Compression

Compressing the XMLs sent over the wire helps reduce the network traffic significantly. Compressing responses on the server and decompressing them on the client consumes additional CPU cycles on both server and client, but the savings in bandwidth is likely to justify the processing cost.

XML compression can be implemented by using one of the following techniques:

1. Set EnableDecompression property

EnableDecompression property gets or sets a Boolean that indicates whether decompression is enabled for this HttpWebClientProtocol. Default value for the property is “False”. The property is supported in .Net framework 2.0 and above.

E.g.

WebServiceRef.Service service = new WebServiceRef.Service();

service.EnableDecompression = true;

When the server returns the results, they are in gzip format, deflated format, or uncompressed. The property is only a suggestion, not a demand.

2.     By extending proxy class in client for the compression and decompression of requests and responses

To enable compression in .Net 1.1 GetWebRequest, GetWebResponse methods needs to be overridden in the proxy class generated. As the modified code will be removed if web reference needs to be added again, instead of modifying proxy code a class is inherited from proxy class and the methods are overridden in that class.

This method is also useful when the message created by proxy needs modification before sending it.

E.g.

protected override WebRequest GetWebRequest(Uri uri)

// Update the request's HTTP headers to specify that

// we can accept compressed (gzipped, deflated) responses

          {

                   WebRequest request = base.GetWebRequest(uri);

                   if (compressResponse)

                   {

                             request.Headers.Add("Accept-Encoding", "gzip, deflate");

                   }

                             return request;

                   }

protected override WebResponse GetWebResponse(WebRequest request)

// If we've requested compressed responses, return a WebResponse

// derivative that's capable of uncompressing it.

          {

                   WebResponse result;

                   // If user checked the Compressed Response checkbox

                   if (compressResponse)

                   {

                             result = new CompressedWebResponse((HttpWebRequest) request);

                   }

                   else // no compression requested, return stock WebResponse object

                   {

                             result = base.GetWebResponse(request);

                   }

                   // Keep track of content length to measure bandwidth savings.

                   responseContentLength = result.ContentLength;

                   return result;

          }

The CompressedWebResponse class uses the #ziplib library (available athttp://www.icsharpcode.net/OpenSource/SharpZipLib/) to decompress responses.

3.     Use HTTP compression features available in IIS 5.0 and later versions for compressing the response from the Web services.

Note that you need a decompression mechanism on the client and client should request the server for zipped response (This could be done by point 1 and 2 above).

To enable IIS 5.0 to compress .aspx pages, follow these steps:

Open a command prompt.

1.      Type net stop iisadmin, and then press ENTER.

2.      Type cd C:\InetPub\adminscripts, and then press ENTER.

3.      Type the following, and then press ENTER:

4.      CSCRIPT.EXE ADSUTIL.VBS SET     W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx" Type the following, and then press ENTER:

5.      CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx"

6.      Type net start w3svc, and then press ENTER

Performance of the application can be increased by executing multiple web methods at the same time if they are not dependent on each other.

For example,

Consider two web service methods as follows:

[WebMethod]

    public float CalculateIncomeTax(float basic)

    {

        //Complicated calculations which takes time

        Thread.Sleep(8000);      

 

        return basic/100;

    }

[WebMethod]

    public float CalculateServiceTax(float basic)

    {

        //Complicated calculations which takes time

        Thread.Sleep(5000);

 

        return basic/1000;

    }

The following code calls these methods.

Synchronous call:

private void buttonSynch_Click(object sender, EventArgs e)

        {

            //Calculates Income tax

            StartProcess(true);

            newWebService.CalculateIncomeTax(100000);

            StartProcess(false);

            //Calculates Service tax

            StartProcess(true);

            newWebService.CalculateServiceTax(100000);

            StartProcess(false);

        }

Time required for execution = Time required for ( CalculateIncomeTax) + Time required for (CalculateServiceTax)

=  Approx 8 Sec. + Approx 5 Sec.

=  13.963955 ( Actual time required)

= Approx 14 Sec.

 

Asynchronous call for one function:

private void buttonAsynch_Click(object sender, EventArgs e)

        {

            StartProcess(true);

            newWebService.CalculateIncomeTaxAsync(100000);

            StartProcess(true);

            newWebService.CalculateServiceTax(100000);

            StartProcess(false);

  }

Time required for execution = Max (Time required for ( CalculateIncomeTax),  Time required for (CalculateServiceTax) )

=  Max ( Approx 8 Sec. , Approx 5 Sec. )

=  8.429886 (Actual time required )

= Approx 8 Sec.

Method StartProcess is written to signal start and end of processing.

int countProcess = 0

void StartProcess( bool startProcess )

        {

            if (startProcess)

                m_countProcess++;

            else

                m_countProcess--;

            if( m_countProcess == 0 )

                labelProcessingTax.Text = "";

            else

                labelProcessingTax.Text = "Processing";

            labelProcessingTax.Refresh();

}

Event handler is written to handle Asynchronous call completion event for CalculateIncomeTax method.

newWebService.CalculateIncomeTaxCompleted += new WindowsApplication1.NewService.CalculateIncomeTaxCompletedEventHandler(newWebService_CalculateIncomeTaxCompleted);

void newWebService_CalculateIncomeTaxCompleted(object sender, WindowsApplication1.NewService.CalculateIncomeTaxCompletedEventArgs e)

        {

            if (e.Cancelled == true)

                return;

             //Output is e.Result, use it to display IncomeTax

             StartProcess(false);

        }

Web service proxy class is generated by Visual studio when web reference is added.

Proxy class contains two overloaded methods for each web method to facilitate asynchronous calls.

 

       I.            Single-invocation:

(Web method name)Async ( method parameters…)

 

     II.            Multiple-invocation:

(Web method name)Async ( method parameters…, , object userState)

          Userstate should be unique value (for example, a GUID or hash code).

Usage:

1.      Multiple asynchronous calls:

Multiple-invocation makes possible calling methods asynchronously multiple times without waiting for any pending asynchronous operations to finish.

Trying to call methods asynchronously with single-invocation syntax multiple times, before previous invocation is completed throws InvalidOperationException.

 

2.      Tracking Pending Operations:

userState objects passed as parameters can be stored in collection to keep track of all pending tasks.

 

3.      Canceling Pending Operations:

Any particular instance of asynchronous operation can be cancelled by specifying the userState parameter passed during invocation to CancelAsync function.

E.g. newWebService.CancelAsync(userState);

Asynchronous calls invoked by single-invocation syntax are not cancellable.

Caching in ASP.Net WebService

One of the ways to enhance Web Service Performance is by caching data.

Various caching mechanisms possible are:

·         ASP.NET Output Caching

·         HTTP Response Caching

·         ASP.NET Application Caching

Before using any caching mechanism caching design for a Web service no. issues will be required to be considered and addressed, like:

·         How frequently the cached data needs to be updated

·         Whether the data is user-specific or application-wide

·         What mechanism to use to indicate that the cache needs updating, etc 

Which caching mechanism could be used will depend upon the pros and cons of each method and the properties of the data being returned.

 

ASP.NET Output Caching:

This type of caching could be considered when the data is static or almost static. 

This could be simply achieved by adding a ‘CacheDuration’ property to the WebMethod.

 

e.g.

  [WebMethod ( CacheDuration = 60)]

  public string GetCacheEntryTime(string Name)

    {

        StringBuilder sb = new StringBuilder("Hi ");

        sb.Append(Name);

        sb.Append(", the Cache entry was made at ");

        sb.Append(System.DateTime.Now.ToString());

       

        return (sb.ToString());

    }  

 

The method could be tested by calling it repeatedly with same and different parameters.

Calling the method in following sequence with a gap of 1 min:

textwriter.WriteLine(webService.GetCacheEntryTime("Angelina"));

textwriter.WriteLine(webService.GetCacheEntryTime("Brad"));

textwriter.WriteLine(webService.GetCacheEntryTime("DiffAngelina"));

textwriter.WriteLine(webService.GetCacheEntryTime("DiffBrad"));

textwriter.WriteLine(webService.GetCacheEntryTime("Angelina"));

textwriter.WriteLine(webService.GetCacheEntryTime("Brad"));

 

Results in Output as follows:

Hi Angelina, the Cache entry was made at 12/11/2007 1:16:37 PM

Hi Brad, the Cache entry was made at 12/11/2007 1:16:38 PM

Hi DiffAngelina, the Cache entry was made at 12/11/2007 1:16:41 PM

Hi DiffBrad, the Cache entry was made at 12/11/2007 1:16:42 PM

Hi Angelina, the Cache entry was made at 12/11/2007 1:16:37 PM

Hi Brad, the Cache entry was made at 12/11/2007 1:16:38 PM

 

Notice the same time stamp for same parameter value passed.

The .asmx page of Web Service by Default uses POST with “No-Cache” specified. Thus, this could not be tested with the test page of Web Service.

If the client wants to override server side output caching then it is possible by specifing “No-Cache” while making the request.