Wednesday, December 22, 2010

Guidelines for number of application pools for IIS 6

Hi,
Was doing some exploration in terms of the number of application pools for any web server.
Here goes the collection of results.
1) It is good to have one application pool / web application
2) It is also a good practice to run each one of that application pool by a unique identity.
a) In this case we need to ensure that it is tied up to IIS_WPG group.

By doing this we will be benefitted both from security and functionality. It also ensure reliability and availability.Since an issue/error with one application wont cause any side effects to the other application.

I hope this helps!.

Regards,
-Vinayak

Tuesday, December 21, 2010

Code generator for Data Transfer Objects

Reference,
http://www.codeproject.com/KB/vista/Code_Generator.aspx

FIle splitter & Builder

Reference,

http://www.codeproject.com/KB/cs/File_Splitter___Builder.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62047/Refactoring-Tips-Tip-1.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62190/Refactoring-Tips-Tip-5.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62441/Refactoring-Tips-Tip-9.aspx

Refactoring Tips

Reference,

http://www.codeproject.com/Tips/62058/Refactoring-Tips-Tip-4.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62219/Refactoring-Tips-Tip-8.aspx

Refactoring Tips

Reference,

http://www.codeproject.com/Tips/62215/Refactoring-Tips-Tip-7.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62209/Refactoring-Tips-Tip-6.aspx

Refactoring Tips

Reference,

http://www.codeproject.com/Tips/62050/Refactoring-Tips-Tip-3.aspx

Refactoring Tips

Reference,
http://www.codeproject.com/Tips/62048/Refactoring-Tips-Tip-2.aspx

.NET Coding Best Practices

Reference,
http://www.codeproject.com/Tips/48526/NET-Coding-Best-Practices.aspx

Extension points for ADO.NET Entity Framework

Reference,

http://www.codeproject.com/Tips/49859/Extension-points-for-ADO-NET-Entity-Framework.aspx

About Entity Framework

Reference -
http://www.codeproject.com/Tips/49178/About-Entity-Framework.aspx

Entity Framework 4 twist in Complex Type and stored procedure

Hi,

I was exploring a bit on new features of EF4. especially from the aspect of Complex types and storedprocedure.A case where you need to be very careful is as follows:A storedprocedure returns the result set which exactly matches Entity Object of EDM(Entity Data Model).From the wizard if you choose the return type as Entity and map to the respective Entity Object from the drop down of the wizard everything works fine, but the twist is that - if in the EDM if you create a complex type on the Entity Object, and then if you map this(Entity Object with Complex type property) to the return type of stored procedure - You will get the Invalid Operation Exception.Hence the suggestion is that - you create the new complex type to work aroundI hope this will be fixed with the next patch /release...

I hope this heps!.

Regards,

-Vinayak

Best Strategy for deploying ASP.NET application and application Pool

Hi,

I was exploring some aspect of deployment with respect to the asp.net application and application pool.

So the conclusion is :If you want to share the security context, deploy them under the same application pool.With this setup, Keep in mind that by default you will get only 25 threads/cpu. If one collapses, all in that pool will get collapses.Hence If your priority is for availability ,reliability and cpu bound then the best practice could be to Segregate the application( one application/pool).

I hope this helps!.

Regards,

-Vinayak

Hi,
I was exploring the options to scaleout the asp.net session.Here goes the results.

Different modes of storing the session

) InProc
2) Out Proc
a) ASP.NET State Service
b)Sql Server
Yes we all know that when comapared to the Asp.net State service option with Sql Server, Sql server scales/available very well at the expense of performance.But if we have an option where in 2 servers are available(with enough memory and a bit of relaxation in terms of availability requirement), then we can think of partitioning the session data across those 2 boxes.(intermediate scaling option with asp.net state service) so that availability is the sum of 2 boxes. for instance if one of the two boxes is down then the user whos session data is stored in the other machine will be served without any interruption..
It can be achieved with the help of interface - IPartitionResolver and few configuration entries(PartitionResolverType)
For more info on using this session partition refer to msdn.
http://msdn.microsoft.com/en-us/library/system.web.ipartitionresolver.aspx

I hope this helps!.
Regards,
-Vinayak

Twist in Performance counters for Connection pool settings

Hi,
Last week i was doing a bit search in terms of connection pooling behavior. Hence i wanted to set the counters related to connection pooling.

Initially i set all the counters from the '.Net CLR Data' category. and at the end i was surprised that, it was not capturing anything. Then i did some more investigation and found that those counters are only for .Net 1.0 and 1.1 and Microsoft kept those set of counters for backward compatability.

For ADO.NET 2.0 those counters wont work, and MS added a new set of counters under '.Net Data provider for SQLServer' - for SQL server and '.Net Data provider for Oracle' - for Oracle database

Hence be proactive, before setting the counters related to connection pool.

