AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1549&pId=-1
Working with .NET Remoting Under IIS Server and Using ASP.NET as Remote Client
page
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 44715/ 55

Introduction

Remoting is a technology based on communication between one or more different application domains in a distributed environment. It means this enables objects residing on the same domain or computer, else spread among different domains or computers worldwide, to be accessible by each other and they can communicate and transfer data among each other. Normally, this follows the client server architecture of a centralized server application and its multiple client applications.

.NET Remoting without using IIS Server

As you know, we can implement remote server and client objects in different type of applications. For example, Console application, Windows Form based application, and Windows Services. In this case, we need to implement security options, authentication and authorization in the application.

.NET Remoting using IIS Server

To implement the remote client object in ASP.NET web application we have to setup the remote server object in the IIS Server. In this case, we do not need to implement security options, authentication and authorization, since the application would be able to use security features provided by IIS as these would be hosted in the IIS.

Which one to use, when and why?

We should go for IIS hosted remoting if we require one or more points among the following:

·         We want to use the remote server object to be accessible by web client applications (ASP.NET clients).

·         If webservice has slow performance on processing large or complex data.

·         We do not want to bother about implementing security, authentication or authorization in the code.

·         IIS setting at server are more scalable than stand alone application and have proxy backup servers.

Building a remote server application using VB 2005

Create a library project to build the server interface.

Open Visual Studio 2005 IDE. Create a class project as: File >> New >> Projects.

Choose Visual Basic as in Project Types and "Class Library" in project templates.

Give the project name as IServer and click OK.

It will create the Iserver project containing a default class Class1.vb. Rename this class to IServer.vb.

Now implement the following code in Iserver.vb file.

Listing 1: Code for Iserver.vb

Public Interface IServer
  Function GetTimeFromServer() As DateTime
End Interface

Build the IServer project.

Create (Add) a library project to build the server object

In order to have remoting using IIS Server, we will build remote server object as an assembly. Then we will go for its setup into the IIS Server.

To add library project for remote server object: In IServer solution- File >> Add >> New Project.

Choose Visual Basic in the Project Types and "Class Library" in project templates. Give the project name as Server and click OK.

It will create the Iserver project containing a default class Class1.vb. Rename this class to MyServer.vb.

In this we need to import System.EnterpriseServices namespace and before doing it add a reference of System.EnterpriseServices into the project.

Project >> Add Reference >> Browse >> Locate the dll file \IServer\IServer\bin\Debug\IServer.dll (if build in debug mode) or \IServer\IServer\bin\Release\IServer.dll (if build in release mode) >> OK

For this example I assume the project is compiled in Debug mode.

We will implement the functions declared in IServer.dll by implementing the IServer interface. Change the class name in the code to clsServer. Add the following code in MyServer.vb for class clsServer to achieve this.

Listing 2: Code to implement functions of IServer interface

Public Class clsServer
    Inherits MarshalByRefObject
    Implements IServer.IServer
 
    'Implement server remote method
    Public Overridable Function GetTimeFromServer() As DateTime _
      Implements IServer.IServer.GetTimeFromServer
        Return Now.ToString()
    End Function
End Class

Build the Server project. If the build is successful then we are ready with the server object and to get it to setup in IIS Server.

Configure and Setup remote server application and web.config in IIS Server

We need setup Server project assembly in IIS.

Step 1: Go to the primary web root directory C:\Inetpub\wwwroot and create a new folder named Server.

Step 2: Inside folder Server, create a new folder named bin. Now, move to your project Debug directory (IServer\Server\bin\Debug). Copy all files from here and place them into \wwwroot\Server\bin.

At this point the wwwroot\Server\bin directory may have the following files:

Figure 1: File structure in \server\bin folder in web root directory

Step 3: We need a web config to enable the assembly to work as remote objects. Create a file web.config in the \Server folder. Open web.config in notepad and add the following content in it.

