March 2011 - Posts

Last week, I needed to complete one task on which I am going to blog about in this entry. The task is "Create a control panel like webpage to control (Start/Stop) Window Services which are part of my solution installed on computer where the main application is hosted".

Here are the important points to accomplish:
[1] You need to add System.ServiceProcess reference in your application. This namespace holds ServiceController Class to access the window service.

[2] You need to check the status of the window services before you explicitly start or stop it.

[3] By default, IIS application runs under ASP.NET account which doesn't have access rights permission to window service. So, Very Important part of the solution is: Impersonation. You need to impersonate the application/part of the code with the User Credentials which is having proper rights and permission to access the window service.

If you try to access window service it will generate "access denied" error.

The alternatives are: You can either impersonate whole application by adding Identity tag in web.cofig as:
        <identity impersonate="true" userName="" password=""/>

This tag will be under System.Web section. the "userName" and "password" will be the credentials of the user which is having rights to access the window service. But, this would not be a wise and good solution; because you may not impersonate whole website like this just to have access window service (which is going to be a small part of code).

Second alternative is: Only impersonate part of code where you need to access the window service to start or stop it. I opted this one. But, to be fair; I am really unaware of the code part for impersonation. So, I just googled it and injected the code in my solution in a separate class file named as "Impersonate" with required static methods. In Impersonate class; impersonateValidUser() is the method to impersonate a part of code and undoImpersonation() is the method to undo the impersonation. Below is one example:

 Start/Stop Window Service from ASP.NET page

 You need to provide domain name (which is "." if you are working on your home computer), username and password of appropriate user to impersonate.

[4] Here, it is very important to note that: You need to have to store the Access Credentials (username and password) which you are going to user for impersonation; to some secured and encrypted format. I have used Machinekey Encryption to store the value encrypted value inside database.

[5] So now; The real part is to start or stop a window service. You are almost done; because ServiceController class has simple Start() and Stop() methods to start or stop a window service. A ServiceController class has parametrized constructor that takes name of the service as parameter.

Code to Start the window service:

Start/Stop Window Service from ASP.NET page 

Code to Stop the window service:

Start/Stop Window Service from ASP.NET page 

Isn't that too easy! ServiceController made it easy :) I have attached a working example with this post here to start/stop "SQLBrowser" service where you need to provide proper credentials who have permission to access to window service.

Start/Stop Window Service from ASP.NET page 

hope it would helps./.


I found this something interesting while wandering over community which I would like to share. The post is whole about: DBML is not considering the table field's "Default value or Binding" setting which is a NotNull. I mean the field which can not be null but having default value set needs to be set IsDbGenerated = true in DBML file explicitly.

Consider this situation: There is a simple tblEmployee table with below structure:

tblEmployee Image 

The fields are simple. EmployeeID is a Primary Key with Identity Specification = True with Identity Seed = 1 to autogenerate numeric value for this field. EmployeeName and their EmailAddress to store in rest of 2 fields. And the last one is "DateAdded" with DateTime datatype which doesn't allow NULL but having Default Value/Binding with "GetDate()". That means if we don't pass any value to this field then SQL will insert current date in "DateAdded" field.

So, I start with a new website, add a DBML file and dropped the said table to generate LINQ To SQL context class. Finally, I write a simple code snippet to insert data into the tblEmployee table; BUT, I am not passing any value to "DateAdded" field. Because I am considering SQL Server's "Default Value or Binding (GetDate())" setting to this field and understand that SQL will insert current date to this field.

        using (TestDatabaseDataContext context = new TestDatabaseDataContext())
        {
            tblEmployee tblEmpObjet = new tblEmployee();
            tblEmpObjet.EmployeeName = "KaushaL";
            tblEmpObjet.EmployeeEmailAddress = "kaushal@emaildomain.com";
            context.tblEmployees.InsertOnSubmit(tblEmpObjet);
            context.SubmitChanges();
        }

Here comes the twist when application give me below error:

Error Image 

This is something not expecting! From the error it clearly depicts that LINQ is passing NULL value to "DateAdded" Field while according to my understanding it should respect Sql Server's "Default value or Binding" setting for this field. A bit googling and I found very interesting related to this problem.

When we set Primary Key to any field with "Identity Specification" Property set to true; DBML set one important property "IsDbGenerated=true" for this field.

IsDbGenerated=True Setting in Dbml.Designer.cs file 

BUT, when we set "Default Value or Biding" property for some field; we need to explicitly tell the DBML/LINQ to let it know that this field is having default binding at DB side that needs to be respected if I don't pass any value. So, the solution is: You need to explicitly set "IsDbGenerated=true" for such field to tell the LINQ that the field is having default value or binding at Sql Server side so, please don't worry if i don't pass any value for it.

You can select the field and set this property from property window in DBML Designer file or write the property in DBML.Designer.cs file directly.

Field with Default Value or Binding needs to be set IsDbGenerated = true in DBML file

 I have attached a working example with required table script with this post here. I hope this would be helpful for someone hunting for the same. Happy Discovery!