November 23, 2013

IIS interview Question

What is the Role of Windows Activation Process in IIS ?

WAP is the Controller of Worker process under a Application Pool. Windows Activation Process which is managed by the worker process by starting, stopping and recycling the application pool. When to start, stop and Recycle should be defined on Application Pool Settings. Activation Process is also responsible for Health Monitor of Application Pool during runtime.

FYI : Health monitoring setting can be easily found in Properties of Application Pool.

What are the different type of application pool available in IIS 7.0 ?


IIS 7.0 having two types of application pool.
1. DefaultAppPool (Integrated)
2. ClassicAppPool
What are the worker process for IIS 5.1 and IIS 6.0 ?

For IIS 5.1 > aspnet_wp.exe
For IIS 6.0 > w3wp.exe


What is Web Farm ?

This is one of the most question in IIS. And along with that interviewer can as what is the different between Web farm and Web Garden ?

When we hosted our web Application on multiple web server under a load balancer call the Web Farm. This is generally used for heavy load web application where there are many user request at a time. So When Web Application is hosted on Different IIS Server over a load balancer, Load balancer is responsible for distribute the load on different server.

What is the default Identity of an Application Pool ?

NetworkServices


How can we set the default page for any web application ?

We can set the default page for a web site from the Virtual Directory Setting.
How To :
IIS Manager > Virtual Directory > Right Click > Properties > GoTo Document Tab.


How we can set the Idle Time out of an worker process ?

We can set the Idle time out for an worker process from Application Pool Properties.
In Performance Tab of Application pool, we can set the Idle Time out of the worker process. This means worker process will shut down after that given time period if it stay idle. And will again wake up again if a new request comes.


Can we create one Application Pool From Another Application Pool ?

Yes. We can.

While creating Application Application Pool From IIS, there should have two option available first one is for Default Setting and Another is for Existing Setting as template.
We can select the second one and from the drop down listed below we can select any on the Application Pool as Template,.


What are the main components of SVCHost.exe ?


Main components for SVCHost.exe are WWW Publishing Service (W3SVC) and Windows Activation Porcess (WAP) .

W3SVC is the mediator of HTTP.SYS and Windows Activation Process. Windows Activation Process maintain the worker processes.

What are the different way that we can hosted site on IIS ?

We can hosted site on IIS either creating Virtual Directory through IIS manager or Using Folder Web Sharing .
Apart from that Visual studio provide some inbuilt features to host the site on IIS like using Publishing the web site , Using Copy web Tool or Creating Virtual directory during the creating the project by choosing Location as HTTP

How does IIS process an ASP.net request ?

When client request for an aspx pages, request comes to kernel level off IIS means to HTTP.SYS . HTTP.SYS receives the request and based on the application pool name [ Which is already registred with the HTTP.SYS ] it send the request to worker process. Windows Activation processworks as mediator of them. w3wp.exe loads "aspnet_isapi.dll" files to start the HTTPRuntime . HTTPRuntime creates HTTPApplication objects and all request are passed through HTTPModule and finally reached to HttpHandler . This is the request pipeline. After end of Request pipeline ASP.NET Page lifecycle starts.

From where we can set the Session Time Out in IIS ?

We can set the Session time out settings from the Virtual Directory for that site.
Right Click on Virtual Directory > Properties > Click on "Configuration" Button
Goto the "Option" Tab. There in Enable Session State Section you can configure the Session Timeout .


What are the different "Execution Permission" available for IIS for an virtual directory ?
There are three Execution Permission available.
1. None
2. Scripts Only
3. Scripts and Executable


From where you can change the ASP.NET Version in IIS ?
This can be change from Virtual Directory properties. First open Properties of Virtual Directory > GoTo ASP.NET Version Tab.
There we can have change the ASP.NET Version.


What is the default user name of an anonymous login in IIS?
In IIS, an anonymous user will be given with a user name of "IUSR_MachineName "


How can we take back-ups in IIS Server?

Step 1 : In the IIS (inetmgr), right click on the "Computer" icon under "Internet Information Services" . Click "All Tasks" and select "Backup/Restore Configuration".
Step 2 : Click on button "Create backup". Give Name for your backup file. If you want encryption enable encryption option and give UserName and Password and then click OK.


What is IIS metabase? And In which format IIS stors configurations?
IIS metabase is a special databse which is used to maintain the settings and configurations data for IIS. In simple term, it is a configuration base for IIS (Metabase.xml).
IIS 5.0 --> Metabse is in Binary.
IIS 6.0 & 7.5 --> Metabase is in XML.

List of Error & Status codes in IIS 6.0?

Status Code Type of Code
100 Series - Informational
200 Series - Success
300 Series - Redirection
400 Series - Client Error
500 Series - Server Error


How to recycle application pool from the command prompt in IIS7?


1. Use appcmd.exe to recycle the application pool from the command prompt.
2. appcmd.exe is the command line tool for IIS7, you will find this tool at following location :
%systemroot%\system32\inetsrv\appcmd
3. To recycle your application pool use the following command:
appcmd recycle apppool /apppool.name:<application pool name>


What are the Different steps to be followed to get SSL(Secure Sockets Layer) for our Web Application ?


