Object serialization is the process of converting an object to a format that is suitable for persistence (database, file, etc) or transportation (remoting, Web Services, MSMQ, etc).

The format of the output byte stream is governed by a formatter object. When you serialize data, you construct a formatter object that implements the required format.

With .NET Framework 2.0 and above you can use two formatters: BinaryFormatter and SoapFormatter. You can also serialize objects (well, just the public members, not the private ones - which is know as shallow serialization) by using the XMLSerializer class.

The following class Person implements some attributes that are used in the process of seralization. This class is also used on the application example you can download below.

 [Serializable] //<-- This atribute is just required by BinaryFormatter and SoapFormatter
    public class Person  //<-- XMLSerializer needs that the class is defined as public
    {
        //[System.Xml.Serialization.XmlIgnore]   //<-- using this attribute, this field will be ignored in Xml Serialization        

        public string FirstName; //Some few public properties to serialize. They  will be serialize by the three formatters.

        public string LastName;                
               
        [NonSerialized] //<-- Using this attribute, the field Nationality won't be serialized (by any of the three serializers)
        public string Nationality;        
        
        private string _Address; //this private field won't be serialized using XmlSerialization. They will be serialized using Binary or Soap formatters

        private string _ZIPCode; //this private field won't be serialized  using XmlSerialization. They will be serialized using Binary or Soap formatters

		//let's create a method to set the private properties.
        public void SetAddress(string address, string zipCode) 
        {
            _Address = address;
            _ZIPCode = zipCode;
        }

        public override string ToString()
        {
            return string.Format("I'm {0} {1} from {2}!. Address: {3}, {4}", 
				FirstName,LastName,Nationality, _Address,_ZIPCode);
        }
    }

Serialization formatters in the .NET Framework

1) BinaryFormatter (System.Runtime.Serialization.Formatters.Binary namespace)

  • Serializes and object in an internal binary format that the .NET Framework understands.
  • The type needs to be marked with the [Serializable] atribute (see class Person)
  • BinaryFormatter saves metadata (assembly and type information) on the output stream along with object data. This information is necessary to deserialize the data and rebuild the object in memory.

PROS

  • The output byte stream generated is compact
  • The serialization process is faster than using the other formatters.
  • This formatter can serialize generic and non generic collections (being the items within the collection serializable)
  • Serializes public and private members (deep serialization)

CONS

  • Format not readable by other techonolgies (just .NET Framework)

BinaryFormatter serialization use example

Person p=new Person();
string path=@"c:\myfile.bin";
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
	System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
	b.Serialize(fs, p);
	fs.Close();
}

This code will serialize object p (type Person) on file c:\myfile.bin with binary format

BinaryFormatter deserialization use example:

string path=@"c:\myfile.bin";
using (System.IO.FileStream ds = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
	System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
	Person p = bf.Deserialize(ds) as Person;	
}

This code will deserialize object p (type Person) from c:\myfile.bin

2) SoapFormatter (System.Runtime.Serialization.Formatters.Soap namespace)

  • Serializes an object in SOAP format.
  • The type also needs to be marked with the [Serializable] atribute.
  • To use this class you need a reference to the System.Runtime.Serialization.Soap assembly.
  • This formatter also saves metadata (assembly and type information) on the output stream along with object data.

PROS

  • Follows a standard (SOAP) that other platforms can understand.
  • Serializes public and private members (deep serialization)

CONS

  • It is more verbose (less efficient) than BinaryFormatter.
  • It can NOT serialize generic collections (System.Collections.Generic namespace)

SoapFormatter serialization use example:

Person p=new Person();
string path=@"c:\myfile.soap";
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
	System.Runtime.Serialization.Formatters.Soap.SoapFormatter
	f = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
	f.Serialize(fs, p);
	fs.Close();
}

This code will serialize object p (type Person) on file c:\myfile.soap with Soap format

SoapFormatter deserialization use example:

string path=@"c:\myfile.soap";
using (System.IO.FileStream ds = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
	System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
	Person p = sf.Deserialize(ds) as Person;
	AppendToLog(string.Format("Single person deserialized from {0} in SOAP format: {1}", path, p));
}

This code will deserialize object p (type Person) from file on c:\myfile.soap using Soap formatter

A Person object after Soap serialization looks like this:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
	<SOAP-ENV:Body>
		<a1:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/dotneat_net.Serialization%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
			<FirstName id="ref-3">Joe</FirstName>
			<LastName id="ref-4">Doe</LastName>
			<_Address id="ref-5">dotneat.net Street, Zaragoza, Spain</_Address>
			<_ZIPCode id="ref-6">50007</_ZIPCode>
		</a1:Person>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