More detailed information about this you can find at
http://msdn.microsoft.com/en-us/library/ms254503.aspx
http://blogs.msdn.com/b/angelsb/archive/2004/08/02/206470.aspx

I hope this helps!.

Regards,
-Vinayak

Probable leak while using SqlDataReader in ADO.NET

I was doing a bit of exploration on Connections with ado.net.While doing this exercise i realized that when using CloseConnection along with the reader i was under the impression that it closes by itself but actually it wont close the connection. I had pasted the code sample below which i executed during the test.

while (true)
{
Thread.Sleep(2000);
SqlConnection objConnection = default(SqlConnection);
objConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=.;Max Pool Size=1");
SqlCommand objCommand = new SqlCommand("Select * from customers", objConnection);
objConnection.Open();
SqlDataReader reader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (reader.Read()) { }
Console.WriteLine((i++).ToString());
}

So the tip is either create the sqlconnection object by using - using syntax (which ensures that it Disposes connection Object) or after the execution before leaving make a call to explicitly close the connection.

This ensures that connection will be closed for sure. Hence no leak

I hope this helps!.

Regards,
-Vinayak

Tweaking ASP.NET Memory settings

Hi,
I was doing a bit of experiment on contribution of ASP.Net Memory configuration for out of memory exception.(By refering mutilple papers)At the end of my experiment i was amazed by the way how the ASP.Net engine is selecting the value for its runtime.
Here are the list of items i had added based on the findings for all of our references.1)
It basically looks at 2 entries for memory settingsa) Entry at the process model(memory limit)b) Entry at the Cache settings( private byte Memory Limit)
Lets say, if both of them are not set, then how it picks up the entry is quite interesting..Any guess...It looks at the system /OS (32 or 64 bit) ,If it is 32bit and if my virtual address space of the process is upto 2GB - it choose the limit as 800 MB
If the os is of 32 bit and If the virtual address space limit is up to 3GB - then it choose 1800 MB
If it is 64 bit, then it takes 1TB as the limit
All the above input is gathered from msdn blog...
However if the system is low in memory, lets say i have just 500 MB of physical Ram, then the memory setting value is based on the least of above conditional result and 60% of 500 MB (which ever is the least , it takes that value), you might be wondered from where did i got 60% - this is the default setting about the memory limit exist in machine config file.
Now lets say - you had set memory values both at memory and cahce level(as i had mentioned earlier) then it takes which ever is the least among.

Last but not the least, w3wp.exe can't exist alone in any machine , it has to work/exist with other process as well. Now one more interesting entry at the config which plays a major role in determing the limit for .Net memory when total system memory is low.which we can configure at Cache entry( Percentange Physical memory used Limit), based on this percentage w3wp's appdomain will get recycled....

formula for calculation of % limit = (total memory - used memory)/total memory....

I hope this helps!
Regards,
-Vinayak

Setting the best value for Application pool Queue length limit

Hi,
I was doing some experiment in finding the best limit/setting for the application queue limit in IIS....and here goes the results( I found this at Administrators pocket guide for IIS 6)

Formula for calculation :
Application pool Queue Length limit = ( Available memory in MB * number of processors * 10 ) / ( total number ofapplication pools).
for eg: let us say that i have 10 applications ( i mean 10 application pool, one to each application) and available memory 500 MB, and machine has Core 2 duo processor , then the good number for Application queue length limit is500 * 2 * 10 / 10 = 1000
A small twist, let us say your machine has only one application in the previous case, then you shouldnt use the above formula for calculation If you use you will go for a toss by setting 10000
I hope this helps!.
Regards,
-Vinayak

Coding Best practices

Last week i was trying to explore how the if statement performs under various conditions as shown below.

if( statement 1)
{
if( statement 2)
{ if( statement 3)
{ ...... }
}
}

VS

if( statement 1 && statement 2 && statement 3){ .....}


The results indicate that, the second approach is far better than the first one.Hence, the recommendation would be to use the second approach over the first one
I hope this helps!.
Regards,
-Vinayak

Performance tips

Hi All,

Last week i was doing some exploration on performance of if statements vs switch statements and surprisingly i found that switch is more performance intent than the if statements.Hence the recomendation would be to prefer swicth over if statements.

I hope this helps!.
Regards,
-Vinayak

Coding Best practice - 3

There are 2 modes by which the CLR emits the JIT code while executing static type constructor
1) Precise mode
2) Before - field - Init ModeIn Precise mode - the code will be Jitted just beofe the first initialization/access call to a static variable
In Before - field - Init Mode - it will be Jitted during the first instance creation of the type Hence, Before field init mode is much more performant intent than the precise mode,as shown below

public class BeforInitMode
{
static int i = 20;
}

In before init Mode - type constructor is jittted much before the static member variable initialization/access

public class PreciseMode
{
static int i;
static PreciseMode()
{ i = 20;
}
}
Ref: CLR via C# by Jeffrey
I hope this helps!.
Regards,
-Vinayak

