Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Programming Best Practices » Web Service Optimization

Web Service Optimization

This article gives you 10 tips to optimize web services.

Page Views : 14883
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Web Service

  • A Web services are used to expose data and functionality in a distributed environment.
  • The data exposed can be as simple as a list of product categories or as complex as a multileveled dataset containing a complete customer purchase history.
  • Web services are XML based, and distributed applications access them over the Internet using a communications protocol such as Simple Object Access Protocol (SOAP).
  • Because the data returned by a Web service is in the form of a standardized XML message, applications residing on different platforms can access the same Web services.
  • The primary difference between a Web service application and an ASP.NET application is the interface. An XML Web service relies on help pages and discovery files to interface with its users.
  • The user of a Web service is typically another application, so no traditional user interface is necessary.

Web Service optimization

  1. Call Web Methods Asynchronously
  2. Use ExecuteXmlReader instead of DataSet
  3. Use Compression
  4. Using SOAP Extensions
  5. Use One-Way Method
  6. Using Transactions in Web Services
  7. SOAP, HTTP GET, HTTP Post
  8. Create instances in advance to preload all the referenced assemblies at the very beginning
  9. Use pre-authentication
  10. Edit IIS custom error messages

Call Web Methods Asynchronously

  • Invoking a web-method asynchronously means that an invoked member will perform its work on a different thread and after all the work is done, this member must notify the main thread and, optionally, pass the return values to it.
  • This seriously increases performance. If a complicated and slow web method is executed in a separate thread, the main application can proceed performing its tasks without waiting for a web method to complete.
  • To call a Web method asynchronously, you have the following choices:

    Create a new thread and call the method from there.
    Create a delegate object that matches the Web method signature and call it from there.

Use ExecuteXmlReader instead of DataSet

  • Using ExecuteXmlReader you can return an XmlDocument object or a string from a Web service.
  • You can fully control the response of a WebMethod.
     

    String sqlString = "SELECT CustomerID, CompanyName FROM Customers For XML AUTO";

    rdr = command.ExecuteXmlReader();

    rdr.Read();

    while (rdr.ReadState != System.Xml.ReadState.EndOfFile)

    {

        ret += rdr.ReadOuterXml();

    }

Use Compression

  • Compress the XML-traffic between the client and the server using the SOAPExtensions mechanism and any compression algorithm.
  • You can use a ZIP-compression for you data. There are a lot of open-source compression libraries for C#.
  • You can use a free library called SharpZipLib to compress your web methods. 
  • Adding a compression to your webservice is very easy: include the downloaded project into your solution, reference it in your webservice, and mark your web methods with [CompressionExtension] in the proxy-class code.

Using SOAP Extensions

  • Typically SOAP extensions are used to handle data encryption or to compress data to provide better performance. SOAP extensions are often used in conjunction with a custom SOAP header for user authentication.
  • You can use custom SOAP extensions to handle unhandled exceptions for Web services.
  • For this solution, code need to be added in AfterSerialize stage to check for unhanded SOAP exceptions.
  • SOAP extension can be used to override the processing of the SOAP message on the Web server. On the Web server, there are four stages associated with these steps. Each of these stages is repeated twice.

    1. BeforeSerialize
    2. AfterSerialize
    3. BeforeDeserialize
    4. AfterDeserialize

Use OneWay Method

  • In many cases, the Web method you expose through your Web service does not require a response from the client. In cases such as this, you can specify that your Web method is a one-way method by applying the OneWay attribute.
  • Applying this attribute makes your method perform more efficiently because it will not wait for a response from the client.
  • Your method will only process input parameters and cannot be used to return referenced values or throw exceptions.
  • In this caller does not get anything in return so it is called as one-way communication. 
     

    [SoapDocumentMethod (OneWay=true)]

    [WebMethod]

    public string TestMethod()

    { } 

Using Transactions in Web Services

  • You can declare the Web service's transactional behaviour by setting the TransactionOption property of the WebMethod attribute applied to the Web service method.
  • XML Web service methods can only participate as the root object in a transaction, due to the stateless nature of the HTTP protocol. 
     
    Example:

    using System.EnterpriseServices;

    public class Bank : WebService

    {

        [WebMethod(TransactionOption = TransactionOption.RequiresNew)]

        public int DeleteAuthor(string lastName)

        {

            //ContextUtil.SetAbort();

        }

    }

SOAP, HTTP GET, HTTP Post

  • By default, in .NET Framework 1.0, all three protocols are enabled. By default, in .NET Framework 1.1, HTTP GET and HTTP POST are both disabled. This is for security reasons.
  • Only HTTP POST and SOAP care about the Content-Type because they send the data in the body of the HTTP request. The HTTP GET protocol does not really care about the Content-Type because all of the parameters are packaged into the query string.

Create instances in advance to preload all the referenced assemblies at the beginning

Everybody notices that a first call to a web service takes more time to execute than the subsequent calls.
Why is that? Try to run your application in Debug mode and look at the "Output" window of Visual Studio.

  • Once a web method is invoked, the application loads a number of libraries like System.Web.dll, System.Web.Services.dll, System.XML.dll etc. And maybe other assemblies of your own, which describe some classes you, use to work with your web service.
  • This loading takes time, of course. So create an instance of a web service in advance, when your application starts.
  • This loads all the needed libraries in memory and saves time.

Using pre-authentication

  • Any call to a webservice's method generates at least one 401 error (and if you use "integrated Windows authentication", then there might be two or three 401 errors!). To avoid this, set PreAuthenticate to true.
  • But keep in mind that not all authentication methods used by the hosting web servers support pre-authentication if your hosting IIS-server uses "Windows Integrated Authentication", pre-authentication is not possible.
  • To speed-up the web service call, use the "basic" authentication scheme. Use it only with SSL-protected webservices and websites.

Edit IIS custom error messages

  • When something goes wrong, a web server displays an error message. The htm-files containing these messages are located in the c:\WINDOWS\Help\iisHelp\common\ server folder (if not, look at your IIS settings).
  • Edit these files to minimize traffic! Get rid of styles, tables, and other "beauties".
  • There' always a 401 error when a web-service is accessed. Even if PreAuthenticate is set to true, the 401-error will be generated when a Web Service is accessed for the first time. So minimize the 401-files.
  • Error 401.2 - Unauthorized: Access is denied due to server configuration.
  • IIS's default 401-2.htm is 5 kilobytes, but you can change it and it will be only 73 bytes long.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
devanand laroiya
I have been using .NET Framework and the C# language for morr than 3 years. I started with C++. I am working with a MNC company in Gurgaon.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
webservice in windows application by Vipin On December 15, 2008
I want to knw hw is webservice applied in windows applications
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.