3) XMLSerializer

  • Serializes and object in XML format.
  • Requeriments:
    • Type must be public (public class person)
    • Must implement a parameterless constructor (in order to deserialize the object)
    • If you are serializing a non generic collection of items, you must pass the types that are stored in the collection as a parameter in the constructor of the XmlSerializer (see example code).

PROS

  • It can serialize generic and non generic collections (being the items within the collection serializable)
  • Class doesn't need to be decorated with [Serializable] attribute.
  • Developer has a deep control about how each field is going to be serialized by using the attributes:
    • [XmlAttribute] : over a field, marks that the field will be serialized as attribute, instead of a node
    • [XmlIgnore] : won't serialize that field. The same as NonSerializable, but just for the XmlSerializer.
    • [XmlElement (ElementName="NewName"]: Allows you to rename the field when being serialized.
    • ....

CONS

  • Only public members will be serialize! (shallow serialization) (both BinaryFormatter and SoapFormatter would serialize also object private members)

XmlSerializer serialization use example:

Person p=new Person();
string path=@"c:\myfile.xml";
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Person));
using (System.IO.FileStream xmlStream = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
	serializer.Serialize(xmlStream, p);
	xmlStream.Close();
}

This code will serialize object p (type Person) on file on c:\myfile.xml using the XmlSerializer class

XmlSerializer deserialization use example:

string path=@"c:\myfile.xml";
System.Xml.Serialization.XmlSerializer dxml = new System.Xml.Serialization.XmlSerializer(typeof(Person));
using (System.IO.FileStream xmlStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
	Person p = dxml.Deserialize(xmlStream) as Person;
	xmlStream.Close();
}                

This code will deserialize object p (type Person) from file c:\myfile.xml using the XmlSerializer class

A Person object after XML serialization looks like this (see how just public members are serialized):

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Joe</FirstName>
  <LastName>Doe</LastName>
</Person>

As you may see, private members are not serialized.

Selecting Fields to serialize

You can omit fields from being serialized by marking them with the [NonSerialized] attribute (see Nationality field in Person class).

Customizing the process of serialization and deserialization

If you are required to, you can modify the way the data in objects is serialized and deserialized. For example, you may want to serialize the account number and pass code of the class BackAccount crypted (just those two fields)

Serialization process

You need to implement the interface ISerializable on the class to serialize. If the type implements ISerializable interface, the formatter calls the GetObjectData to convert the object into the stream of bytes.

Use OnSerializingAttribute and OnSerializedAttribute to mark methods that will be executed before and after the serialization takes place.

[Serializable]
public class Person : ISerializable{

    .....

    [OnSerializing]
    public void Deserializing (StreamingContext context){
        //before serialization
    }


    [OnSerialized]
    public void Serialized (StreamingContext context){
        //after serialization
    }
}

Deserialization process

It is also possible to control how the deserialization process by implementing a constructor that takes a SerializationInfo and a StreamingContext as parameters.

[Serializable]
public class Person : ISerializable{
    //Deserialization constructor
    private Person(SerializationInfo info, StreamingContext context){
        string fname=info.GetString("FirstName");    
        // ...   
    }
}

Use OnDeserializingAttribute and OnDeserializedAttribute to specify methods to run before and after the object is deserialized.

[Serializable]
public class Person : ISerializable{

    .....

    [OnDeserializing]
    public void Deserializing (StreamingContext context){
        //before deserialization
    }


    [OnDeserialized]
    public void Deserialized (StreamingContext context){
        //after deserialization
    }

}

Example application code


(click on the image to view screenshot in full size)

Serialization application example source code (hosted on google code)

About the example application

You can use the application for serialize/deserialize a simple example class "Person". You can go and check file size and format of output file. You can also serialize/deserilize non generic (ArrayList) and generic (List) collections of Person, so you can see how the formatters serialize those types.

Performance

There is a performance test functionality. You can check which is the faster way of serializing a simple type. Feel free to extend the example to create performance tests that suits your needs

Final review

kick it on DotNetKicks.com AddThis Social Bookmark Button

Backgroundworker example

Posted in .NET 2.0 | Performance | Visual Studio 2005 at Tuesday, February 10, 2009 5:57 PM UTC

The background worker allows you to execute intense or long operations on a separate thread, without having to deal with threads, invokes or delegates. This is essential in today's cluttered webspace brought forth by the constant growth of broadband technology.

This simple example pretty much cover all posibilities of use of this component: cancellation support, backgroundworker error handling and report progress (also passing UserState data on report progress notifications)