. Intially we have to Generate a certificate request from our IIS
. Now we have to request a certificate from the certificate authority(CA)
. This CA is an entity which issues Digital Certificates.
. After receiving the certificate we have to install that particular certificate on our Web Server using IIS
. We have to use Secure Hyper Text Transfer Protocol(HTTPS) when accessing secure pages in our application.
By this way we could make our web page as SSL protected. !!!

Which DLL is used to translate XML to SQL in Internet Information Services (IIS) ?


What is the purpose of IIS application pools?
In simplest words we use applicaiton pools for ISOLATION purpose.


what is Windows Process Activation Service ?
The Windows Process Activation Service (WAS) provides process activation, resource management and health management services for message-activated applications. It manages application pool configuration and the creation and lifetime of worker processes for HTTP and other protocols (net.tcp,net.pipe,net.msmq)


How can we check whether IIS is being installed in my system or not?
To verify if IIS is installed or not we need to go to ’Add or Remove Programs’ utility in the Control panel and click on the ’Add/Remove Windows Components’ in the side menu.
There we must locate an item called "Internet Information Services (IIS)". If this is checked, IIS should be installed.
So that you can have your IIS installed in your system if it is not installed.
What are the permission settings are available for Virtual Directory ? 

Below are the list of permission that can be set during virtaul directory creation
1. Read
2. Run Scripts
3. Execute:
4. Write:
5. Browse


What is the use of Enable Pinging Properties for Application Pool ?

IIS should periodically monitor the health of a worker process

[ Idle or not , Time for recycle or not, All Worker process are running properly or not ] .
Pining means, Activation Process monitor Worker process performance, health, idle time etc.
By default it sets to 30s .


Does One Web Application can have multiple Application Pool ?


No. Every Web Application should have one Application Pool. Bydefault it is "DefaultAppPool ".


How we can save an Application Pool Settings?

Application Pool Settings can be save as "XML" Format.

Right Click on Application Pool > All Task > Save Configuration to a File .

This will save all the settings of Application Pool as an XML file.We can make It

password protected also.


Which Tool is used for Remote IIS Debugging ?


Tools is : msvsmon.exe

This is located at : Install path\Microsoft Visual Studio 8\Common7\IDE\Remote Debugger\x86


What are the different authentication mode available for IIS Remote Debugging ?


For IIS Remote Debugging msvsmon supported two authentication mode

1. Windows Authentication
2. No-Authentication
How can we get the list of worker process running in IIS along with the Application pool name ? 

By running iisapp.vbs script from command Prompt.

Below are the steps :
1. Start > Run > Cmd
2. Go To Windows > System32
3. Run cscript iisapp.vbs

If there are multiple worker process running on IIS, then how can you attach a particular worker

process for application ?


Well, If there are multiple worker process running in IIS, it means I have to know the name of
my application pool. Then I can run cscript iisapi.vbs script to find out the process ID and 
Application Pool name Based on the process Id for particular application.
I have to attache the process from Visual studio IDE.


Why do we need to IIS Remote Debugging ?

There are following reasons where we can use remote debugging
1. Your development server does not have IIS installed.
2. Development server and Build/Released/Hosting Server is different
3. Multiple user want to debug simultaneously.
Does IIS allows multiple user to Remote debug simultaneously ? 

Yes. This is one of the great features of msvsmon.exe . Each instance of the remote
debugger has a unique server name.we can give an instance of the remote debugger
any server name. Now multiple user can able to access the server instance.

What is the use of aspnet_regiis -i command ?

This is used automatically register the .NET Framework with your IIS.

Can we have multiple web sites on IIS ?

Yes. IIS Can have multiple web sites and Each and every web sites can have multiple virtual Directory.

Where is the default location for IIS Log files ?

Its
C:\WINDOWS\system32\LogFiles\W3SVC1
What are the major innovation in IIS 7.0 ? 

Below are the Major Innovation in IIS 7.0
Components are designed as module and there are major change in administration settings.
FYI : You can find out many of them, just go thorugh Microsoft IIS web site.


What is the Role of Windows Activation Process in IIS ?

WAP is the Controller of Worker process under a Application Pool. Windows Activation Process which is managed by the worker process by starting, stopping and recycling the application pool. When to start, stop and Recycle should be defined on Application Pool Settings. Activation Process is also responsible for Health Monitor of Application Pool during runtime.

FYI : Health monitoring setting can be easily found in Properties of Application Pool.
How can we set the default page for any web application ? 

We can set the default page for a web site from the Virtual Directory Setting.
How To :
IIS Manager > Virtual Directory > Right Click > Properties > GoTo Document Tab.
How we can set the Idle Time out of an worker process ? 

We can set the Idle time out for an worker process from Application Pool Properties.
In Performance Tab of Application pool, we can set the Idle Time out of the worker process.
This means worker process will shut down after that given time period if it stay idle.
And will again wake up again if a new request comes.
what is Windows Process Activation Service ? 

The Windows Process Activation Service (WAS) provides process activation, resource management and health management services for message-activated applications. It manages application pool configuration and the creation and lifetime of worker processes for HTTP and other protocols (net.tcp,net.pipe,net.msmq)


What are the Different Authentication Methods(Using Windows Authentication) which are provided by IIS ?

