Tuesday, October 17, 2006

Remoting vs XML Web Services


So which to use?

They share alot of similarities but also alot of differences.

XML Web services built using cross-platform standards based on xml messaging. The are execute by the ASP.NET runtime thus gaining ASP.NET features like output caching. An important point is that it is fundamentally stateless. The best uses for XML web services is when cross platform boundaries or trust boundaries are required.

Remoting is a .NET specific technology for building distributed objects. It supperceeds DCOM. Perfect for in-house systems that need to communicate and are built using the same .NET platform. It allows for different types of communication, such as sharing binary messages to lean TCP/IP connections. If you want stateful objects and bidirectional communication via callbacks then this is your technology. It also allows you to send custom .NET objects over the wire.
Object Instantiation with Reflection


So i need to instantiate objects at runtime. Whats the best way of doing this? Reflection !

What you need to do is obtain the Type object representing the type of object you want to instantiate then call the Type.GetConstructor method. This will give you the System.Reflection.ConstructorInfo that represents the constructor you want to use. Then execute the ConstructorInfo.Invoke method.


Code : sample

----Type type = typeof(StringBuilder);
----Type[] argTypes = new Type[] {typeof(System.String), typeof(System.Int32);}

----ConstructorInfo cInfo = type.GetConstructor(argTypes);

----object[] argVals = new object[] {"something", 30};
----StringBuilder sb = (StringBuilder) cInfo.Invoke(argVals);
Application Domain Creation



This feature lets you isolate your application safely from others. Load assemblies into your application domain and if it dies it wont kill anything else!

Use SystemAppDomain.CreateDomain.

Sample Code:

----AppDomainSetup sup = new AppDomainSetup();

----sup.ApplicationBase = @"c:\RootDir";
----sup.ConfigurationFile = "test.Config";
----sup.PrivateBinPath = "bin;external;";

----AppDomain newDom = AppDomain.CreateDomain("A new domain", null, setupInfo);


** Once you've created the application domain you need to keep a reference to it as there is no known method of getting a list of created application domains.
Serializable object to a file


Simple steps, take a deep breath and follow this list carefully. Its not as hard as it may sound!

Use a formatter to serialize the object and write it to the System.IO.FileStream object.

formaters available:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter

example:

public static void BinarySerialization(ArrayList list){
--using (FileStream str = File.Create("stuff.bin")){
----BinaryFormatter bf = new BinaryFormatter();
----bf.Serialize(str, list);
--}
}


public static ArrayList BinaryDeserialization(){
--ArrayList stuff = null;
--using (FileStream str = File.OpenRead("stuff.bin")){
----BinaryFormatter bf = new BinaryFormatter();
----stuff = (ArrayList)bf.Deserialize(str);
--}
--return stuff ;
}
Sorting an Array/ArrayList


So you have an array with alpha elements that you want sorted?

Use the ArrayList.Sort or Array.Sort its that simple

As long as the array contains elements that are of datatypes that implement the IComparable interface then it can be sorted. The basic datatypes all support this interface!

So what happens if some of the elements don't support this interface? You must pass the Sort method to an object that implements the System.Collections.IComparer interface.