Nota: Este artículo está disponible en castellano aquí

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BackgroundWorker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //mandatory. Otherwise will throw an exception when calling ReportProgress method
            backgroundWorker1.WorkerReportsProgress = true; 

            //mandatory. Otherwise we would get an InvalidOperationException when trying to cancel the operation
            backgroundWorker1.WorkerSupportsCancellation = true;
        }



        //This method is executed in a separate thread created by the background worker.
        //so don't try to access any UI controls here!! (unless you use a delegate to do it)
        //this attribute will prevent the debugger to stop here if any exception is raised.
        //[System.Diagnostics.DebuggerNonUserCodeAttribute()]
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //NOTE: we shouldn't use a try catch block here (unless you rethrow the exception)
            //the backgroundworker will be able to detect any exception on this code.
            //if any exception is produced, it will be available to you on 
            //the RunWorkerCompletedEventArgs object, method backgroundWorker1_RunWorkerCompleted
            //try
            //{
                DateTime start = DateTime.Now;
                e.Result = "";
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(50); //do some intense task here.
                    backgroundWorker1.ReportProgress(i, DateTime.Now); //notify progress to main thread. We also pass time information in UserState to cover this property in the example.
                    //Error handling: uncomment this code if you want to test how an exception is handled by the background worker.
                    //also uncomment the mentioned attribute above to it doesn't stop in the debugger.
                    //if (i == 34)
                    //    throw new Exception("something wrong here!!");

                    //if cancellation is pending, cancel work.
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true; 
                        return;
                    }
                }

                TimeSpan duration = DateTime.Now - start;
                
                //we could return some useful information here, like calculation output, number of items affected, etc.. to the main thread.
                e.Result = "Duration: " + duration.TotalMilliseconds.ToString() + " ms.";
            //}
            //catch(Exception ex){
            //    MessageBox.Show("Don't use try catch here, let the backgroundworker handle it for you!");
            //}
        }


        
        //This event is raised on the main thread.
        //It is safe to access UI controls here.
        private void backgroundWorker1_ProgressChanged(object sender, 
            ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage; //update progress bar
            
            DateTime time = Convert.ToDateTime(e.UserState); //get additional information about progress
            
            //in this example, we log that optional additional info to textbox
            txtOutput.AppendText(time.ToLongTimeString());
            txtOutput.AppendText(Environment.NewLine);            
        }



        //This is executed after the task is complete whatever the task has completed: a) sucessfully, b) with error c)has been cancelled
        private void backgroundWorker1_RunWorkerCompleted(object sender, 
            RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled) {
                MessageBox.Show("The task has been cancelled");
            }
            else if (e.Error != null)
            {                
                MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());
            }
            else {
                MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());
            }
            
        }




        private void btoCancel_Click(object sender, EventArgs e)
        {
            //notify background worker we want to cancel the operation.
            //this code doesn't actually cancel or kill the thread that is executing the job.
            backgroundWorker1.CancelAsync();
        }

        private void btoStart_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

      
    }
}

Download sample project

Download BackgroundWorker.ZIP (C# sample project)

kick it on DotNetKicks.com AddThis Social Bookmark Button

If you go to Build-> Publish Web Site menu on Visual Studio 2005, you will find this publishing screen:

If you UNCHECK the "Allow this precompiled site to be updatable", all the aspx pages within the projects will be generated empty. To be more exact, you will find the following unique line on them:

This is a marker file generated by the precompilation tool, and should not be deleted!

If you compare this screenshot:

with this one:

you will realize how the first was published allowing ASPX pages to be updated and the second not (notice how in second screenshot, all the ASPX files have 1kb file size)

Don't worry!, All HTML markup and inline code is compiled into assemblies on the bin folder, and it is correctly executed and rendered when the page is requested.

This way, nobody would be able to change either code or HTML content after the publishing is done!

kick it on DotNetKicks.com AddThis Social Bookmark Button

I am sure you know how to add the predefined TextBox or ComboBox controls to a Windows Forms context Menu:

Just wanted to quickly show you how you can add your own custom controls to that context menu by using the ToolStripControlHost class, like this (the yellow panel is a user control):

Here is a short video:

And here is the code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //instance my control
            UserControl1 c = new UserControl1();

            //pass as parameter to ToolStripControlHost instance
            ToolStripControlHost host = new ToolStripControlHost(c);

            //add ToolStripControlHost object to context menu. simple!
            contextMenuStrip1.Items.Add(host);
        }
    }
}


 

kick it on DotNetKicks.com AddThis Social Bookmark Button

ASP.NET MVC Validation of viewstate MAC failed error

Posted in MVC | ASP.NET | .NET 3.5 at Saturday, December 13, 2008 2:30 AM UTC

If you get a Validation of viewstate MAC failed error on ASP.NET MVC, before trying to hard code your <machineKey attribute in web.config, disabling the keying of viewstate

<system.web>
  <pages enableViewStateMac="false" />
</system.web>

, or any other literature you may want to check if the page you are posting data from has two "<form" attributes (the one hard coded on the page and the one created with Html.BeginForm()). If you play with master templates and have to merge some MVC pages with tradicional ASP.NET you may get to that error.

It took me a while to find the solution...