Generally IIS provides four different kinds of Authentication Methods they are :

Anonymous Method

If we select this authentication, IIS doesn't perform any authentication so that any one can access the application.

Basic Method

If we select this method, the user who access the application should provide windows username and password to access the application. Although this is sent through a network by transmitting direct text so it it very insecure.

Digest Method

This method is almost equal to Basic method but the difference is the password is hashed before it is transmitted through out a network.

Windows Integrated Method

In this the application uses the Kerberos protocol to validate(Authenticate) the user. This uses a Secret key cryptography which provides strign authentication for Client/Server applications.
How can we check whether IIS is being installed in my system or not? 

To verify if IIS is installed or not we need to go to ’Add or Remove Programs’ utility in the Control panel and click on the ’Add/Remove Windows Components’ in the side menu.

There we must locate an item called "Internet Information Services (IIS)". If this is checked, IIS should be installed.

So that you can have your IIS installed in your system if it is not installed


What is the Name of Default Application Pool in IIS ?

Though we can create new application pool IIS with different settings, but IIS having

its own default application pool named :DefaultAppPool
What are the different types of Identity available in IIS 6.0 ? 

IIS having three different Identity.
1. Local System
2. Local Services
3. NetworkServices
Name of default Identity of IIS6.0 

Default Identity of IIS 6.0 is NetworkServices .
Which is having very minimum rights on your system. The user can only have the read access of the site.
What is Recycling of Application Pool ? 

ecycling Application pool means recycle the Worker process (w3wp.exe ) and the

memory used for the web application.
There are two types of recycling related with Application pool

1. Recycling Worker Process - Predefined Settings
2. Recycling Worker Process - Based on Memory
What are the main layers of IIS Architecture ? 

IIS having mainly two layer Kernel Mode and User Mode

Below are the subsection of both of them.
1. Kernel Mode
o HTTP.SYS
2. User Mode
o Web Admin Service
o Virtual Directory
o Application Pool
What is the Role of Http.Sys in IIS ? 

HTTP.SYS is the kernel level components of IIS. All client request comes from client

hit the HTTP.Sys of Kernel level. HTTP.SYS then makes a queue for each and every request

for each and individual application pool based on the request.
Whenever we create any application pool IIS automatically registers the pool

with HTTP.SYS to identify the particular during request processing.
What are the different security settings available in IIS ? 

Below are the commonly used IIS Security settings
1 Anonymous
2 Integrated Windows Authentication
3. Basic Authentication
4. Digest Authentication
5. Passport Authentication

For Set security permission you need to

go to Virtul Directory > Right Click > Properties > Directory Security
Click on Edit Button .
What is the default authentication settings for IIS ? 

Anonymous authentication is the default authentication mode for any site that is hosted on IIS,
and it runs under the"IUSR_[ServerName]" account.


What is web garden ?

By default Each Application Pool runs with a Single Worker Process (W3Wp.exe).
We can assign multiple Worker Process With a Single Application Pool.
An Application Poll with multiple Worker process called Web Gardens. Each Worker Process
Should have there own Thread and Own Memory space. 
Generally its not recommended to use InProc Session mode while we are using Web Garden.
Where session data stores in case of "In-Proc" Session mode ? 

Session data store inside process memory of worker process [ w3wp.exe ] .

How we can create an web garden ?

For creating web graden we need to go to Application Pool,
then Right Click on Application Pool > Properties > Goto Performance Tab

In Web Garden Section, increase the number of worker process. By default it is 1.
How we can debug a web application which is hosted on IIS ? 

We can easily debug any web application that are hosted on IIS by using Attaching of Worker

Process.
From Visual Studio IDE > Tools > Attach To Process
Select the particular Process, then start debugging.




November 6, 2013

Converting VS 2010 Solution file to VS 2008 solution file

If you are working with VS 2010 (any edition) and cannot open your 2010 solution on VS 2008, then just follow these 3 steps:

For <PROJECT_NAME>.sln:

Open the solution file in your favorite text editor (example: Notepad).

Find the following:
Microsoft Visual Studio Solution File, Format Version 11.00.
# Visual Studio 2010.
Replace with:
Microsoft Visual Studio Solution File, Format Version 10.00. (I)
# Visual Studio 2008. (II, optional)


For <Project_name >.csproj/vbproj:

Open project file in your favorite text editor (example: Notepad++).

Find the following:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">.
Replace with:
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">. (III)


After these changes, you may receive the below error:
Unable to read the project file 'XXX.csproj'.

C:\Project path\XXX.csproj(89,11): The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

To fix this error, just change the VS version(v10.0 to v9.0) in your <Project_name >.csproj/vbproj file, that's all

Now you can open your solution file on VS2008.
Reverse of this will enable VS2008 solution to open in VS2010.

October 31, 2013

Design Patterns

Design patterns are recurring solutions to software design problems you find again and again in real-world application development. 

Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.

The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns.
They are categorized in three groups:
1) Creational
2) Structural
3) Behavioral.

Creational Patterns:
These patterns provide solutions that encapsulate the logic to instantiate or create objects.

