Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Host a Remote Object in ASP.NET and Use File Authorization to Gain Access to the Object


View products that this article applies to.

Summary

This step-by-step article describes how to host a remote object in ASP.NET and how to use file authorization to gain access to the object.

Create a Remotable Class

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Type, click Visual C# Projects.
  4. Under Template, click Class Library.
  5. In the Name text box, type SampleRemotedClassLib.
  6. In the Location text box, type C:\MyApps\SampleRemoting.
  7. Click OK.

    By default, the Class1 class is created.
  8. To add a GetDateTime public method to Class1, replace the existing code in the Class1.cs file with the following code:
    using System;
    
    namespace SampleRemotedClassLib
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	public class Class1: System.MarshalByRefObject
    	{
    		public Class1()
    		{
    			//
    			// TODO: Add constructor logic here.
    			//
    		}
    
    		public string GetDateTime()
    		{
    			return System.DateTime.Now.ToString();
    		}
    	}
    }
  9. On the Build menu, click Build Solution.

Host a Remote Object in Microsoft Internet Information Services (IIS)

  1. Create a directory in the Inetpub\wwwroot folder, and then name the directory RemComponent.
  2. Create a directory in the RemComponent directory, and then name the directory bin.
  3. Copy the SampleRemotedClassLib.dll file from the C:\MyApps\SampleRemoting\bin directory to the RemComponent\bin directory.
  4. Use Notepad to create a text file, and then name the text file Web.config.
  5. Copy the following text to the Web.config file, and then save the file in the RemComponent directory:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.runtime.remoting>
    	<application>
    	   <service>
    	     <wellknown mode="SingleCall" 
                      objectUri="Class1.rem" 
                      type="SampleRemotedClassLib.Class1, SampleRemotedClassLib"/> 
    	    </service>
    	</application>
        </system.runtime.remoting>
    </configuration>
  6. Click Start, and then point to Programs.
  7. Point to Administrative Tools, and then click Internet Services Manager to open the Internet Information Services window.
  8. Create a virtual directory in IIS. Set the virtual directory alias to RemComponent, and then set the source directory to the RemComponent directory.

Create a Client Web Application

  1. Start Visual Studio .NET.
  2. Use Microsoft Visual C# .NET to create an ASP.NET Web Application project, and then name the project RemotingClientApp.

    By default, the WebForm1.aspx file is created.
  3. Add a Button control to the WebForm1 Web form, and then add a Label control to the WebForm1.
  4. In Solution Explorer, right-click References, and then click Add Reference.
  5. Click Browse, and then locate the C:\Inetpub\wwwroot\RemComponent\bin folder.
  6. Click the SampleRemotedClassLib.dll file, and then click OK.
  7. In Solution Explorer, right-click the RemotingClientApp project.
  8. Point to Add, and then click Add New Item.
  9. In the Add New Item - RemotingClientApp dialog box, click Text File under Templates.
  10. Type Remoting.config in the Name text box, and then click Open.
  11. Add Class1 to the system.runtime.remoting section of the Remoting.config file as follows:
    <?xml version="1.0" encoding="utf-8"?> 
    <configuration>
    	<system.runtime.remoting>
    		<application>
    		<client url="http://localhost:80/RemComponent">
    			<wellknown type="SampleRemotedClassLib.Class1, SampleRemotedClassLib" 
    url="http://localhost:80/RemComponent/Class1.rem" />
    		</client>
    		<channels>
    			<channel ref="http" useDefaultCredentials="true">
    				<clientProviders>
    					<formatter ref="binary" />
    				</clientProviders>
    			</channel>
    		</channels>
    		</application>
    	</system.runtime.remoting>
    </configuration>            
    
  12. Open the code-behind file for the Global.asax file.
  13. To use the "System.Runtime.Remoting" namespace, add the following code at the top of the Global.asax.cs file:
    using System.Runtime.Remoting;
    
  14. To use the Remoting.config file to register the objects to be remoted, modify the Application_Start event as follows:
    protected void Application_Start(Object sender, EventArgs e)
    {
      // Set the name of the remoting configuration file.
      string remotingConfig = @"c:\inetpub\wwwroot\RemotingClientApp\Remoting.config";		
      // Register the remoted objects.
      System.Runtime.Remoting.RemotingConfiguration.Configure(remotingConfig);
    }
  15. On the Debug menu, click Start to run the application.
  16. Click Button.

    The current date and the current time are displayed.

Verify That Remoting Occurs

  1. Locate the %windir%\system32\Logfiles\W3SVC1 folder.
  2. Open the log file for the current day.
  3. Verify that the Class1.rem file is accessed through IIS.

Use File Authorization to Gain Access to the Remote Object

To use file authorization to gain access to the remote object, add a file that has the Class1.rem Uniform Resource Identifier (URI) name to the RemComponent folder. To do this, follow these steps:
  1. Locate the C:\Inetpub\wwwroot\RemComponent folder.
  2. Use Notepad to create a file named Class1.rem in the C:\Inetpub\wwwroot\RemComponent folder.
  3. Add the following line of code to the Class1.rem URI file, and then save the file:
    <%@ WebService class="SampleRemotedClassLib.Class1" %>
    
  4. Type the following URL in the Web browser:

    http://localhost/RemotingClientApp/WebForm1.aspx
  5. Click Button.
  6. You may change the Access Control List (ACL) for the Class1.rem file to use file authorization to gain access to the remote object. To change the ACL for a file, follow these steps:
    1. Start Windows Explorer.
    2. Locate the Class1.rem file, and then click Class1.rem.
    3. On the File menu, click Properties.
    4. In the Properties dialog box, click the Security tab.
    5. Click to select the check boxes for the ACL authorizations.

Troubleshooting

If the Class1.rem URI file does not have the following WebService directive:
<%@ WebService class="SampleRemotedClassLib.Class1" %>
you may receive the following error message:
Server Error in '/RemotingClientApp' Application.
--------------------------------------------------------------------------------
BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1835627630.1699884645.
Description: An unhandled exception occurred during the execution of the current Web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.Serialization.SerializationException: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1835627630.1699884645.

↑ Back to the top


References

For more information about remoting, visit the following Microsoft Web sites:
An Introduction to Microsoft .NET Remoting Framework
http://msdn2.microsoft.com/en-us/library/ms973864.aspx
Microsoft .NET Remoting: A Technical Overview
http://msdn2.microsoft.com/en-us/library/ms973857.aspx\
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
323490 INFO: Configure .NET Remoting When the Remoting Client Is an ASP.NET Application or the Client Is Another Remoted Component That Is Hosted by IIS

↑ Back to the top


Keywords: KB810107, kbhowtomaster, kbremoting, kbconfig, kbdll, kbwebforms

↑ Back to the top

Article Info
Article ID : 810107
Revision : 6
Created on : 5/9/2007
Published on : 5/9/2007
Exists online : False
Views : 494