Search This Blog

Wednesday, January 21, 2015




A delegate (known as function pointer in C/C++) is a references type that invokes single/multiple method(s) through the delegate instance. It holds a reference of the methods. Delegate types are sealed and immutable type.
A delegate provides a way to encapsulate a method. Delegate type as being a bit like an interface with a single method. It specifies the signature of a method, and when you have a delegate instance, you can make a call to it as if it were a method with the same signature.
A delegate is a C# language element that allows you to reference a method. If you were a C or C++ programmer, this would sound familiar because a delegate is basically a function pointer. However, developers who have used other languages are probably wondering, "Why do I need a reference to a method?". The answer boils down to giving you maximum flexibility to implement any functionality you want at runtime.
When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


Delegates have the following properties:
  • Delegates are like C++ function pointers but are type safe.
  • Delegates allow methods to be passed as parameters.
  • Delegates can be used to define callback methods.
  • Delegates can be chained together; for example, multiple methods can be called on a single event.
  • Methods do not have to match the delegate type exactly
  • C# version 2.0 introduced the concept of Anonymous Methods, which allow code blocks to be passed as parameters in place of a separately defined method. C# 3.0 introduced lambda expressions as a more concise way of writing inline code blocks. Both anonymous methods and lambda expressions (in certain contexts) are compiled to delegate types. Together, these features are now known as anonymous functions.


There are three types of delegates that can be used in C#.
  • Single Delegate
  • Multicast Delegate
  • Generic Delegate

Scenarios where can be used

·   Handle (call/invoke) multiple methods on a single event.
·   Define callback (asynchronous) methods.
·   Decoupling and implementing generic behaviors.
·   Invoke method at run time.
·   In LINQ for parsing the Expression Tree.

Syntax of Delegates

delegate <return type> <delegate-name> <parameter list>
public delegate int MyDelegate (string s);


using System;

namespace Samples

    //Declare delegate outside the class
    public delegate void mydelegate(int x, int y);

    class MyClass
        //Declare a custom event
        public static event mydelegate myevent;
        public static void sum(int x, int y)
            Console.WriteLine("Sum " + (x + y));
        public static void diff(int x, int y)
            Console.WriteLine("Diff " + (x - y));
        //Raising event
        public static void RaiseEvent(int x, int y)
            if (myevent != null)
                myevent(x, y);

    class MainClass
        static void Main(string[] args)
            //Instantiate delegate by passing a method name
            mydelegate del = new mydelegate(MyClass.sum);
            //attaching a method to delegate
            del += MyClass.diff;
            //As the above delegate holds reference of 2 methods ,
            //it is called as multicast delegate
            //calling a delegate
            del(10, 8);
            // alternate way to invoke a delegate
            del.Invoke(10, 10);
            //attaching a delegate to event
            MyClass.myevent += del;
            MyClass.RaiseEvent(10, 6);

Tuesday, January 13, 2015

Microsoft Dynamics CRM Email Router Consuming 99% of CPU Processing

This post will let you resolve the issue of CPU usage which increases to 99% after installing Microsoft Dynamics CRM Email Router.

To resolve this go to the services and find Microsoft CRM Email Router service and follow below steps:

1. Go to the properties of the router service and then click on the log on tab

2. You will notice that router service is running under Local System account. Change this to other account. Click on apply and close the pop up by clicking OK.

3. Now open the task manager and you will notice that CPU starts getting down.

Configuring Email Router for Dynamics CRM 2013

This post will cover how we can install and configure the Dynamics CRM Email Router using POP3 and SMTP server (For Example using Gmail).

First you need to download the Dynamics CRM 2013 Email Router and after that you need to follow the steps mention in below two sections:

Installing Email Router

1. On the Welcome to Microsoft Dynamics CRM 2013 Email Router setup page, select whether you   want to update Email Router setup. We recommend that you click Get updates for Microsoft Dynamics CRM. Then, click Next

2. On the License Agreement page, review the information and if you accept the license agreement, click I accept this license agreement, and then click I Accept.

3. If required components are missing, the Install Required Components page appears. If this page does not appear, all required components are installed, and you can skip to the next step in the installation procedure

4. On the Select Router Components page, select either or both options, and then click Next.

5. On the Select Microsoft Update Preference page, select how you would like Install the updates.

6. On the Select Install Location page, either accept the Default file installation directory or Browse to indicate a different location, and then click Next.

7. The System Checks page appears. This page is a summary of all system requirements for a successful Email Router installation. Verification errors must be corrected before the installation can continue.

8. The Ready to Install page appears. Review the installation selections that you have made. Click Back to change your selections, or Install to install now.

9. After Email Router Setup is finished installing files, click Finish.

Configure the Email Router

Now after installing the Dynamics CRM Email Router we will go through the configuration steps of it using POP3 and SMTP server type here i will show that using Gmail server.

Just follow the below simple steps:

