Thursday, 2 August 2012

DataContractSerializer in WCF



What is Serialization?

Let’s start with the basics.  Serialization has been a key part of .Net since version 1.  It is basically the process of converting an object instance into a portable and transferable format.  The objects can be serialized into all sorts of formats.  Serializing to Xml is most often used for its interoperability.  Serializing to binary is useful when you want to send the object from one .Net application to another.  .Net even supports the interfaces and base classes to build your own serializes. There are libraries out there to serialize to comma delimited strings, JSON, etc.
Deserialization is basically the reverse of serialization.  Its the process of taking some data (Xml, binary, etc) and converting it back into an object.

What is the XmlSerialzer?

For those that may not be familiar with System.Xml.Serialization.XmlSerializer let’s go over it briefly.  This is the xml serializer that has been around since .Net version one.  To serialize or deserialize an object, you basically just need to create an instance of the XmlSerializer for the type you want to work with, then just call Serialize() or Deserialize().  It works with streams, so you could serialize to any stream such as an MemoryStream, FileStream, etc.
// Create serializer for the type
System.Xml.Serialization.XmlSerializer xmlSerializer =
    new System.Xml.Serialization.XmlSerializer(typeof(MyType)); 
 
// Serialize from an object to a stream
xmlSerializer.Serialize(stream, myInstanceOfMyType); 
 
// Deserialize from a stream to an object
myInstanceOfMyType = (MyType)xmlSerializer.Deserialize(stream);

What is the DataContractSerializer?

The System.Runtime.Serialization.DataContractSerializer is new in .Net 3.0 and was designed for contract-first development and speed.  Specifically it was brought in to be used by Wcf, but can be used for general serialization as well. Using the DataContractSerializer isn’t that much different than using the XmlSerializer.  There are a few more options, but the only real key difference is that you use a WriteObject() method to serialize instead of a Serialize() method and a ReadObject() method to deserialize instead of a Deserialize() method.  It works with the same types of streams, so you can write to memory, files, etc.
DataContractSerializer dataContractSerializer =
    new DataContractSerializer(typeof(MyType)); 
 
// Serialize from an object to a stream
dataContractSerializer.WriteObject(stream, myInstanceOfMyType); 
 
// Deserialize from a stream to an object
myInstanceOfMyType = (MyType)dataContractSerializer.ReadObject(stream);







A The XmlSerializer has been in .Net since version 1.0 and has served us well for everything from Remoting, Web Services, serializing to a file, etc. However in .Net 3.0 the DataContractSerializer came along.  And all of a sudden a lot of guidance suggests that we should use it over the old tried and true XmlSerializer. Wcf even uses this as the default mechanism for serialization.  The question is, “Is it really better?”.  The verdict is yes, and no.  Like most things it depends on your implementation and what you need.  For Wcf, you should prefer to use the DataContractSerializer.  If you need full control over how the xml looks though, you should go back to the XmlSerializer.
Lets look at the both of these in detail and leave it up to you to decide which is best for your implementation.  Here are a few of the advantages and disadvantages of each of them:


Advantages:
1. Opt-out rather than opt-in properties to serialize. This mean you don’t have to specify each and every property to serialize, only those you don’t wan to serialize2. Full control over how a property is serialized including it it should be a node or an attribute
3. Supports more of the XSD standard
Disadvantages:
1. Can only serialize properties
2. Properties must be public
3. Properties must have a get and a set which can result in some awkward design
4. Supports a narrower set of types
5. Cannot understand the DataContractAttribute and will not serialize it unless there is a SerializableAttribute too
Advantages:
1. Opt-in rather than opt-out properties to serialize. This mean you specify what you want serialize
2. Because it is opt in you can serialize not only properties, but also fields.  You can even serialize non-public members such as private or protected members. And you dont need a set on a property either (however without a setter you can serialize, but not deserialize)
3. Is about 10% faster than XmlSerializer to serialize the data because since you don’t have full control over how it is serialize, there is a lot that can be done to optimize the serialization/deserialization process.
4. Can understand the SerializableAttribute and know that it needs to be serialized
5. More options and control over KnownTypes
Disadvantages:
1. No control over how the object is serialized outside of setting the name and the order



