Thursday, 27 January 2011

Example code For .Net Remoting

create class library name as Sample Object using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
public class SampleObject : MarshalByRefObject
{
public SampleObject() // Constructor
{
}public string HelloWorld() // return string{return "Hello World!";}}} Create a Server To Expose the Remotable Object Create a new C# console application project. Add a class called SampleServer
namespace CodeGuru.Remoting
{
public class SampleServer
{
public static int Main(string [] args)
{
TcpChannel channel = new TcpChannel(8080);// Create an instance of a channel
ChannelServices.RegisterChannel(channel);

//Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(SampleObject), "HelloWorld", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
return 0;
}
}}

Create a Client To Use the Remotable Object


public class SampleClient
{
public static int Main(string [] args)
{
// Create a channel for communicating w/ the remote object
// Notice no port is specified on the clientTcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object
SampleObject obj = (SampleObject) Activator.GetObject
(typeof(CodeGuru.Remoting.SampleObject),"tcp://localhost:8080/HelloWorld" );
// Use the object
if( obj.Equals(null) )
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
Console.WriteLine(obj.HelloWorld());
}
return 0;
}
Create a new C# console application project. Add a class called SampleClient

Difference Between WebService and .Net Remoting


  • ASP.NET based Web services can only be accessed over HTTP.
  • .NET Remoting can be used across any protocol. (TCP.Http)
  • Web services work in a stateless environment where each request results in a new object created to service the request.
  • .NET Remoting supports state management options and can correlate multiple calls from the same client and support callbacks.Web services are easy to create and use while .NET Remoting are complex  to be created
  • Web services serialize objects through XML contained in the SOAP messages and can thus only handle items that can be fully expressed in XML.
  • .NET Remoting relies on the existence of the common language runtime assemblies that contain information about data types. This limits the information that must be passed about an object and allows objects to be passed by value or by reference.
  • Web services support interoperability across platforms and are good for heterogeneous environments.
  • .NET Remoting requires the clients be built using .NET, or another framework that supports .NET Remoting, which means a homogeneous environment.

Remote objects:

Asp.net Remote objects:

Any object outside the application domain of the caller application should be considered remote
Remote objects are accessed through Channels ,two existing channels TcpChannel and HttpChannel

Types of .Net remotable objects
  • Single Call=Single Call objects service one and only one request coming in.
  • Single Call objects are usually not required to store state information.
  • Singleton Objects =Singleton objects are those objects that service multiple clients, and hence share data by storing state information between client invocations
  • Client Activated Objects are server-side objects that are activated upon request from
    client . When the client submits a request for a server object using a "new" operator, an activation request message is sent to the remote application. The server then creates an instance of the requested class, and returns an ObjRef back to the client application that invoked it

What is .NET Remoting?

  •  .NET Remoting is a mechanism for communicating between objects which are not in the same process. It is a generic system for different applications to communicate with one another. .NET objects are exposed to remote processes, thus allowing inter process communication. The applications can be located on the same computer, different computers on the same network, or on computers across separate networks.

  • Inter process communication between application was handled through Distributed COM.

  • .Net Remoting eliminates the difficulties of DCOM by supporting different transport protocol formats and communication protocols

Checked and Unchecked Keyword in .Net

    Checked Key word is used to enforce the compiler to check the buffer overflow while operating arithmetic operators. If there is any Buffer overflow code will throw exception.
     static void Main(string[] args)
            {
                int _value1 = 1500056564;
                int _value2 = 1200046565;
                checked
                {
    // this Code will give exception BuferOverflow
                            System.Console.WriteLine(_value1 * _value2);                System.Console.Read();
    }
    }
    Unchecked Keyword is used to enforce the compiler to do not check that is there any buffer overflow or Not. If here any Buffer overflow is occurred it will just truncate the value but not throw exception
    static void Main(string[] args)
            {
                int _value1 = 1500056564;
                             int _value2 = 1200046565;
                unchecked
                {
    // This Code will Not Give Exception
                 System.Console.WriteLine(_value1 * _value2);               
    System.Console.Read();
    }
    }

what are Nullable Types in C#


Nullable Types are the same generic data type.

If any object of the generic type has the '?' attached to the data type while declaring the value object. It denotes the object is a Nullable object of that data type
e.g. if I declare the RollNumber as
Public int ?RollNo=null;
The RollNumber declared above is Nullable roll number that means assigning the value null to the Rollnumber is valid and completely acceptable.
It has 2 properties
Hasvalue =A boolean property denoting validity of value containing in the object
value=A value contained in the object.

Inter Process communication

Inter-Process Communication,with the techniques and mechanisms that facilitate communication between processes

The IPC mechanisms can be classified into the following categories as given below:

1 .Pipes -> flow of communication between same pipe, data flows only in same direction
2 .FIFOi -:First IN First OUT
3. Shared memory ->different processes to communicate with each other as if these processes shared the virtual address space; hence, any process sharing the memory region can read or write to it.
4. Mapped memory
5. Message queues
6 Sockets