1. Open the Email Router Configuration Manager.

2. Create a new profile. Give it appropriate name and set
a. Direction to Incoming
b. Email Server Type to POP3
c. Authentication Type to Clear Text.
d. Check the Use SSL box.
e. Email Server value to which is the name of the Gmail POP server.
f. Access Credentials as User Specified (that will be pick up from the Dynamics CRM User Profile will show that in later steps).

g. Now go to advanced tab and update the Network Port number to 995.

3. Now create another profile, this one is for outgoing mails. Give it appropriate profile name and set the values
a. Direction to Outgoing
b. Email Server Type to SMTP
c. Protocol to SMTP
d. Authentication Type to Clear Text
e. Check the Use SSL box
f. Email server value to
g. Access credentials will be freeze to other specified. As like Incoming Direction there is no option for user specified. So you need to specify the credential of Gmail account which will be use for sending the emails from the Dynamics CRM and on the behalf of other CRM users.

h. Now go to advanced tab and update the Network Port number to 587.

4. Now go to deployment tab of the Email Router Configuration and create new deployment.
a. Select Deployment to My Company (Because i am having on premise (IFD) you can choose option according to your instance).
b. Write your Dynamics CRM server URL for example: https://DiscoveryServer:PortNumber/OrganizationName
c. Access Credentials to Other specified and enter the credentials of your CRM (recommend to use Dynamics CRM admin credentials as that user will have full access to CRM).
d. Set incoming and outgoing configuration profile to profiles which we created earlier.
e. Press OK and close the popup.

5. In next step go to Users, Queues and Forward Mail boxes tab and select the deployment that we created in previous step then click on load data.

6. Now you will get the list of all the Users, Queues from the Dynamics CRM.

7. Now click on publish (That will save your all configurations done till now).

8. Select any of the User from the list and click on Test Access then you will get the error "Incoming Status: Failure - The Email Router service configuration parameter "EmailUser" is missing. This parameter is required".

This is because we had set the Access Credentials to User Specified in Incoming Profile for Email Router

So to resolve this user or administrator need to specify credentials in Dynamics CRM mail box which is explained in next step.

9. Log in to Dynamics CRM. Go to the Logged in User Profile and then Open the Mailbox.

  • Enable Allow to use Credentials for Email Processing and enter the credentials i.e.Username (email address of the User) and Password.
  • Ensure the Incoming, Outgoing Email profile and Appointments, Contacts, and Tasks set to Server Side Synchronization or Email Router.
  • Also need to create Server Profile where 
    • Server Type set to Other POP3/SMTP.
    • Incoming and Outgoing server location are set to and respectively.
    • In Credentials section set Authenticate Using to Credentials specified by a User or Queue
    • Enable the option Use same setting for Outgoing.
    • Also ensure Incoming port set to 995 and outgoing port set to 587.
  • And set the created Server Profile in Mailbox.

  • Now you can test the configuration by click on Test & Enable Mailbox from the ribbon.
  • That's it now you can see in the Configuration Test Results section that all status are update to  success and the user will receive a test mail from Dynamics CRM.

*Note Email address of the CRM Users need to be approved by the administrator.

10. Also now if you go to Email Router again and test the access for the user from the user list/queue that will show both incoming and outgoing status as succeeded.

* If still facing any issues for Incoming Status then you need to verify that POP is enabled for the User email.

Monday, January 12, 2015

Paging in SQL Server 2012 using Offset and Fetch

Paging in SQL Server 2012 using Offset and Fetch

The OFFSET and FETCH clause in SQL Server 2012 provides an option to fetch only a page or limited set of the results from the complete result set.

Offset provides a starting row from which result set is display.
Fetch is use to display number of row which are to be retrieved from the result set.

Using these two we create paging in SQL Server. Lets take a simple example in which we directly query with SQL Server using Offset and Fetch.

SELECT * FROM [dbo].[YourTableName] ORDER BY ID 

offset ((@IndexValue - 1) * PageSize) ROWS


In this we have to put value of @IndexValue and @PageSize.

Now using this, lets make Store Procedure for Paging  

CREATE PROC [dbo].[spGetPagedDispatch]


@Index INT=1,
@PageSize INT=5,
@TotalRecordCount INT OUTPUT
SELECT * FROM [dbo].[YourTableName] ORDER BY ID
offset ((@Index - 1) * @PageSize) ROWS
SET @TotalRecordCount = (SELECT COUNT(*) FROM [dbo].[YourTableName]  )

Hear we pass @Index and @PageSize as a parameter. If user doesn't supply these value it will take default values of 1 and 5 respectively.

@TotalRecordCount is the output parameter which gives the total record count within the table.
It will be used to display total number of pages and for other purposes.

  • OFFSET and FETCH are only support in SQL Server 2012.
  • ORDER BY is mandatory to use OFFSET and FETCH.
  • OFFSET clause is mandatory with FETCH.