Below example class setup to use the DatContractSerializer.  Notice that I am explicitly setting the DataMemberAttribute on the properties I want to serialize, but not on the others.
[DataContract]
public class Individual
{
    private string m_FirstName;
    private string m_LastName;
    private int m_SocialSecurityNumber; 
 
    [DataMember]
    public string FirstName
    {
        get { return m_FirstName; }
        set { m_FirstName = value; }
    } 
 
    [DataMember]
    public string LastName
    {
        get { return m_LastName; }
        set { m_LastName = value; }
    } 
 
    public int SocialSecurityNumber
    {
        get { return m_SocialSecurityNumber; }
        set { m_SocialSecurityNumber = value; }
    } 
 
    public Individual()
    {
    }
    public Individual(string firstName, string lastName)
    {
        m_FirstName = firstName;
        m_LastName = lastName;
    }
}

One other important thing to talk about with the DataContractSerializer are the ServiceKnownTypeAttribute and KnownTypeAttribute attributes.  These are similar to the XmlIncludeAttribute used by the XmlSerializer.  When used in Wcf, these identify what types should be represented in the WSDL that is generated.
The KnownTypeAttribute specifies types that should be recognized by the DataContractSerializer when serializing and deserializing a type.  It is applied to a class and basically specifies what other types are used in the class.  You don’t need to specify known .Net types, but any custom classes should be added here.  This attribute can be used multiple times to identify multiple types.
[DataContract]
[KnownType(typeof(MyOtherType))]
public class MyType
{
    [DataMember]
    public MyOtherType TheOtherType;
} 
 
[DataContract]
public class MyOtherType
{
    [DataMember]
    public string MyValue;
}
The ServiceKnownTypeAttribute specifies known types to be used by a service when serializing or deserializing.  It is applied to a ServiceContract or to an OperationContract and specifies what types are used in the methods.  Again (like the KnownTypeAttribute), you don’t need to specify known .Net types and this attribute can be used multiple times to identify multiple types.
[ServiceContract]
[ServiceKnownType(typeof(MyType))]
[ServiceKnownType(typeof(MyOtherType))]
public interface MyService
{
    [OperationContract]
    [ServiceKnownType(typeof(YetAnotherType))]
    void MyMethod();
}

When to use which serializer?

For Wcf, you should prefer to use the DataContractSerializer.  If you need full control over how the xml looks though, you should go back to the XmlSerializer.   If you are doing general serialization, it is up to you, but I would weigh out the advantages and disadvantages.  I would still prefer the DataContractSerializer for the same reasons I prefer it for Wcf.  I do not recommend using the NetDataContractSerializer unless you have to.  You can lose too much interoperability and its not as descriptive.
If you need some custom xml serializer, by all means go ahead and implement it.  Wcf supports any serializer you can throw at it.  Just be careful not to “reinvent the wheel”.  The XmlSerializer is very configurable and may suit your needs. If that doesn’t work, then ISerializable gives you full control.

Saturday, 7 July 2012

Migrating ASP.NET Web Services to WCF

Migrating ASP.NET Web Services to WCF


Migration is a key issue in existing applications and many a times we get confused as to what to do and how to in migration related tasks. Some of the key points of the MSDN migration article are as follows:

WCF has several important advantages relative to ASP.NET Web services. While ASP.NET Web services tools are solely for building Web services, WCF provides tools that can be used when software entities must be made to communicate with one another. This will reduce the number of technologies that developers are required to know in order to accommodate different software communication scenarios, which in turn will reduce the cost of software development resources, as well as the time to complete software development projects.

Even for Web service development projects, WCF supports more Web service protocols than ASP.NET Web services support. These additional protocols provide for more sophisticated solutions involving, amongst other things, reliable sessions and transactions.

WCF supports more protocols for transporting messages than ASP.NET Web services. ASP.NET Web services only support sending messages by using the Hypertext Transfer Protocol (HTTP). WCF supports sending messages by using HTTP, as well as the Transmission Control Protocol (TCP), named pipes, and Microsoft Message Queuing (MSMQ). More important, WCF can be extended to support additional transport protocols. Therefore, software developed using WCF can be adapted to work together with a wider variety of other software, thereby increasing the potential return on the investment.

