Thursday, March 12, 2009

.Net Remoting


HI,


In this post we will see what .Net Remoting is and go through an example.


Basics


.Net Remoting provides you the way to build distributed application, either on the same computer or across the entire world. Your client application can use the objects from other application/processes on the same computer or any computer that is reachable over the network. Basically Remoting allows or gives platform to communicate with other application domains easily.


Code running in one application cannot directly access code or resources from another application. The common language runtime enforces this isolation by preventing direct calls between objects in different application domains. Objects that pass between domains are either copied or accessed by proxy. If the object is copied, the call to the object is local. That is, both the caller and the object being referenced are in the same application domain. If the object is accessed through a proxy, the call to the object is remote. In this case, the caller and the object being referenced are in different application domains. Cross-domain calls use the same remote call infrastructure as calls between two processes or between two machines.


Remoting allows you to choose any of the application models for communication; you can use Web application, Windows application or Console application for communication between the objects/application. Similarly, Remoting servers can also be any type of application domain. Any application can host remoting objects and provide its services to any client on its computer or network.


While using .Net Remoting where two components communicate directly with each other you need to build three things:

- Remotable Object

- Host application domain: Listen to the request from the client application to provide object.

- Client application domain: Makes request to the host for getting the object.


The real strength of the remoting system, however, resides in its ability to enable communication between objects in different application domains or processes using different transportation protocols, serialization formats, object lifetime schemes, and modes of object creation. In addition, remoting makes it possible to intervene in almost any stage of the communication process, for any reason.


Architecture


Communication between client and server object references is the base of the Remoting. Once you start or create server object and client receive the reference to the same, your application will be ready to use all resources and functionality of the server object.


.Net Remoting uses the proxy objects to create an instance of the remote type:

- Proxy objects to create the impression that the server object is in the client's process.

- Proxies are stand-in objects that present themselves as some other object.

- When client creates an instance of the remote type or server object, .Net Remoting creates a proxy object, which looks very similar like remote type.

- Client calls methods on proxy object then:

1. Remoting system receives the call,

2. Routes it to the server process,

3. Invokes the server object,

4. Returns the return value/object to client proxy,

5. And finally client proxy returns the value/object to client.


While doing all these things, .Net Remoting requires opening a network connection and use the particular protocol to the receiving application, is called transport channel. A channel is a type that takes a stream of data, creates a package according to a particular network protocol, and sends the package to another computer. Some channels can only receive information, others can only send information, and still others, such as the default TcpChannel and HttpChannel classes, can be used in either direction.


Although the server process knows everything about each unique type, the client knows only that it wants a reference to an object in another application domain, perhaps on another computer. From the world outside the server application domain, a URL locates the object. The URLs that represent unique types to the outside world are activation URLs, which ensure that your remote call is made to the proper type.


Below image shows the general remoting process:















Above figure shows the complete flow in the .Net Remoting, if the network connection is set properly, remoting system creates a proxy object and returns it to the client object. Client uses this proxy object as remote type, calls its method’s, the call gets transfers from client to proxy object and then remoting system transfers the call to the server object and returns the return value/object.


Here, channel is shown as the communication link between the Client Object and Server Object which runs over the TCP-IP protocol.


In the next post we will the working example of the .Net Remoting.


Thanks,


Paresh Bhole

Thursday, March 5, 2009

C#.Net 3+ - Automatic Properties, Object Initializers & Collection Initializers

Hi,


In this post we will see C# 3+ features Automatic Properties, Object Initializers & Collection Initializers.


Automatic Properties


Automatic Properties provides the technique to reduce the efforts declaring private field and implementing get/set property for the same. Now in C# 3+ you can just declare the get/set property without having the private field as shown below.


Below is the traditional declaration of the private field and its get/set property.


namespace AutomaticProperties

{

public class AutomaticProperties

{

private string firstName;

private int empNumber;


public string FirstName

{

get

{

return firstName;

}

set

{

firstName = value;

}

}


public int EmpNumber

{

get

{

return empNumber;

}

set

{

empNumber = value;

}

}

}

}


