5,442,164 members and growing! (18,584 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate

.NET Remoting under IIS - ASP.NET Application as Client.

By Raj Settipalli

Simplest method to host remote component under IIS with ASP.NET application as client.
VB.NET 1.1, WinXP, Win2003, Windows, .NET, ASP.NET, IIS, Visual Studio, IIS 6, VS.NET2003, Dev

Posted: 3 Mar 2005
Updated: 3 Mar 2005
Views: 68,864
Bookmarked: 31 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 4.19 Rating: 3.48 out of 5
4 votes, 25.0%
1
1 vote, 6.3%
2
2 votes, 12.5%
3
4 votes, 25.0%
4
5 votes, 31.3%
5

Introduction

I have come across couple of good articles that tell us about creating .NET remote application, but none of these use ASP.NET application as client. Even the MSDN example, Hosting in Internet Information Services (IIS), uses a console application as client.

Background

If the remote object is to be accessed through internet, one should implement their own security to authenticate user as .NET remote applications require FullTrust permissions to execute. By hosting remote application under IIS, implementing security becomes much easier especially when the intranet is protected from outside world by a firewall and allowed communication channel is HTTP.

Using the code

Here are the steps to create a server application and client application.

Server Object

  1. Create a new VB.NET or C# class library IServer and replace code in Class1.vb with the following. Compile the project to IServer.dll.
    Public Interface IServer
    
        Function getServerTime() As DateTime
    
    End Interface
  2. Create new VB.NET or C# Class Library project with clsServer class which will implement getServerTime function of interface IServer.
    Public Class clsServer
        Inherits MarshalByRefObject
        Implements IServer.IServer
    
        Public Overridable Function getServerTime() _
                 As DateTime Implements IServer.IServer.getServerTime
            Return Now.ToString()
        End Function
    End Class
  3. I have also included one more function in the demo project to create a file in the server. This will convince the user beyond any doubt that remote object did get activated through transparent proxy. Please make sure that the folder where you want to create the file has write permissions.
  4. Compile this project into "Server.dll" or any other name that is appropriate.
  5. Create a new virtual directory, Server under IIS - either by using "Virtual Directory Creation Wizard" or
    • Create new folder under c:\Inetpub\wwwroot and rename it to Server.
    • Open IIS management console, view properties of the Server folder.
    • Click on the Create button against the "Application name", by default new application name will be the name of the folder. Click on OK button to close the properties window.
  6. Navigate to Server folder in Windows Explorer and create a sub folder "bin" under Server folder.
  7. Copy "Server.dll" into the bin folder.
  8. Create new text file, type the following XML code and save it as "web.config" and not as web.config.txt (by selecting "All files" in the Save As dialog box or by enclosing the file name in double quotes in the save dialog box). This XML code declares the service and channel. MSDN has lot of information about each of these XML attributes that we are coding here.
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <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>
    
  9. Most important attribute that we need to understand is "type". In the above example its value is "Server.clsServer, Server" which is nothing but "RootNamespace.ClassName, Assembly name". To find out what are these value for an existing project, just right click on the project name in solution explorer and choose properties. ObjectUri is something that uniquely identifies this object which is required to invoke this object from client. We can use other format "Server.rem" .
  10. The channel that we are using is HTTP and formatter is binary. Please refer to MSDN for alternative channels and formatters. We need to use HTTP here as we are hosting our server object under IIS and binary formatter is more efficient over SOAP formatter.
  11. At stage, if you type the URL http://localhost/Server/Server.soap?wsdl in the explorer address bar (assuming that you are hosting the server object in your local system, else replace "localhost" with valid webserver name), you should see the XML out put. This XML is what describes the service that we are hosting under IIS.
  12. It's about time to talk about client.

ASP.NET Client Application

  1. Create new ASP.NET web application.
  2. We need a reference to the server object before coding any of the function calls. Now we have several options at this stage, you could actually add the server.dll to the references (even though we are not going to use the local objects, this is the laziest way), create a proxy using soapsuds.exe tool or create an interface and reference that interface. Soapsuds tool has a limitation in that you need to create a proxy for each of the class objects in an assembly. Try the following command at .NET command prompt, you should have "proxy.dll" created for you by soapsuds.exe and you can add reference to this proxy object. But this will not work for assemblies that encapsulate several classes.

  3. We can also use more powerful WSDL.exe tool to create proxy class source code which can be included in the project. The remote objects can be created as if they are local to the client.

    If you open this file, you will find URL, http://localhost/server/server.soap in the class constructor. This proxy class will also have other methods like BegingetServerTime and EndgetServerTime which are used for making asynchronous calls.

  4. Other recommended method is to use Interfaces as we are doing in this case. In the client ASP.NET project add reference to the ISever.dll.
  5. Type
    Imports System.Runtime.Remoting

    at the top of the page to use .NET remoting infrastructure.

  6. Now type the following code in the Page_Load event of the WebForm1.
    Private Sub Page_Load(ByVal sender As System.Object, _
                       ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim objRemote As IServer.IServer
    
        'Getting transperent proxy 
    
        objRemote = Activator.GetObject(GetType(IServer.IServer), _
                      "http://localhost/server/server.soap")
    
        'Executing remote object function
    
        Response.Write(objRemote.getServerTime())
    
    End Sub
    
  7. Press F5 to execute and you should see the time displayed.

Points of Interest

You may be surprised that there is no mention of Client.exe.config or Global.aspx files so far. Client.exe.config file is required only when you want to use "new" operator instead of Activator.GetObject method. Having this file is a good idea when server hosting remote objects is changing often and your code will execute normally by modifying ObjectUri in client.exe.config file without recompiling the server objects. This can also be achieved (as I do) by specifying remote object's URL as key under appSettings tag in web.config. This technique is extremely useful when copying projects from development/testing to production environment, all that you have to do is modify these config files to change the remote object's URL!

Another important aspect to remember is, if you want to pass custom class objects as parameters between the client and server application, these objects should be serialized at one end and deserialized at the other end. This can be achieved by implementing ISerializable interface or implement your own methods to convert objects into byte arrays.

History

Posted for the first time.

References

  1. Absolute beginner's introduction to remoting
  2. Simple but potentially useful example of .NET Remoting
  3. .NET Remoting - Basic Maneuvers
  4. MSDN

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Raj Settipalli



Occupation: Web Developer
Location: Australia Australia

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Subject  Author Date 
Generalu just copied others workmemberRAINBOW0070:23 28 Feb '08  
GeneralRe: u just copied others workmemberRaj Settipalli0:33 28 Feb '08  
QuestionProblem with .NET 2.0 :(memberajai80854:58 21 Sep '07  
GeneralEasy to understand, great articlememberrderose12:59 16 May '07  
GeneralWhat if server assemble containe more than one classmembersan.vb1:39 26 Feb '07  
GeneralRe: What if server assemble containe more than one classmemberRaj Settipalli14:19 26 Feb '07  
QuestionCannot load typememberdistressedflesh18:47 6 Feb '07  
GeneralChange in .net 2.0?memberfleclerc21:34 13 Nov '06  
GeneralDoesnt work on a live websitememberHemalRathod1:37 19 Aug '06  
GeneralRe: Doesnt work on a live websitememberRaj Settipalli12:21 24 Aug '06  
GeneralStrange problemmemberHet21092:54 25 Jul '06  
GeneralRe: Strange problemmemberRaj Settipalli21:36 25 Jul '06  
GeneralEvent Handlermemberaahmed1917:19 22 Jan '06  
GeneralHow about using Client Activated ObjectsussDavid Bartolome7:00 19 Sep '05  
GeneralRe: How about using Client Activated ObjectmemberRaj Settipalli13:49 19 Sep '05  
General(405) Method Not Allowedmemberxty_ceron10:46 29 Jun '05  
GeneralRe: (405) Method Not Allowedmemberajai80855:00 21 Sep '07  
GeneralCannot load type Server.clsServer, Serversusspv1234562:14 31 Mar '05  
GeneralRe: Cannot load type Server.clsServer, Serversusspv1234562:19 31 Mar '05  
GeneralRe: Cannot load type Server.clsServer, ServersussAnonymous15:19 31 Mar '05  
GeneralRe: Cannot load type Server.clsServer, Serversusspv12345619:38 31 Mar '05  
GeneralRe: Cannot load type Server.clsServer, ServersussRaj Settipalli20:00 31 Mar '05  
GeneralRe: Cannot load type Server.clsServer, Servermembermikecran6:15 15 Apr '05  
General'The remote server returned an error: (404) Not Foundmemberfipil6:46 23 Mar '05  
GeneralRe: 'The remote server returned an error: (404) Not FoundmemberRaj Settiappli12:59 28 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Mar 2005
Editor: Sumalatha K.R.
Copyright 2005 by Raj Settipalli
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project