Listing 3: Content of web.config for server in IIS

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.runtime.remoting>
    <application>
 
        <service>
            <wellknown mode="Singleton" type="Server.clsServer,Server"
                               objectUri="server.soap" />
        </service>
        <channels>
            <channel ref="http" />
            <serverProviders>
                <formatter href="binary" />
            </serverProviders>
        </channels>
    </application>
</system.runtime.remoting>
</configuration>

Save web.config file. At this point the wwwroot\Server\ directory may look like:

Figure 2: File structure in \server folder in web root directory

Step 4: We need setup Server folder as a virtual directory in IIS. Open the IIS window as: Control Panel >> Administrative Tools >> Internet Information Services.

Expand the node to reach to the folder Server: Local computer >> Web Sites >> Default Web Site >> Server.

Right click on COMAppASPClient and open the properties window.

Figure 3: Choosing properties option for the directory of ASP page

Click "Create" button in the application name tag. This makes the directory a virtual directory.

Figure 4: Creating the server folder to be a virtual directory

Click OK to finish with the IIS Server properties window. Here the remote object is installed. Let us test whether it is successfully installed. Open an internet explorer window and write the following in URL (use server name/ip instead of localhost if remote server is installed on other machine in LAN/WAN).

http://localhost/server/server.soap?wsdl

Opening this link would show information in XML format about implemented remote methods on the page as given below.

Figure 5: Get the description of remote object services in XML format

If you get the XML output on the page about the remote service then you could be sure that remote server object is installed in IIS correctly. Now we can proceed to create a web client which consumes these services.

Building a remote client web application using ASP.NET (VB)

At this point our remote server is hosted in IIS. It is time to create a client to test it. We will create a web application in ASP.NET which will consume or call the functions of remote server.

Create a new ASP.NET web application

Step 1: Open Visual Studio 2005 IDE. Create a class project as: File >> New >> Web Site.

Choose ASP.NET Web Site in the template. Give the project name as Client as shown below.

Figure 6: Creating the ASP.NET client application (remote client)

It will create the Client folder in www.root folder with default files and folder. You will see in the Client project VS automatically added two files Defaut.aspx and web.config. Rename file Default.aspx to Client.aspx.

Step 2: Add reference to the web server interface assembly (IServer.dll in this example).

In the project menu choose- Website >> Add Reference >> Browse >> \IServer\IServer\bin\Debug\IServer.dll >> OK.

Step 3: Now implement the Page_Load of the Client.aspx as given below.

Listing 4: Code for Page_Load in Client page

Protected Sub Page_Load(ByVal sender As ObjectByValAs System.EventArgs)_
 Handles Me.Load
  Dim objRemote As IServer.IServer
  'create transperent proxy object
  objRemote = CType(Activator.GetObject(GetType(IServer.IServer), _
    "http://localhost/server/server.soap"), IServer.IServer)
  'Executing remote object function
  Response.Write("Time received from Server: " & objRemote.GetTimeFromServer())
End Sub

Build the Client website.

Test remote server hosted in IIS using ASP.NET remote client

Run the client website to execute Client.aspx page. You should get following result on the page.

Figure 7: Result at client web page which calls the remote method of server

Note that the date time 1/4/2008 12:53:49 PM has been sent by the server and server is hosted in IIS. So here we are done.

As per your requirements you can add more functions in server application and setup build output in IIS following the procedures given above.

Downloads
Conclusion

I have described the procedures to implement .net remoting in IIS. By hosting the server and client in IIS we can achieve the following benefits based on project implementation.

·         Client web site does not need to bother about remote settings in config or code at minimum. It just requires adding reference to the proxy interface. However, if you want, you can use Client.exe.config for more setting.

·         Client needs to be host once which can be accessed by as many users. It saves client application deployment time for each user/machine.

·         Server and client automatically use the IIS security features, etc.

We can implement different features of remoting, such as SingleCall/Singleton, CAO, and SAO, etc. In my view, SingleCall SAO with Http channel and binary formatter suits best when hosting server side component in IIS.

I hope this article will be helpful for .NET programmers. Comments and suggestions are appreciated.

Happy Programming!!!

Abhishek Kumar Singh


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-08 11:23:29 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search