Did this post save you some time? post it. I'm curious. :) thanks.

kick it on DotNetKicks.com AddThis Social Bookmark Button

Dynamic DataGridView filtering in C#

Posted in .NET 2.0 | .NET UI Controls | DataGridView at Wednesday, December 03, 2008 9:31 PM UTC

In a few lines of code, we can convert a DataGridView control in a powerfull fully searchable control with dynamically generated filters for each column of field the DataGridView is binded to.

How does it work?

  1. The DataGridControl is binded to the datasource.
  2. The user hits the button (or shortcut) to start the dynamic filtering feature.
    1. A new form is dinamycally generated on top of the DatagridView control (it has some transparency on it so you can still see the DataGridView data on the background)
    2. The form contains several textboxes, one per each column or field the DataGridView datasource contains (you can change that behaviour)
    3. When the user starts entering text in any of the textboxes, the filter condition is applied to the underlying datasource (for every keystroke) so you can see how the filtered data looks on the DataGridView as you are typing.
    4. You can, of course define filters for one or more textboxes (fields) at the time, so you can work with multiple filters for many columns/fields.
  3. You can choose whatever you create a dynamically generated search form that contains all the fields on DataGridView datasource, or the ones of your choice (video and example code shows both cases).

Screenshots


This screenshot shows the dynamically search form generated with all the fields of the underlying DataGridView

This screenshot shows the dynamically search form generated with some of the fields of the underlying DataGridView

Demo Video


(sorry about the video quality. if you are interested, please download the demo. it may be worthy)

Some code:

 /// 
        /// Build texbox controls for dinamyc search form based on fields collection
        /// 
        /// 
        private void BuildControls(List fields) {
            int top = 10;
            bool focused = false;

            #region Loop for each field
            foreach (field f in fields)
            {
                Label label = new Label();
                label.Text = f.FriendlyName + ":";
                label.Top = top;
                label.Left = 5;
                label.AutoSize = true;
                this.Controls.Add(label);

                TextBox textbox = new TextBox();
                textbox.TextChanged += new EventHandler(textBox_TextChanged);
                textbox.Tag = f.Field;
                textbox.Top = top;
                textbox.Left = 68;
                textbox.Width = this.Width - 80;
                if (!focused) {

                    textbox.Focus(); //the first control focused
                    focused = true;
                }
                top += 35;
                this.Controls.Add(textbox);
            }
            #endregion

            this.Height = top + 30;
        }


  // Raise event to parent form if textbox content changes
        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (TextChanged != null)
                TextChanged(GetFilterValues());
        }


        #region Shortcuts
        private void frmSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Escape) || (e.KeyCode == Keys.Enter))
                Close();
        }
        #endregion


namespace dynamicGridFilter
{
    /// 
    /// This struct contains information about each field to be filtered along with filter value, if exists.
    /// 
    public struct field {
        public string Field;
        public string FriendlyName;
        public string Value;
    }
}


namespace dynamicGridFilter
{
    public delegate void SearchContextChangedHandler(List fields);
}

Complete source code available to download:

Includes full source code and compiled binary (use it at your own risk)
dynamicGridFilter.zip (50,23 KB)

What's next?!

If could have gone all the way up to creating a custom control that inherits from DataGridView and has all the logic embedded. Well, you have here all you need for doing it yourself, so don't call me lazy! :P

Comments are welcome!


kick it on DotNetKicks.com AddThis Social Bookmark Button

Really really simple. Feel free to write your own custom authentication method to fit your project context.

This is download.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="download.aspx.cs" Inherits="download" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Download Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

and this is the code behind that file:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class download : System.Web.UI.Page 
{

    /// 
    /// Show error to user and close response object.
    /// 
    /// 
    private void WriteError(string error) {
        Response.Write(error);
        Response.End();
    }

    /// 
    /// Check authentication ticket
    /// 
    /// 
    private bool Authenticated() {
        //whatever is a session ticket, membership provider base, container in a coded URL querystring parameters, etc..
        return true;    
    }

    private string GetRepositoryFolder() {
        System.Configuration.AppSettingsReader r = new AppSettingsReader();
        return r.GetValue("RepositoryFolder", typeof(string)).ToString();                
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Authenticated()){
            WriteError("You are not allowed to download this file");
            return;
        }
        else if (Request.QueryString["id"] == null)
        {
            WriteError("Missing parameter : id");
            return;
        }       
        
        string filePath = System.IO.Path.Combine(GetRepositoryFolder(),Request.QueryString["id"]);
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (!file.Exists)
        {
            WriteError("File doesn't exists");
            return;
        }
        else {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }        
    }
}
If successfully authenticated, you will be able to directly download the file:


If not, you will get an error message:

You are not allowed to download this file

kick it on DotNetKicks.com AddThis Social Bookmark Button