Abstract Factory
Creates an instance of several families of classes
Builder
Separates object construction from its representation
Factory Method
Creates an instance of several derived classes
Prototype
A fully initialized instance to be copied or cloned
Singleton
A class of which only a single instance can exist

Structural Patterns:

These patterns describe how you can build increasingly complex and powerful classes and objects by combining classes or objects (respectively) together into larger entities.

Adapter
Match interfaces of different classes
Bridge
Separates an object’s interface from its implementation
Composite
A tree structure of simple and composite objects
Decorator
Add responsibilities to objects dynamically
Facade
A single class that represents an entire subsystem
Flyweight
A fine-grained instance used for efficient sharing
Proxy
An object representing another object


Behavioral Patterns:
Behavioral patterns provide solutions that control how an object or objects behave at run-time. These patterns focus on how objects communicate or interact, and how classes are assigned responsibilities.


Chain of Resp.
A way of passing a request between a chain of objects
Command
Encapsulate a command request as an object
Interpreter
A way to include language elements in a program
Iterator
Sequentially access the elements of a collection
Mediator
Defines simplified communication between classes
Memento
Capture and restore an object's internal state
Observer
A way of notifying change to a number of classes
State
Alter an object's behavior when its state changes
Strategy
Encapsulates an algorithm inside a class
Template Method
Defer the exact steps of an algorithm to a subclass
Visitor
Defines a new operation to a class without change

October 26, 2013

.NET Remoting

Introduction

.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.

Microsoft .NET Remoting provides a framework that allows objects to interact with each other across application domains. Remoting was designed in such a way that it hides the most difficult aspects like managing connections, marshaling data, and reading and writing XML and SOAP. The framework provides a number of services, including object activation and object lifetime support, as well as communication channels which are responsible for transporting messages to and from remote applications.

Remote Objects

What are remote objects? Any object outside the application domain of the caller application should be considered remote, where the object will be reconstructed. Local objects that cannot be serialized cannot be passed to a different application domain, and are therefore non remotable.
Any object can be changed into a remote object by deriving it from MarshalByRefObject, or by making it serializable either by adding the [Serializable] tag or by implementing the ISerializable interface. When a client activates a remote object, it receives a proxy to the remote object. All operations on this proxy are appropriately indirected to enable the Remoting infrastructure to intercept and forward the calls appropriately. In cases where the proxy and remote objects are in different application domains, all method call parameters on the stack are converted into messages and transported to the remote application domain, where the messages are turned back into a stack frame and the method call is invoked. The same procedure is used for returning results from the method call.

Types of .NET Remotable Objects

There are three types of objects that can be configured to serve as .NET remote objects. You can choose the type of object depending on the requirement of your application.

Single Call

Single Call objects service one and only one request coming in. Single Call objects are useful in scenarios where the objects are required to do a finite amount of work. Single Call objects are usually not required to store state information, and they cannot hold state information between method calls.

Singleton Objects

Singleton objects are those objects that service multiple clients, and hence share data by storing state information between client invocations. They are useful in cases in which data needs to be shared explicitly between clients, and also in which the overhead of creating and maintaining objects is substantial.

Client-Activated Objects (CAO)

Client-activated objects (CAO) are server-side objects that are activated upon request from the 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. A proxy is then created on the client side using the ObjRef. The client's method calls will be executed on the proxy. Client-activated objects can store state information between method calls for its specific client, and not across different client objects. Each invocation of "new" returns a proxy to an independent instance of the server type.

Domains

In .NET, when an application is loaded in memory, a process is created, and within this process, an application domain is created. The application is actually loaded in the application domain. If this application communicates with another application, it has to use Remoting because the other application will have its own domain, and across domains, object cannot communicate directly. Different application domains may exist in same process, or they may exist in different processes.

Contexts

The .NET runtime further divides the application domain into contexts. A context guarantees that a common set of constraints and usage semantics govern all access to the objects within it. All applications have a default context in which objects are constructed, unless otherwise instructed. A context, like an application domain, forms a .NET Remoting boundary. Access requests must be marshaled across contexts.

Proxies

When a call is made between objects in the same Application Domain, only a normal local call is required; however, a call across Application Domains requires a remote call. In order to facilitate a remote call, a proxy is introduced by the .NET framework at the client side. This proxy is an instance of the TransparentProxyclass, directly available to the client to communicate with the remote object. Generally, a proxy object is an object that acts in place of some other object. The proxy object ensures that all calls made on the proxy are forwarded to the correct remote object instance. In .NET Remoting, the proxy manages the marshaling process and the other tasks required to make cross-boundary calls. The .NET Remoting infrastructure automatically handles the creation and management of proxies.

RealProxy and TransparentProxy

The .NET Remoting Framework uses two proxy objects to accomplish its work of making a remote call from a client object to a remote server object: a RealProxy object and a TransparentProxy object. TheRealProxy object does the work of actually sending messages to the remote object and receiving response messages from the remote object. The TransparentProxy interacts with the client, and does the work of intercepting the remote method call made by the client.

Marshaling

Object Marshalling specifies how a remote object is exposed to the client application. It is the process of packaging an object access request in one application domain and passing that request to another domain. The .NET Remoting infrastructure manages the entire marshaling process. There are two methods by which a remote object can be made available to a local client object: Marshal by value, and Marshal by reference.