ASP.NET Best practices - 2

ViewState and Server.Transfer Best practices

Scenario :

Server.Transfer("destiny.aspx",true);
Above statement will cause ViewState is invalid exception

Reason :By default EnableViewStateMac is set to true, and hence on every page life cycle , it tries to validate the value of the ViewState(for security reason) ,which is a part of forms collection(__ViewState - hidden field) and by ViewState design at any given point you can post to only one form...Hence in the prior case , it tries to compare the ViewState of the source with the destiny and hence the above mismatch happens Best practices to Transfer the control to the other page without affecting the view state

Option 1

Server.Transfer("destiny.aspx",false);

Option 2

Passing Server control value between pages
http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx

I hope this helps!.

Regards,
-Vinayak

ASP.NET Best practices - 1

Page Transfer best practices in ASP.NET.Scenario :Inside Try/Catch block, If you want to transfer/Redirect the page to another page - To avoid ThreadAbortException
Option 1
Response.Redirect("Pagetotransfer.aspx",false);

Option 2
Server.Execute("Pagetotransfer.aspx");

Option 3

try{
Response.Redirect("Pagetotransfer.aspx");
}
catch(ThreadAbortException ex)
{
}

The preffered way of doing is option 1 or 2. Since it eliminates Exceptions.
I hope this helps!.
Regards,
-Vinayak

Coding best practices - 2

Refactoring 'OR' type of conditions - In a method,if we have multiple conditional statements,with the same result. apply this rule
Bad Practice
private int Method()
{
int result = 1;
if( condition1)
result = 0;
if( condition2)
result = 0;
if( condtion3)
result = 0;
return result;
}

Best Practice

private int OrCondition()
{
if( condition1 condition2 condition3)
return 0;
else
return 1;
}
private int Method()
{
return OrCOndition();
}

I hope this helps!.

Regards,
-Vinayak

Coding Best Practices - 1

If the derived classes(or concrete/specialized classes) cant make use of all the functionalities (100% usage) of its base class(though it is a 'is a kind of' relationship among them, then prefer delegation/composition over Generalization/inheritance.

//Bad Practices
public abstract class BaseClass
{
abstract void Method1();
abstract void Method2();
//Some more methods....
}
public class Derived : BaseClass
{
public override void Method1() { //Some processing... }
public override void Method2() { //Actually this derived class doesnt need this method2...but since it is abstract in its base class, i have to override to getout of compilation issues... }
//Some more methods...
}

Best Practice

public class BaseClass
{
public void Method1() { //Some processing }
public void Method2() { //Some processing.. } //Some more methods....
}
public class Derived
{
private BaseClass objBase;
public Derived():this(new BaseClass()) { }
public Derived(BaseClass obj) { objBase = obj; }
public void Method1()
{
objBase.Method1(); //If needed some more derived class processing... }
//Some more methods...
}

I hope this helps!.

Regards,
-Vinayak

How to decide where to fit in a particular method inside a class

Hi All,

I think the title is quite interesting/confusing. Yes indeed!. Often from my experience i had seen people are getting confused while deciding about a particular methods place inside a particular class..

let me be little deep in what i am syaing. for instance you have 3 classes i.e. customer, order and Product.. and now you want to get all the orders placed by a customer. The question comes where i need to write the method..do i need to write inside customer or order....

Thumb rule - It purley goes based on the return type... in this case since i want to return all the orders placed by customer, so GetOrdersByCustomer() sits inside the Orders class...since i am returning Orders.

In the similar line, if i have to place an order(where in i am not returning anything), then it purly goes by the input .. ie. since i am placing an order..PlaceOrder() should be there inside Order class.

I hope this helps!.

Regards,
-Vinayak

Designing Data Access Layer

Hi Guys,

Last week i was exploring on some of the design aspects with respect to the Data Access Layer.

Here goes the observation and findings of those analysis...

1) For instance if i have to design a Data access layer ,where in i have to deal with Customers, Products and Customer orders. ie. One customer can place many orders and one order can have many products.

For the above case, one approach could be to have a single big file repesenting the Data Access Layer, but if we go with this approach you will end up in
1) maintenance issue
2) It is tough to handle transactions, if we go bigger. ie. if we want to go ahead with multitier deployment ,where to handle transaction ? becomes an issue , since all the code blocks of entities will be there in one single data access layer.

Hence the suggestion would be to have multiple data access components. for instance for the above case, we should have 3 data access components.
1) customer data access
2) Product
3) Order data access.

if we follow this approach , ultimatley all the things will fall in place quite easily or unknowingly.

ie if we want to achieve any functionality where in 3 components has to execute together, then you will end up writing a Business processing component on top of these 3 calls and eventually this component will be inside the Business layer and it becomes the root for the transaction.....

I hope this helps.

Regards,
-Vinayak