Manage System.Transaction timeout

When using System.Transaction in .net framework; you may came across a timeout problem where you code needs longer time to run than the default timeout duration for System.Transaction.. this is because the System.Transaction will be aborted after few seconds which will rollback your changes.. to make the timeout longer than the default timeout duration, you need to adjust your code to do this, there is no configuration on IIS/appconfig level to accomplish that. the following code sample showing how to accomplish this from code perspective

TimeSpan timeoutDuration = TimeSpan.FromSeconds(180);
using(TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, timeoutDuration))
{


Scope.Complete();
}

The timeout default value is 60 seconds; if you want to have an infinite timeout; just set it to be 0.

You need also to change values on machine.config in case you made the timeout value more than 10 minutes. Because the machine.config has a configurable value to limit TransactionManager’s timeout to be 10 minutes at max.

TransactionManager is setting on top of the transaction object; thus it has the upper hand on it if you exceed the 10 minutes limit. The problem with TransactionManager is you can’t change the timeout default value programmatically and you need to update the machine.config to accomplish this. To do so change the following tag value on machine.config

<configuration>
   <system.transactions>
        <machineSettings maxTimeout="00:30:00" />
   </system.transactions>
</configuration>

SOLID Object-Oriented-Design Principles

One of the main principles of OOD (Object Oriented Design) is SOLID principles. SOLID is stands for :

1. Single Responsibility Principle:

This means the class has to have one and only one reason to responsibility. This responsibility should be encapsulated by the class.

2. Open/closed principle:

The open/closed principle states that software entities (classes, modules, functions, …etc) should be open for extension, but not for (closed) modification

This will be very helpful on the production environment where fixing and enhancing certain area is required without affecting other areas. thus you will go with extending the class and its behaviors without modifying the current existing code/methods

3. Liskov substitution principle:

means Substitute-ability; It states that, if SubclassA is a sub-type of MainTypeA, then objects of type MainTypeA may be replaced with objects of type SubclassA without altering any of the properties of that program

4. Interface segregation principle:

The main goal from this point is to make the software more maintainable and loosely coupled; thus easier to re-factor and change. Basically, we need to split the large interfaces into smaller interfaces so that the client classes will be aware of the methods/properties they concern about

5. Dependency inversion principle:

It is about decoupling software layers and making sure the software is loosely coupled. Basically this principle states 2 things:

– High-level modules/layers should NOT depend on low-level modules/layers. communication between modules and layers should go through abstractions.

– Abstractions should NOT depend on details and the Details should depend on abstractions.

 

 

Value Injector – How to Use

One of the main tasks that you may face during development is how to translate from one object to another that is very similar in term of properties and attributes. There are many ready to use code to be downloaded from Internet and use.

I tried Value Injector DLL; it was good enough for my needs and it satisfy my needs in very good way. you can download the Value Injector DLL from this link.

The overall performance of this component was very good and it didn’t hit the performance of the system that I’m working on.

The Basic usage of this component is as follow.

using Omu.ValueInjecter;
public class DemoClass { 
 public void MethodInjector(mySourceClass source)
  {
   MyClass destination = new MyClass();
   destination.InjectFrom(source);
   //inject using your own injector calss.
   destination.InjectFrom < MyInjection > (source);
  }
}

The good thing about ValueInjector is that you can build your own Injector class and use it; that is needed if the current implementation for ValueInjector is not satisfying your needs.

for more information about value injector, please visit this link

Sync Data Between SQL Server and SQL Azure

couple of weeks ago I had discussion with a friend of mine about cloud technologies form Microsoft. After we finished that discussion I start thinking of what scenarios that we maybe come over while developing a real enterprise application on cloud. One scenario came into mind about that; What if I get into a need to synchronize data between SQL Azure on the cloud and SQL server on-premises? is this will be an easy thing to be accomplished? Sudhesh through the below video illustrate how to setup data sync between a SQL Azure and SQL Server databases.

TransformXml Task Failed Unexpectedly Error Message While Publishing Web Project in Visual Studio 2010

 

TransformXml task failed unexpectedly

one of the strange issues that I faced yesterday while trying to publish one of the web projects that I am working on. although the project compilation was working fine and when I run the project locally on my PC it was working fine without any issue.

After investigating for this issue I found the cause of this issue because the Web.config file was containing a special character inside it. so what I did is encode the special character then try to publish it again and things worked for me fine.

I had a very large Web.config file so looking for the special characters was not an easy job. I wrote a regular expression on Visual Studio that helped me on accomplished this job. so I would recommend to use regular expression to search Web.config instead of looking for the special characters inside the web.config file.

Performance Troubleshooting (ANTS tool)

it happened once that a client raise an issue regarding to poor performance at certain area on an application that I was working on. That area was working perfectly on my environment, but at their environment it was not like that. I could not duplicate their environment locally because of security restrictions rules we have. Tracing code and do debugging was not helpful. Thus I decided to look into a tool that will help me on this mission.

I found a very nice tool that help me very much on tracing and figuring out the bottleneck on my application, that tool is called ANTS from Red-Gate. I strongly recommend to try this tool.

The good thing about this tool is that it is integrated inside the visual studio which making your life much easier when you want to start troubleshoot the performance issue. another thing that making it great is when you launch it from the visual studio, the result will be some sort of tree diagram for your code according to the resources utilization. Part of that diagram is the ability to see the code snap that you concerning about. also part of this output is an attribute that is showing you how many a certain method got called, you will find this very helpful because it will make you pay attention to methods that is getting called more than the expected numbers.

Red Gate providing this tool as a free trial to let you try it and make sure it is fitting to your business. you can check more about it by clicking here

Parallelism in .Net 4.0

One of the major enhancements that Microsoft has accomplished is enhancing the parallelism in many .Net class library. that is making things much faster and using the hardware in more effective way. in .Net 3.5 and earlier versions of .Net framework, wee have to using Threads to accomplish the same job. Actually .Net 4.0 is using Threads but that is internally at the .Net class library and it will be hidden from your code which is making your life much easier.

The new Parallelism feature of ..Net framework categorized into one of the folllowings:

1) Task Parallel:

including parallel implementaion for FOREACH and FOR loops letting you define the concurrent, Async tasks without working directlying into threads thing,

2) Parallel LINQ (PLINQ):

Significantly improves the performance of LINQ.

3) Data Structure for Parallel Programming:

including a high performance collection of classes with lock-free and at the same time it is thread safe including lightweight objects.

4) Tools to facilitate the Parallel Programming:

like debugger windows for tasks and parallel stacks.

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 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.