|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe advent of .NET Framework is the most significant event in the programming world over the last 10 years and I think it is still underestimated. It is the most solid and comprehensive framework ever. However, nothing and nobody is perfect. This article is about .NET Framework communication and compact framework communication (serialization) in particular. Communication System ModelWhat is a communication framework? Generally a communication framework is an object delivery system. No more and no less. If this system implements RPC/RMI, another layer is built on top of that. The object delivery system in turn consists of just two sub layers:
The rest can be hidden inside this ideal communication framework. Minimal (Optimal) Communication SystemMinimal communication system has just one method WCFWhen I first looked at WCF, I had a feeling that I was missing something. It could not be true: automatic generic serialization had gone! Instead it has a semi manual very restrictive serialization. Judge for yourself: public class Person
{
public string FirstName;
public string LastName;
public int ID;
ArrayList alist = new ArrayList();
public ClassB _b;
}
In WCF, the decoration of such a primitive object for serialization looks like: [DataContract(Name = "Customer", Namespace = "http://www.contoso.com")]
class Person : IExtensibleDataObject
{
[DataMember()]
public string FirstName;
[DataMember]
public string LastName;
[DataMember()] public int ID;
[DataMember()]
ArrayList alist = new ArrayList();
[DataMember()] public ClassB _b;
private ExtensionDataObject extensionData_Value;
public ExtensionDataObject ExtensionData
{
get
{
return extensionData_Value;
}
set
{
extensionData_Value = value;
}
}
}
Does everybody like that? Apparently not. The Web is full of blogs suggesting numerous "smart" solutions overcoming the restrictive nature of WCF. The limited WCF serialization is "successfully" worked around by using What Makes the Communication Framework OS SpecificThe only thing that makes the system OS specific is the formatter (the serializer). It is the only component in the system that converts the stream to the object. The stream is always the same. As the sculpture is made of stone (any stone) so the object is made of stream. In fact, WCF has all these bits and pieces but the way the system is composed is far from perfect. InteroperabilityAs I already mentioned, the Serializer makes the framework OS specific. In turn the serializer exists in the framework [say .NET]. What if the OS does not support the frameworks and does not have even the notion of the serializer? In this particular case, manual (or semi manual) processing of the byte stream is appropriate but that should be an option, not a default. If You Like Everything Automatic, Why Not to Use Remoting?Standard remoting is based on just synchronous communication model. It is unidirectional. The attempts to use call backs for the duplex communication do not work with the client behind proxy or NAT because Remoting opens a second connection for the events. Since the firewall is a standard network component, we may forget about bidirectional communication using remoting. Total Recall.NET standard communication suffers from the same disease that Windows OS suffered 10 years ago. Core Windows API is the same as it was 10 years ago, but the programming of Windows Form application 10 years back was a serious ordeal. Why? Because the programmer had to provide all the parameters manually. In order to provide all the parameters, he needed to know all Windows internals and how to use them. In .NET environment, all the defaults are set by the framework. We drag the controls from the toolbox and the rest is done automatically. If we need extra features, we can select them from the property grid or type them in the code. In real life, we send the object (the letter or the parcel) to the recipient. Do we really care how the letter is delivered, what is the postman's name or the name of his pet dog? No, we don't. Usually we drop the letter in the postbox and the rest is done by default. If we want some extra features, like better security or delivery confirmation, we can get that as an extra. If the letter delivery looked like .NET communication (WCF in particular), the procedure of the letter delivery would have certainly been like that: we select from the catalog the number plate of the car that carries the letter, the name of the driver, the flight number, the type of the plane, envelope color, brand …. Most probably that would stop us from sending the letter at all. AlternativesWhile high priests of IT industry keep saying that everything is better than ever, the communication frameworks (mostly .NET socket based) keep multiplying. Compact Framework RemotingUnfortunately Remoting and Binary formatter is not implemented (and apparently will not be) for the compact framework. The only choice is using limited Compact Framework SerializationCompactFormatterPlus : Generic Binary Serializer for Compact FrameworkThis work is based on a brilliant Angelo Scotto's CompactFormatter for the full and Compact Framework. The original
Extreme Performance and Serialization StudioOriginal Instead now there is just one custom serialization – an override. Override (in Angelo's Scotto terms) is the original mechanism that allows for injecting custom serialization modules for the specific class. Also Adding the fast serializer to CompactFormatterPlus cfp = new CompactFormatterPlus();
MyFastSerializer mfs = new MyFastSerializer();
cfp.addFastSerializer(mfs);
What if the Fast Serializer cannot serialize the object? The example of this situation can be illustrated by the code below: public class MyClass1
{
public int SomeInteger;
public object UnknownObject;
}
If the fast serializer is generated for this class, Using the CodeOn the PC side, the classes that have to be serialized should be included in the solution as the source code or the library. In the case of library, it has to be referenced in the PC project. Your class must have the same namespace name and the class name as the namespace and the class name on PDA. All the above are true for PDA. Sometimes it is possible to use CF files (EXE and DLLs) on PC and PC files on PDA. But that is a bad practice. It may work or fail with a weird and irrelevant message. CompactFormatterPlus Serializable Types
CreditsThe user by the name Fabien Castell has improved the typed dataset serialization and the instantiation of the objects if they are defined in different assemblies. The code can be downloaded from the original website. New VersionIf a newer version is released, it can be downloaded here.
|
||||||||||||||||||||||||||||||||