Search This Blog

Sunday, May 29, 2011

Some Important Tips on .Net ThreadPool...

As we will be familiar with QueueUserWorkItem method of thread pool and which will add your method to the thread pool and will start a background worker thread for execution...
                       Here I am explaining some of the other useful methods in Thread pool which can be useful in some of the programming scenarios...
                     
                     1. Maximum Number of Thread Pool Threads: You can control the maximum number of threads that can active in a threadpool by using the GetMaxThreads and SetMaxThreads methods. Once it reached the maximum limit, the upcoming equests will be queued into threadpool and will executed once the pool is available.
                      
                     2. Thread Pool Minimums : The thread pool provides new worker threads or I/O completion threads on demand until it reaches a specified minimum for each category. You can use the GetMinThreads method to obtain these minimum values.

                    3. RegisterWaitForSingleObject : Registers a deligate for a waithandle . The deligate will be executed in a worker thread on signaling the waithandle. When you are finished using the RegisteredWaitHandle that is returned by this method, call its RegisteredWaitHandle.Unregister method to release references to the wait handle. We recommend that you always call the RegisteredWaitHandle.Unregister method, even if you specifytrue for executeOnlyOnce. Garbage collection works more efficiently if you call the RegisteredWaitHandle.Unregister method instead of depending on the registered wait handle's finalizer.
                       The RegisterWaitForSingleObject method queues the specified delegate to the thread pool. A worker thread will execute the delegate when one of the following occurs:
              => The specified object is in the signaled state.
              => The time-out interval elapses.

                   The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. If the timeOutInterval parameter is not 0 (zero) and theexecuteOnlyOnce parameter is false, the timer is reset every time the event is signaled or the time-out interval elapses.

Tuesday, May 17, 2011

Managed Extensblity Framework <=> MEF

                 The Managed Extensibility Framework (MEF) is a new library in the .NET Framework 4 that helps you build extensible and composable applications. MEF enables you to specify points where an application can be extended, to expose services to offer to other extensible applications and to create parts for consumption by extensible applications. It also enables easy discoverability of available parts based on metadata, without the need to load the assemblies for the parts.

WHAT MEF Provides
               Instead of this explicit registration of available components, MEF provides a way to discover them implicitly, via composition. A MEF component, called a part, declaratively specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available. When a part is created, the MEF composition engine satisfies its imports with what is available from other parts.
             This approach solves the problems discussed in the previous section. Because MEF parts declaratively specify their capabilities, they are discoverable at runtime, which means an application can make use of parts without either hard-coded references or fragile configuration files. MEF allows applications to discover and examine parts by their metadata, without instantiating them or even loading their assemblies. As a result, there is no need to carefully specify when and how extensions should be loaded.
           In addition to its provided exports, a part can specify its imports, which will be filled by other parts. This makes communication among parts not only possible, but easy, and allows for good factoring of code. For example, services common to many components can be factored into a separate part and easily modified or replaced.

MEF and MAF
           Previous versions of the .NET Framework introduced the Managed Add-in Framework (MAF), designed to allow applications to isolate and manage extensions. The focus of MAF is slightly higher-level then MEF, concentrating on extension isolation and assembly loading and unloading, while MEF's focus is on discoverability, extensibility, and portability. The two frameworks interoperate smoothly, and a single application can take advantage of both.

1.  MEF parts are created with "export" keyword
2. Clients of the parts will use the "import" keyword
3. Composition containers are used to discover the available parts ...
4. Use "ImportMany" attribute to import more than one types, here all the types mentioned will be imported. Still we can use the "Lazyimport" to create only the required types.
5. If we have many implimentations for a single interface and from client side we can mention which interface implimentation we want to use by mentioning the metatag values. Metatag will me specified for each interface types. 

The following example shows how to export and import types in MEF.


namespace SimpleCalculator3
{

    public interface ICalculator
    {
        String Calculate(String input);
    }

    public interface IOperation
    {
        int Operate(int left, int right);
    }

    public interface IOperationData
    {
        Char Symbol { get; }
    }

    [Export(typeof(IOperation))]
    [ExportMetadata("Symbol", '+')]
    class Add : IOperation
    {
        public int Operate(int left, int right)
        {
            return left + right;
        }
    }

    [Export(typeof(IOperation))]
    [ExportMetadata("Symbol", '-')]
    class Subtract : IOperation
    {