WCF provides much richer facilities for deploying and managing applications than ASP.NET Web services provides. In addition to a configuration system, which ASP.NET also has, WCF offers a configuration editor, activity tracing from senders to receivers and back through any number of intermediaries, a trace viewer, message logging, a vast number of performance counters, and support for Windows Management Instrumentation.
Given these potential benefits of WCF relative to ASP.NET Web services, if you are using, or are considering using ASP.NET Web services you have several options:
  • Continue to use ASP.NET Web services, and forego the benefits proffered by WCF.
  • Keep using ASP.NET Web services with the intention of adopting WCF at some time in the future. The topics in this section explain how to maximize the prospects for being able to use new ASP.NET Web service applications together with future WCF applications. The topics in this section also explain how to build new ASP.NET Web services so as to make it easier to migrate them to WCF. However, if securing the services is important, or reliability or transaction assurances are required, or if custom management facilities will have to be constructed, then it is a better option to adopt WCF. WCF is designed for precisely such scenarios.
  • Adopt WCF for new development, while continuing to maintain your existing ASP.NET Web service applications. This choice is very likely the optimal one. It yields the benefits of WCF, while sparing the cost of modifying the existing applications to use it. In this scenario, new WCF applications can co-exist with existing ASP.NET applications. New WCF applications will be able to use existing ASP.NET Web services, and WCF can be used to program new operational capabilities into existing ASP.NET applications by virtue of WCF ASP.NET compatibility mode.
  • Adopt WCF and migrate existing ASP.NET Web service applications to WCF. You may choose this option to enhance the existing applications with features provided by WCF, or to reproduce the functionality of existing ASP.NET Web services within new, more powerful WCF applications.
Happy Programming ! !
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
-----------------------------------------------------------
---------------------------------------------------------------

Choosing the Right Collection Class


Choosing the Right Collection Class

In .net we have number of collection classes that are very useful in managing collections of object. But the question is which one should we use to get the best performance. The answer is that what is the scenario and the output that one requires from the collection, depending on which the selection of the collection type should be made. The following table explains the basic parameters for collections that should be kept in mind while selection the type of collection.


Collection
Ordered?
Contiguous Storage?
Direct Access?
Lookup Efficiency
Notes
Dictionary
No
Yes
Via Key
Key:
O(1)
Best for high performance lookups.
Sorted
Dictionary
Yes No Via Key Key:
O(log n)
Compromise of Dictionary speed and ordering, uses binary search tree.
SortedList
Yes
Yes
Via Key
Key:
O(log n)
Very similar to SortedDictionary, except tree is implemented in an array, so has faster lookup on preloaded data, but slower loads.
List
No
Yes
Via Index
Index: O(1)
Value: O(n)
Best for smaller lists where direct access required and no ordering.
LinkedList
No
No
No
Value:
O(n)
Best for lists where inserting/deleting in middle is common and no direct access required.
HashSet
No
Yes
Via Key
Key:
O(1)
Unique unordered collection, like a Dictionary except key and value are same object.
SortedSet
Yes
No
Via Key
Key:
O(log n)
Unique ordered collection, like SortedDictionary except key and value are same object.
Stack
No
Yes
Only Top
Top: O(1)
Essentially same as List except only process as LIFO
Queue
No
Yes
Only Front
Front: O(1)
Essentially same as List except only process as FIFO

Happy Programming ! !
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

Thursday, 7 June 2012

How to get Motherboard serial Number




Hi Friends,


Last week I was working on one window application. In this application we have to get serial number of motherboard.

Following code describes how to get serial number of motherboard.

You need to add a reference to System.Management.dll to your project.


  ManagementScope ms = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
  ms.Connect();
 ManagementObject wmiClass = new ManagementObject(ms, new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());
string sn = wmiClass.Properties.Cast<PropertyData>().Where(p => p.Name == "SerialNumber").FirstOrDefault().Value.ToString();

  MessageBox.Show(sn);

 

  





In the above code
  • 1.  "\\root\\cimv2: root is because it's the root of the tree. cimv2 is actually CIMv2, because it's version 2 of the CIM (Common Information Model).
  • 2.  ManagementScope() : Initializes a new instance of the Management Scope class, with default values. This is the default constructor.




Happy Programming ! !
If you have any query mail me to Sujeet.bhujbal@gmail.com     

Regards
Sujeet Bhujbal

--------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------