Marshalling objects by value

Marshaling by value is analogous to having a copy of the server object at the client. Objects that are marshaled by value are created on the remote server, serialized into a stream, and transmitted to the client where an exact copy is reconstructed. Once copied to the caller's application domain (by the marshaling process), all method calls and property accesses are executed entirely within that domain.
Marshall by value has several implications; first, the entire remote object is transmitted on the network. Second, some or the entire remote object may have no relevance outside of its local context. For example, the remote object may have a connection to a database, or a handle to a window, or a file handle etc. Third, parts of the remote object may not be serialiazable. In addition, when the client invokes a method on an MBV object, the local machine does the execution, which means that the compiled code (remote class) has to be available to the client. Because the object exists entirely in the caller's application domain, no state changes to the object are communicated to the originating application domain, or from the originator back to the caller.
MBV objects are, however, very efficient if they are small, and provide a repeated function that does not consume bandwidth. The entire object exists in the caller's domain, so there is no need to marshal accesses across domain boundaries. Using marshal-by-value objects can increase performance and reduce network traffic, when used for small objects or objects to which you will be making many accesses.
Marshal by value classes must either be marked with the [Serilaizable] attribute in order to use the default serialization, or must implement the ISerializable interface.

Marshalling objects by reference

Marshaling by reference is analogous to having a pointer to the object. Marshal by reference passes a reference to the remote object back to the client. This reference is an ObjRef class that contains all the information required to generate the proxy object that does the communication with the actual remote object. On the network, only parameters and return values are passed. A remote method invocation requires the remote object to call its method on the remote host (server).
Marshal by reference classes must inherit from System.MarshalByRefObject.

Marshalling done by the .NET Framework

Marshalling is done by the .NET Framework itself. We don’t have to write any code for it.

Channels

The .NET Remoting infrastructure provides a mechanism by which a stream of bytes is sent from one point to the other (client to server etc.). This is achieved via a channel. Strictly speaking, it is a class that implements the IChannel interface. There are two pre-defined .NET Remoting channels existing inSystem.Runtime.Remoting.Channels, the TcpChannel and the HttpChannel. To use theTcpChannel, the server must instantiate and register the TcpServerChannel class, and the client, theTcpClientChannel class.
Channel selection is subject to the following rules:
  • At least one channel must be registered with the remoting framework before a remote object can be called. Channels must be registered before objects are registered.
  • Channels are registered per application domain. There can be multiple application domains in a single process. When a process dies, all channels that it registers are automatically destroyed.
  • It is illegal to register the same channel that listens on the same port more than once. Even though channels are registered per application domain, different application domains on the same machine cannot register the same channel listening on the same port. You can register the same channel listening on two different ports for an application domain.
  • Clients can communicate with a remote object using any registered channel. The remoting framework ensures that the remote object is connected to the right channel when a client attempts to connect to it. The client is responsible for calling the RegisterChannel on the ChannelService class before attempting to communicate with a remote object.

Serialization Formatters

.NET Remoting uses serialization to copy marshal-by-value objects, and to send reference of objects which are marshal-by-reference, between application domains. The .NET Framework supports two kinds of serialization: binary serialization and XML serialization.
Formatters are used for encoding and decoding the messages before they are transported by the channel. There are two native formatters in the .NET runtime, namely binary (System.Runtime.Serialization.Formatters.Binary) and SOAP (System.Runtime.Serialization.Formatters.Soap). Applications can use binary encoding where performance is critical, or XML encoding where interoperability with other remoting frameworks is essential.
A key point to remember is that .NET Remoting always uses binary serialization, but you have your choice of output formats. The type of serialization (binary or XML) determines what object data is output. The formatter determines the format in which that data is stored.
The data in binary format has smaller size then in XML format. The smaller size makes binary formatting the obvious choice for intranet applications or whenever network performance is critical. However, not all firewalls look kindly on binary formatted data, so if you're distributing an application that needs to use Remoting over the Internet, you might find yourself forced to use SOAP formatting. Another benefit of SOAP formatting is that it's human readable, which gives you the opportunity to examine message traffic for debugging. Fortunately, Remoting works exactly the same regardless of which formatter you use, so you can use SOAP formatting during initial development and debugging, and then convert to binary format for production release.

.NET Remoting Metadata

.NET Remoting uses metadata to dynamically create proxy objects. The proxy objects that are created at the client side have the same members as the original class. But the implementation of the proxy object just forwards all requests through the .NET Remoting runtime to the original object. The serialization formatter uses metadata to convert method calls to payload stream and back.
The client can obtain the metadata information required to access the remote object in the following ways:
  • The .NET assembly of the server object.
  • The Remoting object can provide a WSDL (Web Services Description Language) file that describes the object and its methods.
  • .NET clients can use the SOAPSUDS utility to download the XML schema from the server (generated on the server) to generate source files or an assembly that contains only metadata, no code.

Object Activation

Object activation refers to the various ways in which a remote object can be instantiated. Marshal by value objects have a simple activation scheme, they are created when the client first requests them. Marshal by reference objects have two activation schemes:

Server Activated Objects

