Tuesday, 16 September 2014

Use of the Tuple class in .NET 4.0

Use of the Tuple class in .NET 4.0

Hi Friends,

In this article i will introduce Tuple class and its usage in C#

The Tuple class, which was introduced in .NET 4.0, is handy for passing short-lived objects consisting of multiple related values.
·          
·          
1.What is the Tuple class used for?
The Tuple class is used for short-lived objects consisting of multiple values:
var myTuple = new Tuple<string, int>("Ten", 10);

2.Passing multiple values in a single object
Commonly when you want to pass an object consisting of several values you’d use something like a custom struct or class, or perhaps a generic KeyValuePair object (if you want to pass an object with exactly two data members).
A tuple can contain any number of properties (each generic type parameter results in a read-only property in your tuple object). Each property is automatically namedItem1Item2Item3 etc:
// Group related data
var myName =
new Tuple<string, string, int>("Sujeet "Bhujbal", "SujeetBhujbal".Length);

// Output name info
Console.WriteLine(
   
string.Format(
       
"My full name is {0} {1} which consists of {2} characters, whitespace excluded.",
        myName.Item1,
        myName.Item2,
        myName.Item3));

Tuples are read-only
Tuples are immutable, their properties do not have any setters, so once a tuple is initialized its values cannot be modified:

A tuple is a class, not a struct
This might not seem that important, but it’s still noteworthy that each tuple is in fact aclass, meaning it will have to be garbage-collected. This in contrast to customstructs, or objects such as generic KeyValuePair objects which are created asstructs by the compiler. Of course, the KeyValuePair comparison is only relevant if your tuple consists of exactly two properties.
Fixed Size      
 The size is set at the time of its creation. If it was created with three items, you cannot add new items.
Of heterogeneous objects
           Each item has a specific and independent of the type of the other item.
Disadvantages
As Tuples don’t have an explicit semantic meaning, your code becomes unreadable.


Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Tuesday, 22 July 2014

Parallel programming in depth

Hi Friends,

In this article I will tell you what are parallel programming, how to use it and what the advantages over thread using parallel programming. Parallel programming is introduced in 4.0 frameworks

1.      What is Parallel programming

o   Parallel programming is a technique where we use multiple threads to execute a task faster. This means that on modern multi-core architectures we can utilize more of the resources available to perform a task.
o   A great example of this is sorting a list using quicksort.
o   Normally with parallel programming performance is important and all the threads are working to a common goal.
o   Parallel programming means executing operations at the same time using multiple threads, processes cpu's and or cores.
o   You perform parallel programming by running the same process multiple times, with the difference that every process gets a different "identifier", so if you want, you can differentiate each process, but it is not required.
o   Namespace used for parallel programming is using System.Threading.Tasks;

2.      How to use Parallel programming

The Parallel.For() and Parallel.ForEach() methods make use of a Partitioner. It would be very inefficient to execute a loop over 10,000 elements on 10,000 individual Tasks. The partitioner splits the data in segments and ideally the  ForEach() will execute in 4 Tasks (threads) of 2,500 elements on a 4-core CPU. This sometimes requires some heuristics and you can write your own custom-partitioner.
When using the 'normal' (simple) overloads of ForEach() this is fully transparent. But your example uses one of the <TLocal> overloads that surfaces the partitioning.


There are 3 methods for using parallel programming

o   Parallel.Invoke

Parallel.Invoke accepts an array of action delegates (you can construct the array yourself, or just pass a number of delegates to Invoke), which it then executes in parallel
o   Parallel.For.

The Parallel.For() construct is useful if you have a set of data that is to be processed independently. The construct splits the task over multiple processor.
Normal For loop
for (int row=0; row < ds.Tables[0].Rows.Count; ++row)
{      

}

Parallel.For loop will be
Parallel.For(0, ds.Tables[0].Rows.Count), row =>
{
}
 
 
o   Parallel.ForEach.

The Parallel class’s ForEach method is a multithreaded implementation of a common loop construct in C#, the foreach loop

Syntax:

Parallel.ForEach(TSource, TLocal) Method (Partitioner(TSource), ParallelOptions, Func(TLocal), Func(TSource, ParallelLoopState, TLocal, TLocal), Action(TLocal))
 
Example: We have a collection of Customers, and we want to iterate through each customer then Normal ForEach loop will be
 
foreach(var customer in customers)
{
}
Parallel.foreach loop will be
Parallel.ForEach(customers, customer =>
{
}



3.       Some New concepts in Parallel programming

  •          How to handle Exceptions in Parallel Loops

We can handle all exceptions in parallel programming  using        System.AggregateException.

  •          Break Statement in Parallel Loops

We can use ParallelLoopState.Break method for breaking loop
Example :
Parallel.ForEach(list,
    (i, state) =>
    {
       state.Break();
    });
 
  •          Stop Statement in Parallel Loops

We can use ParallelLoopState.Stop method for Stopping loop

Parallel.ForEach(integers, (int item, ParallelLoopState state) =>
{
     if (item > 5)
     {
             Console.WriteLine("Higher than 5: {0}, exiting loop.", item);
             state.Stop();
     }
     else
     {
             Console.WriteLine("Less than 5: {0}", item);
     }
});


  •          How  to Exit from Parallel Loops Early


We can exit from parallel loop early by passing CancellationToken
Explicitly. Cancellation is supported in parallel loops through the new System.Threading.CancellationToken type introduced in .NET 4.0.


  •          ParallelOptions Class in Parallel Programming


There are 3 properties in ParallelOptions Class
o   CancellationToken Gets or sets the CancellationToken associated with this ParallelOptions instance.
o   MaxDegreeOfParallelism             Gets or sets the maximum number of concurrent tasks     enabled by this ParallelOptions instance.
o   TaskScheduler   Gets or sets the TaskScheduler associated with this ParallelOptions instance. Setting this property to null indicates that the current scheduler should be used.

  •         Limit the number of parallel threads in C#


We can limit the number of parallel threads using MaxDegreeOfParallelism clss
Example
 ParallelOptions options = new ParallelOptions();
 
options.MaxDegreeOfParallelism = 4;


4.      Advantages of Parallel programming over thread

    •   Most applications can benefit from implementing some form of Data Parallelism.  Iterating through collections and performing “work” is a very common pattern in nearly every application.
    •   Suppose if you are running long method then you can send that particular method in the thread and you don’t  need to wait until that method is executed. 
    • Performance of the application is very fast


5.      Disadvantages of Parallel programming

    •      It’s a fire and forgets kind of mechanism.
    •       We don’t know when we get the result back.
    •    As per my knowledge its better to use in a dropdownlist. For example: In the long filling info such as Signup.



Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Friday, 23 May 2014

WCF: The maximum message size quota for incoming messages (65536) has been exceeded


Hi Friends,


Problem: 

Recently working on one WCF project we found one issue when we are reading PDF file from SERVER using WCF. There is error like

System.ServiceModel.CommunicationException : The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.


Solution:

To resolve this problem we increased MaxReceivedMessageSize in app.config.
We need to increase the value of two limiting parameters: maxBufferSize and maxReceivedMessageSize. In my example these values are set to 2147483647.
For max file size in WCF, we need to set following parameter in binding files.

MaxBufferPoolSize  gets or sets the maximum size of any buffer pools used by the transport.  The default is 524,288 bytes.

MaxReceivedMessageSize gets and sets the maximum message size that can be receivied. The default is 65536 bytes.

ReaderQuotas: Defines the contraints on the complexity of SOAP messages that can be processed by endpoint configured with a binding. You need to set the maxBytesPerRead, maxDepth, maxNameTableCharCount,maxStringContentLength.

<binding name="MyService"
       maxReceivedMessageSize="2147483647"
       maxBufferSize="2147483647" transferMode="Streamed" >
       <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
     maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
<!-- other binding info here, such as security -->
</binding>


Max possible size: MaxReceivedMessageSize to 2147483647 (i.e. Int32.MaxValue), which is 2GB

Please Note:

1.       But it’s recommended that the file size should be less, as the data is transferred through network there is possibility of slow network performance.
 
 2.       We can set it to the maximum value, which is 2GB (2147483647) - or if we’re using streamed transfers we can go up to 263 bytes (almost 10,000,000 terabytes). But remember that by setting it to a value larger than necessary we may be opening our service (or client) to attacks.
3.       Only if the service is expected to receive messages larger than 65536 bytes. And in this case, we’d be increasing MaxReceivedMessageSize on the endpoints where we expect to receive such big messages.
4.     If the messages that our client/servers expect to receive are larger than the default value for MaxReceivedMessageSize, then we need to increase this quota.

Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------






Wednesday, 30 April 2014

What's New in WPF 4.5


Hi Friends,

In this article I will tell  what are the new features added in WPF with 4.5
 
·         Ribbon control
o   WPF 4.5 added with a Ribbon control that hosts a Quick Access Toolbar, Application Menu, and tabs.

·             Improved performance when displaying large sets of grouped data

·             New features for the VirtualizingPanel

·             Binding to static properties
o   You can use static properties as the source of a data binding. The data binding engine recognizes when the property's value changes if a static event is raised

·             Accessing collections on non-UI Threads
o   WPF enables you to access and modify data collections on threads other than the one that created the collection. This enables you to use a background thread to receive data from an external source, such as a database, and display the data on the UI thread. By using another thread to modify the collection, your user interface remains responsive to user interaction.


·             Synchronously and Asynchronously validating data

·             Automatically updating the source of a data binding

·             Binding to types that Implement ICustomTypeProvider

·             Retrieving data binding information from a binding expression

·             Checking for a valid DataContext object

·             Repositioning data as the data's values change (Live shaping)

·             Improved Support for Establishing a Weak Reference to an Event

·             New methods for the Dispatcher class
The Dispatcher class defines new methods for synchronous and asynchronous operations.

·             Markup Extensions for Events
o   WPF 4.5 supports markup extensions for events. While WPF does not define a markup extension to be used for events, third parties are able to create a markup extension that can be used with events.



Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------

----------------------------------------------------------------------------------