Now, see below one in C# 3+: It only contains the public property with get/set.


namespace AutomaticProperties

{

public class AutomaticProperties

{

public string FirstName

{

get;

set;

}


public int EmpNumber

{

get;

set;

}

}

}


The compiler automatically generates the private field and the default get/set operations, after which the class will look exactly same as the one shown in first example.


Object Initializers


Using Object initializers feature you can create & assign values to all accessible fields/properties of Object. It do not require to explicitly invoke a class constructor.


Below code shows the previous way to create an instance and assign values to fields/properties of Object.


Employee paresh = new Employee();

paresh.FirstName = "Paresh";

paresh.EmpNumber = 3282;


In C#.Net 3+ you can create and assign values to fields/properties of Object as shown below.


Employee mayur = new Employee{

FirstName = "Paresh";

EmpNumber = 3282;

};


Object Initializers are mostly useful in LINQ query expressions and

1. The new object initialization syntax is more inline with the array initialization syntax

2. The new syntax clearly separates what is being initialized as part of the object construction process (i.e. everything in the ()) and what is being initialized with property setters (i.e. everything in the {}).


Collection Initializers


Collection Initializers are very much same as Object Initializers. it resembles with array intializer.


Below code shows the way we used to initialize a collection using Object Initializers.


List<Employee> people = new List<Employee>();

people.Add( new Employee { FirstName = "Scott", EmpNumber = 32 } );
people.Add( new Employee { FirstName = "Bill", EmpNumber = 50 } );
people.Add( new Employee { FirstName = "Susanne", EmpNumber = 32 } );


Now, you can initialize an collection using below code also.


List<Employee> people = new List<Employee> {
new Employee { FirstName = "Scott", EmpNumber = 32 },
new Employee { FirstName = "Bill", EmpNumber = 50 },
new Employee { FirstName = "Susanne", EmpNumber = 32 }
};


When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.


Thanks,


Paresh Bhole

Tuesday, March 3, 2009

C#.Net 3+ - Extension Methods

Hi,


This post summarizes Extension Methods, new feature provided by .net framework 3+ / ORCAS.


As name suggest Extension Methods are like you extends the existing features provided to the primitive data types. Like int data type provides you int.ToString() method, that converts the int to string, you can also add your own feature to int type.


Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.


Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.


In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.


How to do it:


As stated above, you can add new feature to int type, we will see an example of Extension Method.

Suppose you want to check whether the int is even or odd number, below Extension Method checks whether the number is even or odd and returns true or false.


namespace TemplatesCl

{

public class ExtensionMethod

{

public static bool IsEvenNumber(this int value)

{

return (value % 2) == 0 ? true : false;

}

}

}


Above method “IsEvenNumber” is an extension method to the int data type, which returns whether the number is an Even or Odd number.


Below is an example to use this extension method.

Extension method are easy to use, so when I hit the "." keyword on a int variable, my extension methods will now show up in the intelligence drop-down list.


public string TestExtensionMethod(int num)

{

if (num.IsEvenNumber())// LINE A

{

return "Provided number is Even";

}

else

{

return "Provided number is Odd";

}

}


As you can see from the above snippet, the differences between the extension method (IsEvenNumber):

1. Extension methods have the keyword this before the first argument, while static methods do not have the this keyword in its argument declaration.

2. When extension methods are consumed, the argument that was declared with the keyword this is not passed. In the above code, Line A is an example of consuming the extension method IsEvenNumber. No argument is passed to it. When static methods are consumed, no arguments are skipped.

3. Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.

4. Extension methods can be called only on instances values.


Extension methods, though static in nature, can be called only on instances. Trying to call them on a class will result in compilation errors. The class instances on which they are called are determined by the first argument in the declaration, the one having the keyword this.


Thanks,


Paresh Bhole

Hello

Thanks for checking out my post...

... Paresh Bhole