        public int Operate(int left, int right)
        {
            return left - right;
        }

    }



    [Export(typeof(ICalculator))]
    c\lass MySimpleCalculator : ICalculator
    {
        [ImportMany]
        IEnumerable<Lazy<IOperation, IOperationData>> operations;

        public String Calculate(String input)
        {
            int left;
            int right;
            Char operation;
            int fn = FindFirstNonDigit(input); //finds the operator
            if (fn < 0) return "Could not parse command.";

            try
            {
                //separate out the operands
                left = int.Parse(input.Substring(0, fn));
                right = int.Parse(input.Substring(fn + 1));
            }
            catch
            {
                return "Could not parse command.";
            }

            operation = input[fn];

            foreach (Lazy<IOperation, IOperationData> i in operations)
            {
                if (i.Metadata.Symbol.Equals(operation)) return i.Value.Operate(left, right).ToString();
            }
            return "Operation Not Found!";
        }

        private int FindFirstNonDigit(String s)
        {

            for (int i = 0; i < s.Length; i++)
            {
                if (!(Char.IsDigit(s[i]))) return i;
            }
            return -1;
        }


    }


    class Program
    {
        private CompositionContainer _container;

        [Import(typeof(ICalculator))]
        public ICalculator calculator;


        private Program()
        {
            //An aggregate catalog that combines multiple catalogs
            var catalog = new AggregateCatalog();
            //Adds all the parts found in the same assembly as the Program class
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
            catalog.Catalogs.Add(new DirectoryCatalog("E:\\Projects\\MEF\\SimpleCalculator\\SimpleCalculator\\CS\\SimpleCalculator3\\Extensions"));


            //Create the CompositionContainer with the parts in the catalog
            _container = new CompositionContainer(catalog);

            //Fill the imports of this object
            try
            {
                this._container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                Console.WriteLine(compositionException.ToString());
            }
        }


        static void Main(string[] args)
        {
            Program p = new Program(); //Composition is performed in the constructor
            String s;
            Console.WriteLine("Enter Command:");
            while (true)
            {
                s = Console.ReadLine();
                Console.WriteLine(p.calculator.Calculate(s));
            }
            

        }
    }
}




In the above example composition container will look for types in the extensions folder. SO if we want to extend the functions, create  new dll with the required functions and interface and just dump into the folder. Clients will discover and will use it runtime. So the extensions will happen more independently with out any other reworks. Thts what "MEF"................

Friday, May 6, 2011

.Net Framework 4.0 - Parallel Programing Pattern

In the .NET Framework 4, a slew of new support has been added to handle common needs in parallel programming, to help developers tackle the difficult problem that is programming for multi-core and manycore...


IMPLEMENTING A PARALLEL LOOPING CONSTRUCT => Parallelizing For Loop..
                         The .NET Framework 4 provides this in the form of the static Parallel class in the new System.Threading.Tasks namespace in mscorlib.dll. The Parallel class provides just three methods, each with several overloads. One of these methods is For, providing multiple signatures, one of which is almost identical
to the signature for MyParallelFor shown previously:
The following example shows you both sequential and parallel execution of a For loop.
      
SequentialProccessing()
{ 
    Camera camera = scene.Camera;
    for (int y = 0; y < screenHeight; y++)
    {
        int stride = y * screenWidth;
        for (int x = 0; x < screenWidth; x++)
        Patterns of Parallel Programming Page 15
        {
        Color color = TraceRay(
        new Ray(camera.Pos, GetPoint(x, y, camera)), scene, 0);
        rgb[x + stride] = color.ToInt32();
        }
    }
}

void RenderParallel(Scene scene, Int32[] rgb)
{
    Camera camera = scene.Camera;
    Parallel.For(0, screenHeight, y =>
    {
        int stride = y * screenWidth;
        for (int x = 0; x < screenWidth; x++)
        {
            Color color = TraceRay(
            new Ray(camera.Pos, GetPoint(x, y, camera)), scene, 0);
            rgb[x + stride] = color.ToInt32();
        }
    });
}

foreach (var student in students)
{
    student.GradePointAverage =
    student.Tests.Select(test => test.Grade * test.Weight).Sum();
}

To parallelize this, we take advantage of Parallel.ForEach:

