.NET Serialization

Serialization is used to persist the state of the object, so the object can be regenerated with the same state later. .NET provides different types of serializations for objects,

  • BinaryFormatter for bunary serialization
  • XMLSerializer class for XML Serialization
  • SoapFormatter class for Soap Serialization.

You can use these objects for serialization. The Framework also needs to serialize the objects at various places for example in case of web services, the objects needs to be serialized for communication, in case of Remoting and also if the object is to be passed across the boundary of an Application Domain or the process. In all these cases, the framework will only be able to successfully serialize your objects if your objects are serializable.  

All the basic .NET types are serializable except Object, there are various other base classes which are by default serializable and can be successfully used in above scenarios, this includes the very commnly used DataSet, DataTable and other class, the complete list is

These classes are serializable because they implement the ISerializable Interface.

To set your own types Serializable, the most common way is to set the class/type with the [System.Serializable()] attribute, also use the [System.NonSerialized()] for the members that you don’t want to serialize. See the following example


class TestClass


private string str = “Check”;


private SqlCommand cmd = new SqlCommand();

private int count;

public CheckFunction()






However, using the serializable attribute is not the only way, and in some cases is not enough, specially if you are serializing complex objects which are inherited from other base objects. To serialize such objects you can have better control over serialization by implementing the ISerializable interface. This interface has only one method, which is GetObjectData(), this method is called when the object is to be serialized and you need to provide an extra constructor in the class to provide the de-serialization mechanism to regenerate the object. While serialization in GetObjectData() method, you can add your values to SerializationInfo parameter. During de-serialization you can get the values from the SerializationInfo object and set the values to the members of the class to regenarte the object in the same state. The example below will give you can idea

class TestClass : ISerializable


private string str = “Check”;

private int count;

public TestClass(SerializationInfo info, StreamingContext context)


this.str = info.GetString(“str”);

this.count = info.GetInt32(“count”);


public void GetObjectData(SerializationInfo info, StreamingContext context)


// TODO:  Add TestClass.GetObjectData implementation





Generally, as the MS guidelines and PAG documents say, serialization is not recommended for your custom objects, it has various issues regarding performance, security and scalability. But sometimes you have no other option then to use serialization. PAG guidelines do not recommend serialization and due to valid reasons but do not tell any other way you could get around the typical problems without serialization.


AJAX is Cool !

Have you seen Orkut, Google Suggest, Google Maps. Are you amazed by the wonderful web experience? They are using AJAX. AJAX is a really good technique; I don’t call it a technology because it is nothing new, just a blend of few established technologies like JavaScript, XML etc.

To learn about the actual idea behind AJAX, you can go through the article http://www.adaptivepath.com/publications/essays/archives/000385.php.

There are many implementations and third party AJAX libraries available, the idea is not so difficult and you can even implement it yourself without using any library provided that you have time.

Anyway, for .NET programmers AJAX.NET is available, you can download the C# source form https://svn.borgworx.net/SVN/BorgWorX/trunk/Web/Core/Ajax/

Creating an application with AJAX.NET is not very difficult, just a few basic steps you need to follow, here is a good article at MSDN


WinFx Runtime Beta1 and Whidbey

Finally, after so much effort I have WinFX Runtime Beta1 installed on my system with Visual Studio.NET Beta 2. Could’nt get the WinFX SDK installed as yet so I don’t have the XAMLPAd. I downloaded the source for the Chris Anderson’s AvPad and it ran fine.

Wrote my first small Avalon application also; actually I just ran a sample to test everything is properly setup. I thought I’ll write some useful application by the end of the day today but could not get time. Its 11:15 PM now and I need to go home so maybe tomorrow I’ll try to write some Avalon code.

I am still figuring out the XAML and the procedural code relationship in Avalon, I never liked these split architectures. You have to write XAML and then the procedural code and somehow they work together. Also, I know you can Avalon App in pure procedural code without XAML.

I need to do some work on this and get comfortable with this new stuff, and by the way where WinForms stand now, you can still create a WinForm app, I still have to compare these two application types and what are the best places/scenarios for using each of them.