Server Activated Objects are created only when the client makes the first call to the remote method. In other words, when the client asks for the creation of the remote object, only the local proxy is created on the client, the actual remote object on the server is instantiated on the first method call. These proxies can be generated as:
...

// On the Server
RemotingConfiguration.RegisterWellKnownServiceType(
               typeof (RemoteServerObject), "Test",
               WellKnownObjectMode.SingleCall);
...

// On the Client
IRemoteCom obj = (IRemoteCom)Activator.GetObject(typeof(IRemoteCom), 
                  "tcp://localhost:1002/Test");

...
We have two registration types, Singleton and SingleCall, which were explained earlier. The code for both is:
RemotingConfiguration.RegisterWellKnownServiceType( typeof(RemoteServerObject), 
          "Test", WellKnownObjectMode.SingleCall);
...

RemotingConfiguration.RegisterWellKnownServiceType( typeof(RemoteServerObject), 
          "Test", WellKnownObjectMode.Singleton);
For Server Activated Objects, the proxy object (on the client) and the actual object (on the server) are created at different times.

Client Activated Objects

These are created on the server immediately upon the client's request. An instance of a Client Activated Object is created every time the client instantiates one, either by the use of new orActivator.CreateInstance(...).
...

// On the Server
RemotingConfiguration.ApplicationName = "TestCAO";
RemotingConfiguration.RegisterActivatedServiceType(typeof(RemoteObjectCAO));
...

// On the Client
RemotingConfiguration.RegisterActivatedClientType(typeof(RemoteObjectCAO),
                                          "tcp://localhost:1002/TestCAO");
obj = new RemoteObjectCAO();

...
In all these examples, "localhost" can be replaced with an actual remote machine's IP address.
When to use which kind of activation depends upon different scenarios. Some of them are:
  • Singleton object reference to the same server object, and any changes made to that object by one client are visible by all of the object's clients. Think of the Windows Desktop, where a change made by one program is visible by all client programs. Persistent data is held on the server, and is accessible by all clients.
  • Use the SingleCall model to provide a stateless programming model (the traditional Web services request/response model), or any time you have no need to maintain a persistent object state on the server.
  • Use client activation if the object needs to maintain persistent per-client state information.

Object Lifetime and Leases

A Server Activated Object that has been registered as SingelCall has a very simple and limited lifetime. It lives for a single call only. Thereafter, it is marked for removal by the Garbage Collector.
The other two types, Singleton and Client Activated Objects, have their lifetimes managed by a service introduced by the .NET Framework, the Leased Based Manager. On the server, the application domain's lease manager determines when a server-side object should be marked for deletion. Each application domain has its own lease manager object. For objects that have object references that are transported outside the application, a lease is created. The lease has a lease time; when the lease reaches zero, it expires, and the object is disconnected from the .NET Remoting Framework. Once all the references to the object within the AppDomain have been freed, the object will be collected when the next garbage collection occurs. A sponsor is another object that plays a role in the life time. When a lease has expired, one or more of the lease's sponsors are invoked by the Lease Manager, where they are given the opportunity to renew the lease. If none of the sponsors decides to renew the lease, the lease manager removes the lease and the object is garbage collected.
The default settings for the lease manager are five minutes for initial time-to-live, poll every ten seconds, and add an additional two minutes for every remote call by a client. All these values are configurable, we can overrideMarshalByRefObject InitializeLifetimeService() to define our own values in the remotable object.
The InitializeLifetimeService method can be overridden as follows:
public override object InitializeLifetimeService()
{
    ILease lease = (ILease)base.InitializeLifetimeService();

    if (lease.CurrentState == LeaseState.Initial)
    {

        // set times very short for demonstration purposes
        lease.InitialLeaseTime = TimeSpan.FromSeconds(25);
        lease.SponsorshipTimeout = TimeSpan.FromSeconds(25);
        lease.RenewOnCallTime = TimeSpan.FromSeconds(20);
    }
    return lease;
}
Under this scheme, the remote object's (server-side object) lifetime is independent of the client object's lifetime. The decoupling of the server and the client lifetimes is a very flexible design. Consider, a remote object that is costly to instantiate can be given a long lifetime lease, or can be kept alive by a sponsor. In addition, a remote object that holds scarce or important resources can be given short lifetimes, and be reclaimed by the Garbage Collector quickly. More importantly, this decoupling guarantees that the remote object will have a finite lifetime, regardless of network conditions or the existence of any clients.

Renewing Leases (Increasing Life Times)

While objects have a default lease period, there are many ways to extend the lease period to keep the object alive, in case the client wants to maintain state in the same server object.
  • The server object can set its lease time to infinity, which instructs Remoting not to collect it during garbage collection cycles.
  • The client can call the RemotingServices.GetLifetimeService method to get the lease of the server object from the lease manager of the AppDomain. From the Lease object, the client can then call the Lease.Renew method to extend the lease.
  • The client can register a sponsor for the specific lease with the lease manager of the AppDomain. When the remote object's lease expires, the lease manager calls back to the sponsor to request a lease renewal.
  • If the ILease::RenewOnCallTime property is set, then each call to the remote object renews the lease by the amount of time specified by the RenewOnCallTime property.
