Monday, March 30, 2009

How to create (and deploy) a windows service in C# ?

 

Let's say we have the following specifications :

  • We want an application that will turn continuously on a machine
  • This application shall require no manual interaction (starting automatically)
  • This application shall monitor a specific folder on the file

The specifications are very simple and we will probably start thinking to a windows service that would be launched in an automatic manner. This service could then use a FileSystemWatcher to do its job.

Let's create our service

  • Create a new Blank Solution
  • Add a new project of type "Windows Service" (this type is available under the Visual C# / Windows section) and let's call it MyWindowsService
  • Automatically, VS will create a new project containing a class called "Service1", inheriting from System.ServiceProcess.ServiceBase
  • Right-Click on the file and choose Rename. Rename it to MyWindowsService
  • Update the constructor to set a service name.
using System.ServiceProcess; 
  
namespace MyWindowsService 
{ 
   public partial class MyWindowsService : ServiceBase 
   { 
      public MyWindowsService() 
      { 
         InitializeComponent(); 
         this.ServiceName = "MyPersonalService"; 
      } 
  
      protected override void OnStart(string[] args) 
      { 
      } 
  
      protected override void OnStop() 
      { 
      } 
   } 
} 

This class exposes a parameterless constructor and two methods OnStart and OnStop. These are the two methods that will be called when the service is being started (or stopped) by Windows. Note that automatically, when a service is being started or stopped, Windows will log an entry in the event viewer.

Let's check this !

Let's deploy our (empty) service 

To do so, we'll need to create a setup project and some installation actions to parameter the way the service is started.

  • Add a new project of type "Setup project(this type is available under Other Project Types / Setup and deployment)
  • Let's now how add a reference to our project
    • Right-Clic on the setup project and choose "Add / Project Output"
    • Select the project "MyWindowsService", the "Primary Output" and the configuration "Release"
    • Click on OK 

We will need now to control the way the service will be started and to specify it is a service. For that we have two solutions, either we rely on the service itself, or we write it ourself. We will here only see how to create the installer thru the service itself. 

Creating the installer's action class via the Service class itself

  • Double-Click on "MyWindowsService.cs" and right-clic in the designer to choose "Add Installer"
  • This will add a new file called "ProjectInstaller.cs" that contains a "serviceInstaller" and a "serviceProcessInstaller"
  • Check the property of the ServiceInstaller
    • Change the ServiceName to "MyPersonalService" (shall be the same as what you specified in the service constructor)
    • Change the DisplayName to "My Personal Service"
    • Change the StartType to "Automatic"
  • Check the property of the ServiceProcessInstaller
    • Change the Account to... well any value depending of the behaviour you would like. In my case, I will choose "LocalService". You can check the Service.ServiceProcess.ServiceAccount class for more information about this. 

Let's now complete the installer

Now we can complete the installer to add the installer actions we have just created.

  • Right click on the setup project and choose "View / Custom Actions"
  • Now we need to add our installer class to be used for the events "Install" and "Uninstall"
    • Right-Clic on Install and choose "Add Custom Actions"
    • Double clic on Application Folder
    • Select the "Primary Output from MyWindowsService" and click OK
    • Do the same thing for the event "Uninstall"

Now we just need to compile and to install the service to check that it is working.

  • Set yourself in "Release Mode"
  • Right-Clic on the Setup project and choose "Rebuild"
  • Right-Clic on the project and choose "Install"

Let's check the behaviour

As I have said before a service will log some information when being started and stopped. We can so go in the "Services" management window.

 

Note that even if we have put the "start type" to "Automatic", you must start the service manually. It would start automatically only after the next windows reboot !

Let's start the service and check in the Event Viewer.

We can see two new events saying respecively

  • The My Personal Service service was succesfully sent a start control
  • The My Personal Service service entered the running state

So far so good !

Let's complete our service to let him work

Now that we have a functional service, we can complete him to work. We wanted him to monitor a folder. Let's modify our service and do that this way:

using System.IO; 
using System.ServiceProcess; 
  
namespace MyWindowsService 
{ 
   public partial class MyWindowsService : ServiceBase 
   { 
      public MyWindowsService() 
      { 
         InitializeComponent(); 
         this.ServiceName = "MyPersonalService"; 
      } 
  
      protected override void OnStart(string[] args) 
      { 
         //1. Let's check only txt files in a specific folder 
         string path = @"c:\Temp\MyFolderToMonitor"; 
         string filter = "*.txt"; 
         FileSystemWatcher watcher = new FileSystemWatcher(path, filter); 
  
         //2. We want information about the filename 
         watcher.NotifyFilter = NotifyFilters.FileName; 
  
         //3. Let's register the creation event only, and let's start the monitring 
         watcher.Created += new FileSystemEventHandler(watcher_Created); 
         watcher.EnableRaisingEvents = true; 
      } 
  
      ///  
      /// When a file is created, we'll just add an entry in the EventLog 
      /// Note that this.EventLog will log to the Application Log,  
      /// with the name of the service as source. 
      /// To log in another log, you should create yourself an EventLog. 
      ///  
      private void watcher_Created(object sender, FileSystemEventArgs e) 
      { 
         string message = string.Format("{0} : {1}", e.ChangeType, e.Name); 
         this.EventLog.WriteEntry(message); 
      } 
  
      protected override void OnStop() 
      { 
      } 
   } 
} 

Now we can just rebuild and reinstall the service. We can then start it, create a file in the "Temp\FolderToMonitor" folder and stop the service. If we now look to the EventViewer, what can we see ? Three events :

  • Service started successfully
  • Created: Document1.txt
  • Service stopped successfully

And here we are ! We now have a fully functional windows service.

Tuesday, March 24, 2009

Regular Expression in C#

Regular Expression is a language independent feature supported by many languages, notably PERL, Java, JavaScript, C# etc. The support for Regular Expression is extensive under PERL and thus there is a term coined PCRE (Perl Compatible Regular Expression).

.NET has followed the similar pattern writing syntax.

 

The Base Class Library includes a namespace (System.Text.RegularExpressions) where a set of classes have been exposed to utilize the power of regular expressions.

 

Summarizing the widely used classes to utilize the power of regular expressions under C#:

 

Use static methods of Regex class or instance method to match a pattern or replace a pattern. After successful match the result of a regular expression is a collection (MatchCollection) of Match objects. Within each Match object is a collection (GroupCollection) of Group objects. Each Group object within the GroupCollection represents either the entire match or a sub-match that was defined via parenthesis. Within each Group object is a collection (CaptureCollection) of Capture objects. Each Capture object contains the results from a single subexpression capture.

 

I will try to explain each of them with some example for better understanding.

 

Regex class provides several static methods to enable you check for match or get matches without even instantiating the Regex object.

 

Escape: Escapes all meta-characters within a pattern string.

Unescape: Un-escapes any escaped meta-characters within a pattern string.

 

IsMatch: A Boolean value is returned depending on whether the pattern is matched in the string or not.

Match: A match instance is returned for the first string matched as defined by pattern.

Matches: A collection of matches are returned as MatchCollection

Replace: Replaces the first occurrence of the pattern in the string.

Split: Split the strings over pattern to get an array of strings.

 

Except Escape and Unescape all of the above methods are also available as instance members of Regex class. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

 

Let’s write some sample code to see how regular expression works in C#:

 

Sample1:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match more than once occurrence of 'a'

string pattern = "a+";

if(Regex.IsMatch(content, pattern))

Console.WriteLine("Pattern Found");

else

      Console.WriteLine("Pattern Not Found");

 

Output:  Pattern Found

 

 

Sample2:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match one or more than one occurrence of 'a', using ^ and $ enforces that

// whole string must be matched

string pattern = "^a+$";

if(Regex.IsMatch(content, pattern))

      Console.WriteLine("Pattern Found");

else

      Console.WriteLine("Pattern Not Found");

 

Output:  Pattern Not Found

 

 

Sample3:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match all digits (at least one) which is preceded by one or more than one

// occurrence of 'a' and optionally followed by 'b'

string pattern = @"a+(\d+)b*";

MatchCollection mc = Regex.Matches(content, pattern);

string spacer = "";

if(mc.Count > 0)

{

      Console.WriteLine("Printing matches...");

      for(int i =0; i <>

      {

            spacer = "";

            Console.WriteLine();

            Console.WriteLine(spacer+ "Match["+i+"]: "+ mc[i].Value);                    

            Console.WriteLine(spacer+ "Printing groups for this match...");

            GroupCollection gc = mc[i].Groups;

            for(int j =0; j <>

            {

                  spacer = " ";

                  Console.WriteLine(spacer+ "Group["+j+"]: "+ gc[j].Value);                                

                  Console.WriteLine(spacer+ "Printing captures for this group...");

                  CaptureCollection cc = gc[j].Captures;

                  for(int k =0; k <>

                  {

                        spacer = "  ";

                        Console.WriteLine(spacer+ "Capture["+k+"]: "+ cc[k].Value);                              

                  }

            }                            

      }

}

else

{

      Console.WriteLine("Pattern Not Found");

}

 

Output:

Printing matches...

 

Match[0]: aaa123b

Printing groups for this match...

 Group[0]: aaa123b

 Printing captures for this group...

  Capture[0]: aaa123b

 Group[1]: 123

 Printing captures for this group...

  Capture[0]: 123

 

Match[1]: aaa123

Printing groups for this match...

 Group[0]: aaa123

 Printing captures for this group...

  Capture[0]: aaa123

 Group[1]: 123

 Printing captures for this group...

  Capture[0]: 123

 

 

Here the first two samples are simple enough to understand. In ‘Sample 1’ I am just checking whether one or more than one consecutive ‘a’ (i.e. at least 2 ‘a’s) occurs in the given string.  And similarly in ‘Sample 2’ I am checking whether the given string starts and ends with ‘a’ and may contains more ‘a’s (which is false as we have other characters also in the given string).

 

Now let us examine the third sample, here you might be thinking why .NET has so many different classes Match, Group, and then Capture. With this example the difference between these are not very evident. Here we find Match[0], Group[0] and Capture[0] all containing the same value.

 

Let us see what MSDN says about these:

 

Match: The Match class represents the results of a regular expression matching operation.

Group: The Group class represents the results from a single capturing group.

Capture: The Capture class contains the results from a single subexpression capture.

 

In simple words Match produces everything that is matched by given pattern, when regular expression search is performed on the given string. There can be multiple occurrence of pattern in the given string, in that case you can get the collection of such matches by calling the Matches API provided byRegex class.

 

The Group is a part of given pattern enclosed by the ‘(‘ and ‘)’. So a group contains the part of matched string which is matched by the subpattern enclosed under brackets. Exception to this the Group[0] always contains the whole match (same as the value of Match).

 

The Capture is a part of string matched by the group expression i.e. the string matched by a subexpression of group expression. To understand it better I will slightly modify the ‘Sample 3’ to make single digit match as a subexression of the group.

 

Sample4:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match all 1,2 or 3 (at least once) which is preceded by one or more than one

// occurrence of 'a' and optionally followed by 'b'

string pattern = @"a+(1|2|3)+b*";

MatchCollection mc = Regex.Matches(content, pattern);

string spacer = "";

if(mc.Count > 0)

{

      Console.WriteLine("Printing matches...");

      for(int i =0; i <>

      {

            spacer = "";

            Console.WriteLine();

            Console.WriteLine(spacer+ "Match["+i+"]: "+ mc[i].Value);                    

            Console.WriteLine(spacer+ "Printing groups for this match...");

            GroupCollection gc = mc[i].Groups;

            for(int j =0; j <>

            {

                  spacer = " ";

                  Console.WriteLine(spacer+ "Group["+j+"]: "+ gc[j].Value);                                

                  Console.WriteLine(spacer+ "Printing captures for this group...");

                  CaptureCollection cc = gc[j].Captures;

                  for(int k =0; k <>

                  {

                        spacer = "  ";

                        Console.WriteLine(spacer+ "Capture["+k+"]: "+ cc[k].Value);                              

                  }

            }                            

      }

}

else

{

      Console.WriteLine("Pattern Not Found");

}

 

Output:

 

Printing matches...

 

Match[0]: aaa123b

Printing groups for this match...

 Group[0]: aaa123b

 Printing captures for this group...

  Capture[0]: aaa123b

 Group[1]: 3

 Printing captures for this group...

  Capture[0]: 1

  Capture[1]: 2

  Capture[2]: 3

 

Match[1]: aaa123

Printing groups for this match...

 Group[0]: aaa123

 Printing captures for this group...

  Capture[0]: aaa123

 Group[1]: 3

 Printing captures for this group...

  Capture[0]: 1

  Capture[1]: 2

  Capture[2]: 3

 

You must be getting confused over the strings matched by Group[1] in this example. Again quoting from MSDN:

 

Because Group can capture zero, one, or more strings in a single match (using quantifiers), it contains a collection of Capture objects. Because Groupinherits from Capture, the last substring captured can be accessed directly (the Group instance itself is equivalent to the last item of the collection returned by the Captures property).

How to Write a Regular Expression


What Regular Expression? 
A regular expression is a pattern that can match various text strings, used for validations.

Where and when to use Regular Expression?
It can be used in the programming languages which supports or has regular expression class as in built or it supports third party regular expression libraries.

Regular expressions can be used to valid different type of data without increase the code with if and case conditions. A number of if conditions can be omitted with single line of regular expression checking.

Benefits of Regular Expression:
The following are benefits (not all included) of use of Regular Expression.
a) # line of code can be reduced.
b) Speed Coding.
c) Easy maintenance (you don’t need to change if validation criteria changes, just check the regular expression string).
d) Easy to understand (you don’t need to understand the programmer logic on large if statements and case statements).

Elements of Regular Expression:
Here are the basic elements of regular expression characters/literals, which can be used to build big regular expressions:

^ ---->Start of a string.
$ ---->End of a string.
. ----> Any character (except \n newline)
{...}----> Explicit quantifier notation.
[...] ---->Explicit set of characters to match.
(...) ---->Logical grouping of part of an expression.
* ---->0 or more of previous expression.
+ ---->1 or more of previous expression.
? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.
\w ----> matches any word character, equivalent to [a-zA-Z0-9]
\W ----> matches any non word character, equivalent to [^a-zA-Z0-9].
\s ----> matches any white space character, equivalent to [\f\n\r\v]
\S----> matches any non-white space characters, equivalent to [^\f\n\r\v]
\d ----> matches any decimal digits, equivalent to [0-9]
\D----> matches any non-digit characters, equivalent to [^0-9]

\a ----> Matches a bell (alarm) \u0007.
\b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table.
\t ---->Matches a tab \u0009.
\r ---->Matches a carriage return \u000D.
\v ---->Matches a vertical tab \u000B.
\f ---->Matches a form feed \u000C.
\n ---->Matches a new line \u000A.
\e ---->Matches an escape \u001B

$number ----> Substitutes the last substring matched by group number number (decimal).
${name} ----> Substitutes the last substring matched by a (? ) group.
$$ ----> Substitutes a single "$" literal.
$& ----> Substitutes a copy of the entire match itself.
$` ----> Substitutes all the text of the input string before the match.
$' ----> Substitutes all the text of the input string after the match.
$+ ----> Substitutes the last group captured.
$_ ----> Substitutes the entire input string.

(?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited.


Simple Example:
Let us start with small example, taking integer values:
When we are talking about integer, it always has fixed series, i.e. 0 to 9 and we will use the same to write this regular expression in steps.

a) Regular expression starts with “^”
b) As we are using set of characters to be validated, we can use [].
c) So the expression will become “^[1234567890]”
d) As the series is continues we can go for “-“ which gives us to reduce the length of the expression. It becomes “^[0-9]”
e) This will work only for one digit and to make it to work for n number of digits, we can use “*”, now expression becomes “^[0-9]*”
f) As with the starting ending of the expression should be done with “$”, so the final expression becomes “^[0-9]*$”

Note: Double quotes are not part of expression; I used it just to differentiate between the sentences.

Is this the way you need to write:
This is one of the way you can write regular expression and depending on the requirements and personal expertise, regular expression could be compressed much shorter, for example above regular expression could be reduced as.

a) Regular expression starts with “^”
b) As we are checking for the digits, there is a special character to check for digits “\d”
c) And digits can follow digits , we use “*”
d) As expression ends with “$”, the final regular expression will become
"^\d*$”

Digits can be validated with different ways of regular expressions:

1) ^[1234567890]*$
2) ^[0-9]*$
3) ^\d*$

Which one to choose?
Every one of above expressions will work in the same way, choose the way you are comfort, it is always recommended to have a smaller and self expressive and understandable, as these will effect when you write big regular expression.

Example on exclude options:
There are many situation which demands us to exclude only certain portion or certain characters,
Eg: a) Take all alpha numeric and special symbols except “&”
b) Take all digits except “7”
then we cannot prepare a big list which includes all instead we use the symbol of all and exclude the characters / symbols which need to be validated.
Eg: “^\w[^&]*$” is the solution to take all alpha numeric and special symbols except “&”.

Other Examples:
a) There should not be “1” as first digit,?
^[^1]\d*$ ? this will exclude 1 as first digit.

b) There should not be “1” at any place?
^\d[^1]*$ ? this will exclude the 1 at any place in the sequence.

Note: Here ^ operator is used not only to start the string but also used to negate the values.

Testing of Regular expression:
There are several ways of testing this
a) You can write a windows based program.
b) You can write a web based application.
c) You can even write a service based application.


Windows base sample code:
Here are steps which will be used for regular expression checking in dotNet:

a) Use System.Text.RegularExpression.Regex to include the Regex class.
b) Create an Regex object as follows:
Regex regDollar= new System.Text.RegularExpressions.Regex("^[0-9]*$ ");
c) Call the IsMatch(string object) of the Regex call, which will return true or flase.
d) Depending on the return state you can decide whether passed string is valid for regular expression or not.]

Here is the snap shot code as function:

Public boolean IsValid(string regexpObj, string passedString)
{
//This method is direct method without any exceptional throwing..
Regex regDollar= new System.Text.RegularExpressions.Regex(regexpObj);
return regDollar.IsMatch(passedString);
}
With minor changes to the above function it can be used in windows or webbased or even as a service.

Another way -- Online checking:
At last if you are fed up with above and you have internet connection and you don’t have time to write sample, use the following link to test online

http://www.regexplib.com/RETester.aspx

MORE INFO:
You can find more information on these type of characters at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterescapes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpcongroupingconstructs.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp

--Here is the end of article, hope this basic build will definetely useful for writing a big and good Regular Expression ---

Express your code with REGULAR EXPRESSIONS

Thursday, March 19, 2009

How To Create LOG For Our Code Execution Of An Application

To keep track of our code execution we use log files. Here I am giving you a small piece of code for creating log files for out code execution. We will make a project which could have a Logger.cs file which contains a Logger class. The Logger class is a singleton class that could make log for whole execution process of our application. The Logger class contains three methods.

1.       LogDebug(string message):- this method could be used to track the flow of execution of your code. The log will be written into log file with Date and Time of its occurrence.

2.       LogError(Exception ex):- this method could be used to log exceptions that could occur in your code execution. The log will be written into log file with Date and Time of exception occurrence.

3.       LogResult(string message):- this method is to log the result in same file. This will not contain Date and Time of result occurrence.

 

The log file will be opened in Append mode always, whenever we run our application that use logger utility. It means whenever you will run your code using this Logger utility, it will update(append) the log file at specified location if it’s already exists. And if specified log file doesn’t exist at specified location then the log file will be created.

 

You could attach this Looger.cs file in your solutions and could maintain the track of your code execution, exceptions and results.

Please find the code for Logger class below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace SQLServerGACUtilities

{

    ///

    /// Singleton Class for logs.

    ///

    class Logger

    {

        # region Local Variables

        private static Logger logger;

        private static string m_LogFile;

        # endregion

 

        #region Singleton Approach

        private Logger()

        {

        }

        public static Logger CreateInstance(string fileName)

        {

            if (logger == null)

            {

                logger = new Logger();

                m_LogFile = Path.Combine(Directory.GetCurrentDirectory(), fileName);

            }

            return logger;

        }

        #endregion

 

        #region Member Variables

        ///

        /// Log exception

        ///

Exception occured

        public void LogError(Exception ex)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

 

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(String.Concat(DateTime.Now.ToString(), "Error Message: ", ex.Message));

            StreamWr.WriteLine(String.Concat("Error Source:", ex.Source.ToString()));

            StreamWr.WriteLine(String.Concat("Error Details:", ex.ToString()));

            StreamWr.Close();

            logFile.Close();

        }

 

        ///

        /// This function actually logs the information message.

        ///

        ///

message to be logged

        public void LogDebug(string message)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(String.Concat(DateTime.Now.ToString(), " -> ", message));

            StreamWr.Close();

            logFile.Close();

        }

 

        ///

        /// Writes results in Log file.

        ///

        ///

        public void LogResult(string message)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(message);

            StreamWr.Close();

            logFile.Close();

        }

        #endregion

    }

 

}

 

 

NOTE:- Please replace the current namespace of the Logger.cs file with your application’s namespace before running the application. Or you could use “using” keyword to register the existing namespace in your application.

 Have a nice day. J