What is .Net Extension Method

.Net Extension Method is one of a nice features introduced with .Net 3.0. It allows developers to add new functionalities to existing data types without having a need to inherit from these types or compile old code. For example, if you are encrypting and decrypting strings in your application, wouldn’t it nice to have a code like below
StringObject.Encrypt();
In the tradition way, we used to create a static helper method that we call and pass string to it as a parameter:

public static string Encrypt(string input)
{
//….Logic goes here
return encyptedValue;
}

Implementing Extension Methods is very easy.. All what you need to do is to create a static class and have your extension method as static method and use this keyword on the parameter list.. Below is a sample code

public static class MyExtensions {
public static string Encrypt(this string input) {
//….Logic goes here
return encyptedValue;
}
}

Note the “this” keyword that is being used on the parameter.. this what make this .Net Extension Method. You can call this method in 2 ways:
1. As extension method, str.Encrypt();
2. As regular method: MyExtensionClass.Encrypt(str);
Both syntax are equivalent to each other but the first one is less code.

Loops Performance differences in .Net

I never thought there will be a big different when you use different approaches to iterate through a collection of objects. I knew there are some differences between FOREACH/WHILE/FOR loops, but I never thought the different will be as much as an article I read recently in an article published on codekicks site.

The article was demonstrate how using the foreach/while/For loops will affect the performance of your application. The first time I read the article posted there I got convened and posted a recommendation to read it to my personal site. a friend of mine read the article and we had a discussion about it. He had a very true observation about the test applied to come into the conclusion. The main issue raised by Ahmad (my friend who raised the issue) was about the internal operations done by each loop. For example, in FOREACH loop, it is doing an internal object retrieval and assignment to object reference which none of other loops doing it. Even if take this note into consideration, there will be some sort of performance differences between FOREACH/While/FOR loops.

So I still recommending reading the article but please keep into consideration that the test applied is not perfect.

How to register a Plugin inside MS CRM 4.0 using Registration tool shipped with CRM SDK 4.0 Plugins

Plugins on CRM 4.0 replaces Callouts on CRM 3.0.. Mainly Plugins/Callouts is a custom code that should run to response for an action taken place against specific entity. actions like : Create / Update… and so on. Plugins can response to more events than Callouts.. the more events mean more complexity on registering Plugins if we comparing it with Callouts. Thanks for MS, MS released a registration tool with CRM Sdk 4.0 to make out life easier. I listed below main steps to register Plugins inside CRM 4.0.

To register Plugins on CRM 4.0 Please follow these steps:

  1. Your assembly should be strongly named.

  2. The Class that wills response to actions on activity should be implements IPlugin Interface.

  3. Move your assemblies to C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly\ folder with any resources needed[like XML configuration file, and so on.].

  4. Open Plugin Registration tool that is shipped with CRM Sdk 4.0
  5. Connect to CRM server, then to CRM instance [NB: Your User Should be in Deployment Manager group.
  6. Register your assembly… to open Register assembly dialog Press (Ctrl +A)
RegisterCRM Plugin Building

7.  Add step to your assembly to open Register new step Press(Ctrl + T).. Step is representing a response to an Action happened on CRM Entity..
For example: to response to an Update Event for Contact Entity.. You have to add a step like the following Picture:

Register%20CRM%20Plugin2

How Can I Enhance the Performance of .Net Application?

The performance of any system is considered as a key part of the requirements that client usually ask for implicitly. The poor perform application is most likely going to be failed because of that even if it is doing lot of very cool functionalities.
Recently I read an interesting article about this topic published on CodeGuru. They highlighted couple of key actions that is easy to be implemented and they are almost effortless but at the end those actions will improve the overall performance of you application by better utilization of server CPUs.
They depending on the fact that the CPUs we have these days is much sophisticated that previous days and we need to take benefits of that. I strongly recommend reading this article

How to create OOP representations of XML

If you have an XML file and you are seeking for the best way to deal with it inside your code. You do not want to use XPath Queries intensively in your code and want to have the best way to deal with that XML… I faced such a case where I need to build a special configuration file for an FTP windows service. The way I wanted to build this XML file was in a hierarchy way something looks like the following XML:

<FtpConnection Code="CodeName" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

 

This FTP service suppose to work with more than on FTPConnection. Thus the above configuration snap might occur more than one time. Which make it is harder to deal with .

The best way to deal with this configuration inside code is to create a object oriented representation of it and load this XML into it. then let your code deal with the OOP classes instead of directly with XML.

Well, there is a very easy way to build a OOP presentation of this XML. There is a tool that is shipped with .Net framework & visual studio called XSD. You can use this tool to generate the OOP representation by first create XSD schema for your XML then convert that schema to OOP presentations. Below is how to do that step by step.

  1. I created a file on my desktop and I called it "FTPConfig.xml", I paste the following XML into it.

<Root>

<FtpConnection Code="CodeName" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

<FtpConnection Code="Code2Name" Direction="Upload">

<Ftp>

<RemoteHost>IP or the DNS name for the remote FTP server</RemoteHost>

<Username>FTP USerName</Username>

<Password>password</Password>

<ConnectionTimeOut>50</ConnectionTimeOut>

</Ftp>

<Security>

<IsSecured>true</IsSecured>

<ScretPhase>Sec Phrase Goes here</ScretPhase>

</Security>

</FtpConnection>

</Root>

· Note that I duplicated the FTPConnection tag twice, I did that for a purpose. I did that to tell XSD tool to expect this tag to occur more than once. Thus when XSD tool build the Schema representation of it, it will make the MAX occurrence of this tag as infinite

     2. From Start menu, open "Visual Studio 2008 Command Prompt"… it is Command prompt but has special configurations to open some Command Prompt tools shipped with VS 2008

      3. We need to set the current folder on CMD to be the desktop. To do so, write the following comand

Cd c:\users\[windowsUserName]\desktop

· Where [WindowsUserName] is the login name that you are using to login into your PC

       4. Now we need to create the XSD schema file for this XML. To do that, write the following command into your CMD

xsd FTPConfig.xml

· The output from this command is an XSD file and it will placed on your Desktop; the name of this XSD file suppose to be FTPConfig.xsd; My advice to you is to take couple of minutes to review this Schema file and make sure it is meeting your expectation.

       5. Now we need to Create the OOP presentation of this XSD; to do so, write the following command

xsd FTPConfig.xsd /classes /language:CS

a. The meaning of parameter /Classes : will make the output of this command to be Classes not DataSet.

b. While the Meaning of parameter /language is the programming language in which the code will be generated; CS means C# language

    The output form this command suppose a C# code placed on your desktop.

  •  My recommendation to you is to keep all the generated classes into a single class and do not split each class into a separated file. I am saying this because you might decide to change the configuration a little bit and you need to regenerate those classes again. So if you split those classes into different files, then you will get into a loop of re-splitting the classes again and again.

I hope you will find this topic helpful. Please feel free to send any comments/questions.

How to Solve “Error Creating Control” Error Message (ASP.Net Site)

If you face this error while trying to open a web application project

Error Creating Control – control_Name
‘/LM/W3SVC/1/Root/Project_Name’ is not a valid IIS application.

ErrorCreatingControlSnapshot

 

You can resolve this error by following the below steps , the root cause of this error is the incorrect mapping between web application project and the virtual directory in your IIS.
To Resolve this error , follow the following steps:

1-Right click – you web application project and select Properties -> Web
2- in the server section choose Use IIS Web Server -> then click Create Virtual.
3- Clean your project from any DDLs that was previously complied and compile your project again.

By doing the above steps this issue should got resolved.