Anyway, VisualStudio.NET Beta 2 is amazing. The development experience is great, very comfortable and elegant. The new Error Window is really wonderful with that grid view.

I was thinking that Microsoft may be going too fast in some of its development; they should not update the products/specs so frequently. I downloaded C# 3.0 enhancements document today (haven’t seen it though). Now ppl haven’t used C# 2.0 as yet. Most people in my environment and other local software houses are using VS.NET 2003. C# 2.0 and Whidbey are still in the Beta and now they are proposing new C# 3.0.

Maybe this is because they have so many problems with these products that they tend to improve them continuously but this has a real bad effect on the programmer productivity. Most programmers are not able to get benefit and utilize the new features and changes. I think people at Microsoft and surrounding environment do benefit from everything that they add but medium/small companies are not using most of the new features.

I think they need to make a few solid releases without tweaking the things so often, if they need to get the Windows Vista and Widbey some real success at all levels.

Ok ! Its late .. I am leaving .. Bye!

Getting Printer Information through WMI


I just got a mail from one of the friends, he wants to get the information about the printer like printer status, error, current status etc, I thought I should share the answer with all.

Yes there are ways to do this, the most common answer to almost anything related to any information in Windows is

WMI (Windows Management Instrumentation), this is the layer windows provides and uses for retrieving all sorts of information from the system and the running kernel.

In the following code I have listed all your printers on the system and also some of their properties/status like printing, stopped etc, but I can’t possibly list everything.

There is tons of information you can get about printer using WMI Win32_Printer class. Please refer to

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printer.asp and modify this code according to what you actually need.

using System;

using System.Diagnostics;

using System.Management;

using System.ComponentModel;

namespace ConsoleWMI


class Class1



static void Main(string[] args)


new Class1();


public Class1()


                  System.Management.ManagementObjectSearcher searcher = new ManagementObjectSearcher

                        (“SELECT * FROM Win32_Printer”);

foreach (ManagementObject service in searcher.Get())


string [] pStatus = {“Other”,”Unknown”,”Idle”,”Printing”,”WarmUp”,”Stopped Printing”, “Offline”};

string [] pState = {“Paused”,”Error”,”Pending Deletion”,”Paper Jam”,”Paper Out”,”Manual Feed”,”Paper Problem”,

                                                         “Offline”,”IO Active”,”Busy”,”Printing”,”Output Bin Full”,”Not Available”,”Waiting”,

                                                         “Processing”,”Initialization”,”Warming Up”,”Toner Low”,”No Toner”,”Page Punt”,

                                                         “User Intervention Required”,”Out of Memory”,”Door Open”,”Server_Unknown”,”Power Save”};

foreach(System.Management.PropertyData pData in service.Properties)


if(pData.Name == “Caption” || pData.Name == “Default” || pData.Name==”ServerName”)

                                          Console.WriteLine(pData.Name + ” = ” + pData.Value);

else if(pData.Name == “PrinterState”)

                                          Console.WriteLine(pData.Name + ” = ” + pState[Convert.ToInt32(pData.Value)]);

else if(pData.Name == “PrinterStatus”)

                                          Console.WriteLine(pData.Name + ” = ” + pStatus[Convert.ToInt32(pData.Value)]);








Network Load Balancing (Windows 2003 Server)

These are the configurations of the cluster we set-up with two Windows 2003 Web Servers and a DB Server (see last post to catch up) . It is’nt a pure cluster actually but the Application Load Balancing, the load balancing service (available in Windows 2003 Server, I think its also in 2000 server but have’nt checked that) distributes the application requests among both the servers.

In contrast to this load balancing setup, a pure cluster also manages the server fail overs and the state of the servers, but it needs an extra bit of hardware, maybe i’ll say something about that once we have that setup also.

This Load Balancing service in Windows 2003 Server is also interesting, I’ll write a step-by-step process to setup NLB when next time I get to blog 🙂 .. so stay tunned …. Also, there are a few other intersting things coming up, I think i’ll be active here …… C ya !