Parallel.ForEach(students, student =>
{
    student.GradePointAverage =
    student.Tests.Select(test => test.Grade * test.Weight).Sum();
});
                               The return value ParallelLoopResult will give informations about each loop exeutions and also we can use AggregateExceptions to aggregate the exceptions happened each looping. Loopstate can be used to check weather a loop is in running state or in breaked and other loops can use the same for checking the status of other loops.


PARALLEL.INVOKE
               As shown previously, the Parallel class provides support for delightfully parallel loops through the Parallel.For and
Parallel.ForEach methods. Parallel also provides support for patterns based on parallelized regions of code, where every statement in a region may be executed concurrently.


Syntax : static void Invoke(params Action[] actions);


Parallel.Invoke(  () => ComputeMean(),
                  () => ComputeMedian(),
                  () => ComputeMode());



Here the above 3 methods will be executed in parallel.. 
Invoke will will execute methods asynchronously and wont wait for the methods to finish. Stillwe have options to wait for the delegates to wait . We can make use of the Task class for thisas below:

        static void Invoke(Action[] funs)
        {
            var tasks = (from function in funs
                         select Task.Factory.StartNew(function)).ToArray();            
Task.WaitAll(tasks);
        }
WaitAll will block untill all the tasks are finished...

Aggregation:                                 If the tasks that we executes in parallel  generates individual outputs, then the ultimate output will be the aggregation of all these outputs So here we have to consider how we can aggrgate the thread outputes toa  single output . Here we have 2 scenarios :
  1. Each thread will create output and collect and show the ouput( like in a list )
  2. A single output - which will be manipulated from the individual thread outputs. 

                       In the first case, to decrease these overheads and to minimize contention, the new thread-safe collections in the these collections reside in the System.Collections.Concurrent namespace, and are engineered to be scalable, minimizing the impact of contention. Some of these collections are implemented with lock-free techniques, while others are implemented using fine-grained locking. The following example shows the same:

       //Current way of collecting the output
        var output = new List<TOutput>(); 
        Parallel.ForEach(input, item => 
        { 
            var result = Compute(item); 
            lock (output) output.Add(result); 
        }); 

      
      //Using ConcurretnBag....
      var output = new ConcurrentBag<TOutput>(); 
      Parallel.ForEach(input, item => 
     { 
          var result = Compute(item); 

         output.Add(result); 
     }); 


PLINQ AGGREGATIONS - Like Linq, .Net4 introduces Plinq expressions as follows: 

var output = input.AsParallel() .Select(item => Compute(item)) .ToList();

In fact, not only does PLINQ handle all of the synchronization necessary to do this aggregation safely, it can also be used to automatically regain the ordering we lost in our parallelized version when using
Parallel.ForEach: 

var output = input.AsParallel().AsOrdered() .Select(item => Compute(item)) .ToList();
                   Single value Aggregations ( where we need a single output) can be obtained with Plinq as follows:

static double SerialLinqPi() 
{ 

    double step = 1.0 / (double)NUM_STEPS; 

    return Enumerable.Range(0, NUM_STEPS).Select(i => 

    { 

        double x = (i + 0.5) * step; 

        return 4.0 / (1.0 + x * x); 

    }).Sum() * step; 

} 

With a minimal modification, PLINQ can be used to parallelize this: 


static double ParallelLinqPi() 

{ 

    double step = 1.0 / (double)NUM_STEPS; 

    return ParallelEnumerable.Range(0, NUM_STEPS).Select(i => 

    { 

        double x = (i + 0.5) * step; 

        return 4.0 / (1.0 + x * x); 

    }).Sum() * step; 

}

Sunday, May 1, 2011

Windows Communication Framework - Basics


The below is my conclusion regarding wcf, may help you in starting developing wcf applications...


System.ServiceModel

Service => is a dll and have
{
service contracts => The interface that client can expose
operation contracts => the methods that client can expose
data contracts => the data types that will be transferred b/w client and service
Member contracts => the data members that will be transferred b/w client and service
}

Serivice => is described by the endpoints and a service can have multiple endpoints

EndPOint (ABC)=> A (Address), B(Binding/), C(Contract)
  refer http://www.wcftutorial.net/EndPoint.aspx for more info...


Visula Studio Templates

=> WCFClass Library ( it is service library)
=> WCF Console Application ( It will have service host application + service library)

