Hosting .NET Remote Object in ASP.NET (IIS)
Intended Readers: Persons having intermediate
knowledge of ASP.NET, IIS and .NET Remoting.
Platform: .NET 1.1,
2.0, IIS
Level: Expert
Introduction
.NET Remoting allows us to call the object methods remotely, the application
which serves the object is called server application and application which
calls the remote method is called client application. Before client application
calls method, server application needs to be running as well, there are various
ways in which server application can host remote object, in this article I will
discuss about the hosting remote objects in IIS and ASP.NET.
.NET Remoting object can be host in following ways
- Windows Application: This can be either Console
or Windows Application, but the problem here is that application needs to
start manually.
- Windows Service: You don’t need to start the
application manually, since it can be automatically restarted on the
system startup.
- COM+ Application: Fully utilizing the
features of COM+ such as Object Pooling, Transactions.
- ASP.NET and IIS: ASP.NET can be configured
as the hosting environment for the .NET Remoting objects by using the
services of IIS such as Session, Cache etc.
The major
benefit of hosting Remoting application in IIS is that we can utilize the
services of IIS such as Application and Session management for maintaining
states and more importantly we can expose our remote object as a web service
(we will see that in a moment). Before this let’s see how we can host remote
objects in IIS.
For hosting remote objects in IIS we need to derive our class from the System.Runtime.Remoting.Services.RemotingService,
this class provides the features of ASP.NET such as Application, Session
management for the remote objects.
1. Object Definition
lets look at the definition of the sample remote object
using System.Runtime.Remoting.Services;
public
class MyRemoteType
: RemotingService
{
public
MyRemoteType()
{
}
public
string
GetMessage()
{
int count =
1;
if
(Application["Count"] !=
null)
count =
(int)Application["Count"];
string str =
string.Format("Hello World,
Usage count = {0}",
count);
count++;
Application["Count"] =
count;
return
str;
}
public
string
Hello()
{
if
(User.Identity == null)
return
"No
Identity";
else
return
"Hello
" +
User.Identity.Name;
}
}
The above class is self explanatory it contains two methods one returns the
message along with usage count and other is returning the hello message for the
active user. Note the use of Application property here, like the ASP.NET
Remote Services class exposes important properties such as
- Application: returns reference of
HttpApplicationState object for Application management.
- Session: returns reference of
HttpSessionState object for Session Management.
- Context: returns reference of
HttpContext object for accessing HTTP specific features such as Headers
etc.
- Server: returns reference of
HttpServerUtility object for accessing server side features such server
variables etc.
- User: returns IPrincipal
reference which represents the current user which is calling the
application.
2.
Remote Object Hosting in IIS
The process of hosting remote object in IIS is quite simple i.e.
- Create a virtual directory
- Copy the remote object DLL in the bin directory.
- Configure the Web.Config.
Since life time of
ASP.NET application manages by the IIS it self we cannot implicitly register
the remote object in our application, we need to configure it in Web.Config
i.e.
<system.runtime.remoting>
<application>
<service>
<wellknown mode="SingleCall"
type="MyNamespace.MyRemoteType, MyAssembly"
objectUri="RemObject.soap" />
</service>
</application>
</system.runtime.remoting>
The above section will configure the
remote object in ASP.NET without any extra effort, now look at how client
application will calls this object
MyRemoteType remObj = (MyRemoteType)
Activator.GetObject(typeof(MyRemoteType),
"http://server/virtualDir/RemObject.soap");
Console.WriteLine(remObj.GetMessage());
Console.WriteLine(remObj.Hello());
3. IIS and Web services
The above
Remoting object can also be called as a web service by using the automatic WSDL
generation feature of IIS, i.e. the following URL will display the WSDL
information about the Remoting object
http://Server/VirtualDir/RemObject.soap?wsdl
Once the WSDL information is generated, utilities such as disco or wsdl (comes
with .NET framework) can use it in order to build the web service proxies and
the remote object can be called as a web service.
Benefits
of IIS hosting
Hosting .NET
Remoting objects in IIS results the following benefits
- use of ASP.NET specific services such as Application,
Session etc and HTTP specific information such as HTTP headers.
- Ability to expose the Remote object as a web
service by using the WSDL specific information generated by the IIS.
- Utilization of IIS integrated security for securing
the applications.
- Automatic Startup of remote application
Limitations
of IIS hosting
How ever this
approach has some limitations as well such as
- Only well known objects are supported, means state
can only be maintain through ASP.NET services.
- Only HTTP channel is supported with the SOAP
formatter
Reference: C# Web Services, Wrox Press, ISBN 1-861004-39-7
Your Comments and
feedbacks are always welcome