Performing pre-termination activities using ProcessExit event

In many cases we use resources such as files, storage, connections etc. in our application. Being a programmer it is our responsibility to free up all the used resources before terminating our program. We have try {} finally{} which does the same thing. This can be achieved by AppDomains ProcessExit event.

    class Myclass
        // will print 1st also sets up Event Handler
        static Myclass()
            Console.WriteLine("I am inside Myclass");

        static void Main(string[] args)
            Console.WriteLine("I am inside Main()"); // it will print 2nd
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

        static void CurrentDomain_ProcessExit(object sender, EventArgs e)
            Console.WriteLine("I am the last one to run");

Executing this program will print
I am inside Myclass
I am inside Main()

Obviously the last statement will not be visible, as the application is still running. But, you can try writting in an event log to see how it works.

LINQ update on table having no primary key

I was trying to update a record in a table last night, using LINQ. Everything was fine and running without any errors.
Surpisingly the record was not getting updated. I then attached the modified object to the context. It started giving an exception as,

Can't perform Create, Update or Delete operations on Table because it has no primary key.

Then I realised that there is no primary key assigned to the table. The possible solution was to change the database schema and add primary key 
to that table. But this change was not acceptable.
Further debugging into the code I got the workaround for this. 

When we generate a class file using a tool such as sqlmetal or anything else, certain attributes are associated with the columns of that table.
We can open that class file and manually add IsPrimary= true attribute to any one of the columns.

This will fool the LINQ engine and treat the respective column as a primary key.

Here is an example for the same.
I have generated a class with table as TableWithNoPK. It has 3 columns as Field1, Field2, Field3.
I assigned IsPrimary attribute to field1 as,

[global::System.Data.Linq.Mapping.ColumnAttribute(Name="field1", Storage="_Field1", DbType="VarChar(50)", IsPrimaryKey= true)]
        public string Field1
                return this._Field1;
                if ((this._Field1 != value))
                    this._Field1 = value;

The c# code goes normal as,

var qry = from NoPK in instance.TableWithNoPK
                      select NoPK;

       TableWithNoPK test = qry.FirstOrDefault();
       test.Field3 = "Gud 1";



This is a url which specifies where the service is hosted. Example is like,


Binding decribes how the client will communicate with the service. WCF makes use of various protocols to communicate with client.
You can read more details about types of bindings here.


This specified what operation(s) to be carried out. It is simple request/response, duplex exchange of information between client and service.
Examining end-points, client come to know what all operations are exposed by a service.

Detect session expiry using JavaScript

I was working on a web application using ASP.Net. I was making use session in my application. 
There was one scenario where I was trying to access session variable through JavaScript. It works fine, when session is accessed in normal state ( i.e. session is not expired). But after session expiry, if click on any button, it was throwing an exception and start abnormal behavior, resulting in page not found. 
To tackle with this, I used session handling from JavaScript as;

public string TransactionID
   get { 
        var data = ((Hashtable)(Session["SessionData"])); 
        if (data != null)
           return data["TransactionID "];
           return null;

Creating Singleton class in C#

In many of the cases, we come accross situations where we want to create only one instance of a class. And use it throughout the application. As a programmer we have to take care of that from the code itself. Let's take a look at this step by step.

We will create a class MySingletonClass in such a way that it can be instantiated only once.

    public class MySingletonClass
        //This will create a static instance of the class MySingletonClass
        private static MySingletonClass instance;

        //Private constructor
        private MySingletonClass() { }

        public static MySingletonClass Instance
                if (instance == null)
                    instance = new MySingletonClass();
                return instance;

        public void DoWork()
            Console.WriteLine("I am going for lunch");

So we are done with the class definition. Now let's start creating it's instances.

    class Program
        static void Main(string[] args)
            MySingletonClass objMySingleton = MySingletonClass.Instance;
            MySingletonClass objMySingleton2 = MySingletonClass.Instance;



If you debug this code and put a break-point at get of Instance, you will notice that the object creation statement will get executed only once for the first object. For later requests the program will serve the existing one.

Now, if by mistake or intentionally, someone else tries to create an instance of MySingletonClass in traditional way as;

MySingletonClass objMySingleton = new MySingletonClass();

Our private constructor will not allow this to be compiled due to protection level.
So our class can be instantiated only once from anywhere in the application and the instance can be shared.


.net .Net Instrumentation logging .net localization Agile amazon amazon elasticache amazon services AppDomain Application Domain architecture asp ASP.Net authentication authentication mechanisms Byte order mark c# cache cdata certifications class classic mode cloud cloud computing cluster code-behind Combobox compilation Configuration providers configurations connection connectionString constructors control controls contructor CSV CTS .net types conversion database DataGridView DataSource DataTable DataType DBML delegates design pattern dispose double encoding Entity framework Events exception handling expiry fault contracts fault exceptions function pointers functions generics help HostingEnvironmentException IIS inner join instance management integrated mode javascript join left outer join LINQ LINQ join LINQ to SQL memory leak methods microsoft modes in IIS MSIL multiple catch blocks no primary key Nullable Osmos Osmotic Osmotic communication Osmotic communications page events page life cycle partial class PMI preserve precision points private contructor ProcessExit Project management properties property protect connectionString providerName providers query regular expression repository Responsive Web Design return type run-time RWD Saas self join session session expiry sessions singelton singleton pattern software as a service source control system SQLMetal string toolstrip ToolStrip controls ToolStripControlHost tortoise SVN ToString() try catch finally update wcf web application web design web site web.config where-clause xml