Once the object has been marshaled, the lease goes from the initial to the active state, and any attempt to initialize the lease properties will be ignored (an exception is thrown). InitializeLifetimeService is called when the remote object is activated. After activation, the lease can only be renewed.
The .NET Remoting implementation gives you two ways to renew the lease on a remote object. The client can call ILease.Renew directly, or the client can contact a sponsor and ask the sponsor to renew the lease.
The sponsor object listens for queries from the host system's lease manager, and responds accordingly. Sponsors register with the lease manager by obtaining a reference to the lease and calling ILease.Register. The lease manager will periodically query the sponsor to see if it wants to renew the lease. If the sponsor wants to renew the lease, it returns a renewal TimeSpan value in response to the server's query.
Sponsors are preferable to client ILease.Renew loops, for several reasons. First, the server's lease manager keeps track of when the object's lease is about to expire, and will query the sponsor as needed in order to ensure the opportunity to extend the lease before time runs out. It also is more efficient, both in bandwidth and processing time. In general, where there are multiple clients for a single remote object (as in the case of a singleton), it is more efficient for the lease manager to query a single object for renewal rather than to have each client continually pinging the server. This is also true in the case of client activated objects because it reduces the network traffic to just the minimum required to keep the object alive.
You can have the client itself implement the Isponsor interface, or you can create a sponsor object that maintains leases for one or more remote objects and clients. Here, I am specifying the code to implementIsponsor by the client itself.
Implement the Isponsor in the remotable client class:
class RemoteClient: System.MarshalByRefObject, Isponsor
The Isponsor interface requires that you implement a single method, Renewal. The return value is the amount of time, expressed as a TimeSpan, to extend the lease. Our Renewal method will extend the lease for an additional 30 seconds. Here's the code, which you should add to the remote client:
public TimeSpan Renewal (ILease lease)
{
    return TimeSpan.FromSeconds(30);
}
Finally, register the RemoteClient object with the server's lease manager. Insert the code below where the server's object instance is being created:
// Instantiate the object
RemoteableClass MyRemotObject = new RemoteableClass();
// register as a sponsor
ILease lease = (ILease)RemotingServices.GetLifetimeService(MyRemotObject);
lease.Register(this);

How to Implement Remoting

In the following discussion, we will discuss how we can implement remoting in our applications. The code given in the discussion is just to show what is necessary in the code. You can get the whole code from the source code files.

Create a Remotable Object

A remotable object is an object that inherits from MarshalByRefObject.
Create a new C# class library project. Add a class called MyRemotableObject, and put in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. Compile the class to make sure you have everything correct.
MyRemotableObject.cs
namespace RemotableObjects
{
    public class MyRemotableObject : MarshalByRefObject
    { 
        public MyRemotableObject()
        { 
        }
        public void SetMessage(string message)
        {
            Cache.GetInstance().MessageString = message;
        } 
    }
}

Create a Server to Expose the Remotable Object

We need to create a server object that will act as a listener to accept remote object requests. For this example, we will use the TCP/IP channel. We first create an instance of the channel, and then register it for use by clients at a specific port. The service can be registered as WellKnownObjectMode.SingleCall, which results in a new instance of the object for each client, or as WellKnownObjectMode.Singleton, which results in one instance of the object used for all clients.
It is not necessary to create the server listener if you are planning to use IIS. For obvious reasons, IIS only supports the use of the HttpChannel. Create a virtual directory for your application, and then put code to register your service in the Application_Start event.
For our example, we'll go ahead and create a server listener, in case you don't have IIS. Since the service needs to be bound to an available port, for our example, I chose 8080, which is a port that I know to be unused on my computer. You may need to choose a different port depending upon what ports you have available. To see a list of the used ports on your computer, open a command prompt, and issue the command "netstat --a". It may produce a long listing, so make sure the command prompt buffer sizes are set to allow scrolling. Compile the class to make sure you have everything correct.
Create a new C# Windows Application project. Add a Windows Form called frmRServer, and paste in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannelwill not be found. In addition, add a reference to the project containing the MyRemotableObject, otherwise the code will not compile because it won't know how to find a reference to MyRemotableObject. Here, I am giving a part of the code. You can get the complete code from the source code files.
namespace RemotableObjects
{
    public class frmRServer : System.Windows.Forms.Form, IObserver
    {
        private System.Windows.Forms.TextBox textBox1;
        private MyRemotableObject remotableObject;
        private System.ComponentModel.Container components = null;
        public frmRServer()
        {
            InitializeComponent();
            remotableObject = new MyRemotableObject();
            // using TCP protocol
            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(
               typeof(MyRemotableObject),"HelloWorld",
               WellKnownObjectMode.Singleton);
            RemotableObjects.Cache.Attach(this);
        }
    }
}

Create a Client to Use the Remotable Object

The client will be very simple. It will connect to the server, create an instance of the object using the server, and then execute the SetString method.
Create a new C# Windows Application project. Add a Windows Form called frmRCleint, and paste in the following code. Add a reference to the project containing the SampleObject, otherwise the code will not compile because it won't know how to find a reference to SampleObject. Compile the class to make sure you have everything correct. Here, only part of code is given just to show the concept. You can get whole code from the source given:
namespace RemotableObjects
{
    public class frmRCleint : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1; 
        MyRemotableObject remoteObject;
        private System.ComponentModel.Container components = null;
        public frmRCleint()
        {
            InitializeComponent();
            // using TCP protocol 
            // running both client and server on same machines
            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan);
            // Create an instance of the remote object
            remoteObject = (MyRemotableObject) Activator.GetObject(
              typeof(MyRemotableObject),"tcp://localhost:8080/HelloWorld");
        }
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            remoteObject.SetMessage(textBox1.Text);
        }
    }
}

