Part 5: WCSF : Power of [ServiceDependency] attribute

In this part I would like to demonstrate the power and advantage of [ServiceDependecy] attribute in WCSF framework.

Advantage

We can restrict the instance creation of classes and make reuse a single instance across page requests. This would drastically decreases memory utilization and simultaneously reduces the instance management code and makes the easier.

You can use the following class types to be shared using [ServiceDependency]

Data Access Classes

Web Service Classes

Utility Libraries

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GeneralLibrary

{

publicclassUtility

{

privatestaticint _instanceCount;

public Utility()

{

_instanceCount++;

}

publicstring GetMessage()

{

returnstring.Format("Instance Number: " + _instanceCount.ToString());

}

}

}

We can open the previous project and work upon it.

After opening the WebClientApplication1, create a new Class Library project into it. Let us call it GeneralLibrary. After creating the project, add a class named Utility.cs into it. The code should be:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GeneralLibrary

{

publicclassUtility

{

privatestaticint _instanceCount;

public Utility()

{

_instanceCount++;

}

publicstring GetMessage()

{

returnstring.Format("Instance Number: " + _instanceCount.ToString());

}

}

}

Basically, the above class will be counting the instances of itself using a static counter and it is exposing a method named GetMessage() that will give us information on number of instances created.

Creating the test infrastructure

For calling the above method we need to place a button and label on the MyData.aspx. Rename the label to InfoLabel.