Working with .NET Remoting Under IIS Server and Using ASP.NET as Remote Client
 
Published: 23 Jan 2008
Abstract
In this article, Abhishek describes the step-by-step procedures to implement remote server using VB 2005 remoting and host it on Internet Information Services. The article also shows how to invoke remote methods through ASP.NET web applications.
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 44811/ 76

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



User Comments

Title: Another cause of 404   
Name: Tangent
Date: 2011-03-31 9:47:15 PM
Comment:
Occasionally 404 can be caused by too much data has been sent to remoting server. Because there is a maxAllowedContentLength constraint which is by default set at 4MB, unless this setting is overridden, calling Remoting with serialized data potentially larger than 4MB would result a 404 error (non-sense but true). Check http://tangentlin.wordpress.com/2011/03/31/404-error-in-net-remoting/ for more details and solution.
Title: Nice   
Name: amir
Date: 2010-03-05 5:02:08 AM
Comment:
i faced with this question.
Is it possible to write webchat application using asp.net and remoting?

if we can how we can mange chate users and messages?
Title: Good Work..!!   
Name: J.J
Date: 2009-11-26 8:26:28 AM
Comment:
Good work for any .NET remoting beginner like me..!!
I have a problem when I have the IIS installed on a remote server. The server/ip instead of localhost (http://localhost/server/server.soap?wsdl) doesnot seems to work. Please suggest a possible solution.

Thanks In Advance
J.J
Title: Thanks a lot   
Name: Echo
Date: 2009-11-16 4:56:28 AM
Comment:
Ok, It work well base on your description, But there are a mistake need to point out which in Web.config
"type="Server.clsServer,Server"

Not "clsServer", First I don't change it,it should be a Class "MyServer" in here, or else throw a exception:"Cannot load type Server.clsServer, Server Pin", Hope can help others.
Anyway, Good work with your clear explanations.

Echo
Title: I just want to how to deploy Server in IIS   
Name: echo
Date: 2009-11-15 10:53:05 PM
Comment:
hope can help me
Title: Appreciates goood work   
Name: Pasan Indeewara
Date: 2009-08-05 11:52:23 PM
Comment:
.NET remoting is actually great for free services we're hoping to provide like online radio stations where we're not considering security much. Still if we need to include sort of security it's better deal with IIS than .NET remoting.
I Appreciate good work with your clear explanations.
Title: Article   
Name: Saravanan M
Date: 2009-07-20 5:58:48 AM
Comment:
Good Article... :) Great work ...
Title: (404) Not Found   
Name: Richard Fremmerlid
Date: 2009-03-06 6:25:30 PM
Comment:
If you are getting a 404 error it means probably a mapping for the .REM extension doesn't exist or isn't mapped to the aspnet_isapi.dll I'm guessing you installed .NET prior to loading IIS, because it is done with the installation.

From the .NET command prompt run the following
aspnet_regiis -i
Title: client to server in C#.net   
Name: vasantha
Date: 2008-11-13 1:10:02 AM
Comment:
hi,
we r doing chat aplication from the client side to server.this is a desktop application using C#.net. So Can u just send the code in C# or any converter available for free download.
Thanks.
Title: Working with .NET Remoting Under IIS Server .........   
Name: Srinandan
Date: 2008-07-31 5:01:26 AM
Comment:
Thanks Abhishek.
I did it but used the console application which i have to execute everytime.

using class library with web.config using IIS made it easy to deploy..

Really i appreciate your job.
Title: finally a step by step guide   
Name: Fabrizio
Date: 2008-06-28 9:21:32 AM
Comment:
Thanks a lot for your article, some days wandering and i found only generic help. This article step by step guided me to a working solutions. Just two tips:
- explain better what you write in the web.config
- explain better the ways in which an ASP.NET page can call you (through global.asax activation and so on)

Thanks again,
Fabrizio
Title: article   
Name: sam
Date: 2008-05-09 3:33:12 AM
Comment:
good
Title: this article   
Name: abhishek singh
Date: 2008-05-08 3:22:04 AM
Comment:
this is actually for advance programmers only. In normal situation we don't require to host remoting under IIS. It's for hight level busineess processing.
Title: Working with .NET Remoting Under IIS Server and Using ASP.NET as Remote Client   
Name: Santy
Date: 2008-05-08 2:55:39 AM
Comment:
Its pretty gud but I dont think the beginners can understand the terminology much... so be as simple as you can... its just a suggestion rest is left up to you, but the article is really gud... I appreciate your effort.
Title: Very easy to read!   
Name: henkgijsbert
Date: 2008-03-14 5:07:15 PM
Comment:
Well done. Easy to read with lots of screen shots. No need for me to do it myself in order to get good understanding. Saves time.
Title: for C# version   
Name: Abhishek Singh (Author)
Date: 2008-02-28 7:11:37 AM
Comment:
you can easily get C# version of code. Just try to use some convertor available free on internet. You need to put part of vb code and then you will get C# version of that code..
then use that code in your C# project.
Title: mr   
Name: reddy
Date: 2008-02-28 5:25:06 AM
Comment:
could u pls write it in c-sharp?
Title: 404 error   
Name: Abhishek Singh
Date: 2008-02-01 10:51:05 AM
Comment:
Please check if you have set virtual directory to use proper .net framework.
Title: malinimarudhu@gmail.com   
Name: Malini
Date: 2008-01-31 7:25:27 AM
Comment:
Hi..

Its a good Stuff!!!
but i get "The remote server returned an error: (404) Not Found. "
Cause?
Title: Working with .NET Remoting Under IIS Server and Using ASP.NET as Remote Client   
Name: Mena
Date: 2008-01-31 7:23:08 AM
Comment:
Hi..
when i try this, i get the error
"The remote server returned an error: (404) Not Found. "
why?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


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