Monday, October 1, 2012

Best Practices For WCF

There are few the recommended best practices for WCF, what I found during to resolve the performance issue. Recently one my application (WCF based software) is going into production and client had more concerns and worries about the performance because, this application had to deal with bulk data like million records.

In initial phase of development, we have faced lots of design hurdles (due to n-tired approach with SOA) against the performance.

Since last few days I am working on the performance issue and has already done lots of goggling to improve the performance and finally I found few tips and best practices

  1. Avoid multiple service round trips to populate the screen data, Make single call to get all lookup data for screen instead of making each for each data driven control.
  2. If possible and data size allowed, Use the client side caching for static data (master data).
  3. Use the singleton proxy creation pattern or proxy caching approach to create the service proxy.- avoid to create service proxy for each client request.
  4. Implement the server side caching for static data or lookup data.
  5. Choose the proper wcf binding ( nettcp, webhttp, wshttp etcs)
  6. set the right value of the below service behavior Throttling properties to improve WCF performance 
    • maxConcurrentCalls - This specifies the maximum number of messages processed across the service host. The default value for this property is 16 (WCF 4.0 is improved to default is 16 * Processor Count).
    • maxConcurrentInstances - This specifies the maximum number of instances of a context object that executes at one time with the service. The default is Int32.MaxValue.
    • maxConcurrentSessions - This specifies the maximum number of sessions at one time within the service host object. The default value is 10 (WCF 4.0 increases that to 100 * Processor Count).
   <serviceBehaviors>
        <behavior>
         
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
         
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />         

<serviceThrottling 

maxConcurrentCalls="16" maxConcurrentSessions="100" maxConcurrentInstances="10" /> 

          <serviceMetadata httpGetEnabled="true" />         
        </behavior>
      </serviceBehaviors>


Wcf Proxy Caching - 
To generate or dispose the wcf proxy, both are very expensive task and it consume good amount of resource and time. so avoid to this,we should use singleton pattern to create wcf proxy or cache proxy.

you will be get more details to visit on below link 
http://blogs.msdn.com/b/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx

Thanks for visiting !

Tips To Improve Entity framework Query Performance

In this blog, we will discuss about the few tips and techniques to improve the entity framework query performance

  • Object tracking turned off 
  • Turn off automatic DetectChanges
  • Avoid the Using block
  • Lazy Loading

1. Object tracking turned off :
when we loads entity in local memory; by deafult it tracks all changes of entity like entity property value changes or deletion of entity and this feature is only beneficial when you want to update the existing entity and but if you only want to read data and you dont want to update entity, then it will be additioanl overload and it will degrade the query performace.

AsNoTracking() function is very useful in writing of no tracking query and it executes very fast because there is no need to store any entity change information.

Example  :
   DbContext context = new DbContext()
   var projects = context.Projects
        .AsNoTracking()
        .ToList();


2. Turn off automatic DetectChanges :
if your application context is not making any entity update or its making bulk insert or update then you can turn off the autoDetectChanges but make sure it is happened only in try/cacth-finally scope

Example :
DbContext context = new DbContext()
context.Configuration.AutoDetectChangesEnabled = false;

3. Avoid the Using block  to create the instance of ObjectContext. :
it takes the lots of times to initialization of ObjectContext and during the initialization, it performs the several steps

  •  Initialize Context (needs to load schema/table metadata from resources)
  •  Validate context
  •  Open connection to DB
  •  Execute submitted SQL script 
  •  Clean up and close DB          
so if you are creating object of DbContext by using  'Using' block and if exection control go out scope of using block, it automatcally will dispose dbContext object.

using (var context = new DbContext())
{
      var projects = context.Projects
        .AsNoTracking()
        .ToList();
}

4. Lazy Loading :
use lazy loading feature and avoid the unnecessary object loading and more information @ Entity Framework - Eager Loading 

5.Examine queries sent to the Database - use ObjectQuery.ToTraceString() to get the raw SQL query what EF is sending to database or you can log sql statement and more information @ Entity Framework - Log SQL Statement 

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...