See how simple it is in WCF. Microsoft is making everything simple. Cording to its scope: common behaviors affect all endpoints globally, service behaviors affect only service-related aspects, endpoint behaviors affect only endpoint-related properties, and operation-level behaviors affect particular operations.

Service Configuration:
To configure A Service in any application’s config file:
1. Add End Points for the service (Address(relative), Binding, and Contract)
2. Add a Base Address for the Service Endpoints( under “host” tag)
3. Add a service behavior ( with meta data access settings) and set this to the above added service
4. Add one more endpoint for Meta Data Exchange with contract as “IMetaDataExchage”

Main Contracts
http://www.wcftutorial.net/Contracts-and-Service-Host.aspx

 => Message Contracts
 => Fault Contracts
Service needs to be hosted in an Application, and when the host application starts running, clients can access the service.

Microsoft Tools for service hosting and testing:
1. Wcfsvchost => tool for hosting the service
2. Wcftestclient => will expose the service contract and will test the operations.

Service Hosting:
1. Self Hosting => Create an exe with the service reference ( service code here itself). Make sure App.config file have all details( Metadata informations). Use ServiceHost to host the service( will bring into memory) when the exe starts running.

2. IIS Hosting => create a wcf website ( which will give .svc file). Put the folder into IIS folder( through virtual directory). From IIS you can open the svc file in browser.

3. WAS

4. Windows Service Hosting

Service Clients:
Service is hosted, now we need to implement the proxy class for the client. There are different ways of creating the proxy
Using SvcUtil.exe, we can create the proxy class and configuration file with end points.
Adding Service reference to the client application.
Implementing ClientBase<T> class - Here we need to implement the interface with generic parameter T as the service contract. This proxy class will implement all methods. Client will use this proxy class

*Note => the first 2 mechanisms, need to update if service got updated. But if we are using clientbase proxy class, the proxy no need to updated if service got updated, so this is the best method for the client to access service.

1. Add the service reference and get the proxy class for the service contract.

2. Right click the service reference to see the service details and the proxy class generated.

3. Use the proxy to access the services

Callbacks and Delegates

Call backs can be implemented in 3 ways using delagates
1. Using events. Raise the event on function completion and handler will be executed.
2. Passing a callback delegate object with the function call.
3. Using AsyncCallBack Delegate.

1. Callback Methods
You accomplish this goal of delegating work and being called back when it is done with a callback, which you implement with a delegate. The .NET Framework provides a callback mechanism by defining the ASyncCallBack delegate:

public delegate void AsyncCallback(IAsyncResult ar);

Step1. While invoking the method using begininvoke => provide the above AsynCallback delegate object
Step2. When the delegate method finished, CLR will call the callback delegate method with the AsynResult parameter
Step3. In the callback method get the invoking delegate object from the parameter(AsyncState ) and call EndInvoke of the invoking delegate object, which will return you the desired result.


2. Events:
=> Using delegate we can simulate the event scenarios as below:
1. Event Source will define the delegate and delegate object
2. Event subscribers will add its handler to the above delegate object.( either by ‘=’ or ‘+=’)
3. When the source invokes the delegate object, all handlers will be executed.
refer http://en.csharp-online.net/Delegates_and_Events%E2%80%94The_event_Keyword for more info.

Drawbacks: =>
 1. If you write “=” instead of “+=”, the multicast feature will be overrided and only one handler will be executed
 2. Anybody can raise the event by creating the event Source object and call the delegate object.

So the above drawbacks are eliminated by “event” keyword. So just introduce the keyword while creating delegate object, so which will be treated as event object and above 2 cases won’t be allowed.

3. Anonymous Methods

Anonymous methods allow you to pass a code block rather than the name of the method. This can make for more efficient and easier-to-maintain code, and the anonymous method has access to the variables in the scope in which they are defined:
clock.OnSecondChange += delegate( object theClock, TimeInfoEventArgs ti )
{
Console.WriteLine( "Current Time: {0}:{1}:{2}",
ti.hour.ToString(),
ti.minute.ToString(),
ti.second.ToString() );
};
Notice that instead of registering an instance of a delegate, you use the keyword delegate, followed by the parameters that would be passed to your method, followed by the body of your method encased in braces and terminated by a semicolon.
This "method" has no name, hence it is anonymous. You can invoke the method only through the delegate; but that is exactly what you want.