Running the Application

To run the application, compile the Remoteable Object Project object. Compile server application and run it. Compile the client application and run it. Two dialogue boxes will appear: one for the Remoting server, and one for the Remoting client. The Remoting server will listen for the requests. And the Remoting client is ready to pass the message to the server. Now, type any string in the client dialogue box, and the client will send it to the server through Remoting, and the server will display the string in the dialog box of the server application. This is a simple demonstration of easy and understandable Remoting.

Object Configurations

There are two ways to configure remote objects. One is by code, and the other is by using a configuration file, both on the server and the client. The advantage of using a configuration file is we don’t have to compile the code every time we change the configuration, so we can change the configuration of the application at any time without disturbing the code or executables. By convention, the configuration file name has the formatexecutablename.config.

Configuration files

A typical configuration file includes the following, among other, information:
  • Host application information
  • Name of the objects
  • URI of the objects
  • Channels being registered (multiple channels can be registered at the same time)
  • Lease time information for the server objects
Here is an example the configuration filename:

Server remoting configuration (server.exe.config):

<configuration>
 <system.runtime.remoting>
  <application name="server">
   <service>
    <activated type="remote.ServiceClass, remote"/>
   </service>
   <channels>
    <channel ref="http" port="8080">
     <serverProviders>
      <formatter ref="binary" typeFilterLevel="Full"/>
     </serverProviders>
     <clientProviders>
      <formatter ref="binary"/>
     </clientProviders>
    </channel>
   </channels>
  </application>
 </system.runtime.remoting>
</configuration>

Client remoting configuration (client.exe.config)

<configuration>
 <system.runtime.remoting>
  <application name="client">
   <client url = "http://localhost:8080">
    <activated type="remote.ServiceClass, remote"/>
   </client>
   <channels>
    <channel ref="http" port="0">
     <serverProviders>
      <formatter ref="binary" typeFilterLevel="Full"/>
     </serverProviders>
     <clientProviders>
      <formatter ref="binary"/>
     </clientProviders>
    </channel>
   </channels>
  </application>
 </system.runtime.remoting>
</configuration>
To use these configuration files in our Remoting test application, place those files (server.config andclient.config) in the directory that contains the server.exe and client.exe programs.
Using application configuration files greatly simplifies the code required to set up a remoting application. For example, the single line shown here takes the place of all the configuration code in the server.cs program'sMain method:
RemotingConfiguration.Configure("server.config");
The configuration files shown set the client activation--each client gets its own object. If you want to configure for Singleton mode, change the <service> section of the server's configuration file so that it reads:
<service>
   <wellknown
   type="remote.ServiceClass, remote"
   objectUri="ServiceURI"
   mode="Singleton"
/>
</service>
And change the <client> section of the client's configuration file so that it reads:
<client url = "http://localhost:8080">
    <wellknown
    type="remote.ServiceClass, remote"
    url="http://localhost:8080/ServiceURI"
/>
</client>

Configuration through code

Server
TcpServerChannel channel = new TcpServerChannel(rs.mPort);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
       typeof(RemoteServerObject), "Test",
       WellKnownObjectMode.SingleCall);
Client
TcpClientChannel chan = new TcpClientChannel();
ChannelServices.RegisterChannel(chan);
IRemoteCom obj = (IRemoteCom)Activator.GetObject(typeof(IRemoteCom), 
                 "tcp://localhost:1002/Test");

Hosting .NET Remoting Objects

.NET Remoting objects can be hosted in:
  • Managed executablesNET Remoting objects can be hosted in any regular .NET EXE or a managed service.
  • IISRemoting objects can be hosted in the Internet Information Server (IIS). By default, Remoting objects hosted in IIS receive messages through the HTTP channel. For hosting Remoting objects under the IIS, a virtual root has to be created, and a "remoting.config" file has to be copied to it. The executable or DLL containing the remote object should be placed in the bin directory under the directory the IIS root points to. It is important to note that the IIS root name should be the same as the application name specified in the configuration files. The Remoting configuration file is automatically loaded when the first message arrives in the application.
  • .NET Component ServicesNET Remoting objects can be hosted in the .NET component services infrastructure to take advantage of the various COM+ services like Transactions, JIT, and Object pooling.
MyRemotableObject remoteObject;
private System.ComponentModel.Container components = null;
public frmRCleint()
{
    InitializeComponent();
    // using TCP protocol 
    // running both client and server on same machines
    TcpChannel chan = new TcpChannel();
    ChannelServices.RegisterChannel(chan);
    // Create an instance of the remote object
    remoteObject = (MyRemotableObject) Activator.GetObject(
                    typeof(MyRemotableObject),"tcp://localhost:8080/HelloWorld");
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    remoteObject.SetMessage(textBox1.Text);
}