Microsoft KB Archive/810107

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

Article ID: 810107

Article Last Modified on 5/9/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



IN THIS TASK

 * SUMMARY
 * Create a Remotable Class
 * Host a Remote Object in Microsoft Internet Information Services (IIS)
 * Create a Client Web Application
 * Verify That Remoting Occurs
 * Use File Authorization to Secure Access to the Remote Object
 * Troubleshooting
 * REFERENCES



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.

back to the top

Create a Remotable Class
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Type, click Visual C# Projects. Under Template, click Class Library. In the Name text box, type SampleRemotedClassLib . In the Location text box, type C:\MyApps\SampleRemoting . Click OK.

By default, the Class1 class is created.</li>  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 description for Class1. ///    public class Class1: System.MarshalByRefObject {       public Class1 {           //            // TODO: Add constructor logic here. //       }

public string GetDateTime {           return System.DateTime.Now.ToString; }   } } </li> On the Build menu, click Build Solution.</li></ol>

back to the top

Host a Remote Object in Microsoft Internet Information Services (IIS)
<ol> Create a directory in the Inetpub\wwwroot folder, and then name the directory RemComponent .</li> Create a directory in the RemComponent directory, and then name the directory bin .</li> Copy the SampleRemotedClassLib.dll file from the C:\MyApps\SampleRemoting\bin directory to the RemComponent\bin directory.</li> Use Notepad to create a text file, and then name the text file Web.config .</li>  Copy the following text to the Web.config file, and then save the file in the RemComponent directory: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <system.runtime.remoting> <wellknown mode=&quot;SingleCall&quot; objectUri=&quot;Class1.rem&quot; type=&quot;SampleRemotedClassLib.Class1, SampleRemotedClassLib&quot;/> </system.runtime.remoting> </li> Click Start, and then point to Programs.</li> Point to Administrative Tools, and then click Internet Services Manager to open the Internet Information Services window.</li> Create a virtual directory in IIS. Set the virtual directory alias to RemComponent, and then set the source directory to the RemComponent directory.</li></ol>

back to the top

Create a Client Web Application
<ol> Start Visual Studio .NET.</li> 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.</li> Add a Button control to the WebForm1 Web form, and then add a Label control to the WebForm1.</li> In Solution Explorer, right-click References, and then click Add Reference.</li> Click Browse, and then locate the C:\Inetpub\wwwroot\RemComponent\bin folder.</li> Click the SampleRemotedClassLib.dll file, and then click OK.</li> <li>In Solution Explorer, right-click the RemotingClientApp project.</li> <li>Point to Add, and then click Add New Item.</li> <li>In the Add New Item - RemotingClientApp dialog box, click Text File under Templates.</li> <li>Type Remoting.config in the Name text box, and then click Open.</li> <li> Add Class1 to the system.runtime.remoting section of the Remoting.config file as follows: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <system.runtime.remoting> <client url=&quot;http://localhost:80/RemComponent&quot;> <wellknown type=&quot;SampleRemotedClassLib.Class1, SampleRemotedClassLib&quot; url=&quot;http://localhost:80/RemComponent/Class1.rem&quot; /> <channel ref=&quot;http&quot; useDefaultCredentials=&quot;true&quot;> <clientProviders> <formatter ref=&quot;binary&quot; /> </clientProviders> </system.runtime.remoting> </li> <li>Open the code-behind file for the Global.asax file.</li> <li> To use the &quot;System.Runtime.Remoting&quot; namespace, add the following code at the top of the Global.asax.cs file: using System.Runtime.Remoting; </li> <li> 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 = @&quot;c:\inetpub\wwwroot\RemotingClientApp\Remoting.config&quot;; // Register the remoted objects. System.Runtime.Remoting.RemotingConfiguration.Configure(remotingConfig); } </li> <li>On the Debug menu, click Start to run the application.</li> <li>Click Button.

The current date and the current time are displayed.</li></ol>

back to the top

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.

back to the top

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: <ol> <li>Locate the C:\Inetpub\wwwroot\RemComponent folder.</li> <li>Use Notepad to create a file named Class1.rem in the C:\Inetpub\wwwroot\RemComponent folder.</li> <li> Add the following line of code to the Class1.rem URI file, and then save the file: <%@ WebService class=&quot;SampleRemotedClassLib.Class1&quot; %> </li> <li>Type the following URL in the Web browser:

http://localhost/RemotingClientApp/WebForm1.aspx

</li> <li>Click Button.</li> <li>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: <ol style="list-style-type: lower-alpha;"> <li>Start Windows Explorer.</li> <li>Locate the Class1.rem file, and then click Class1.rem.</li> <li>On the File menu, click Properties.</li> <li>In the Properties dialog box, click the Security tab.</li> <li>Click to select the check boxes for the ACL authorizations.</li></ol> </li></ol>

back to the top

Troubleshooting
If the Class1.rem URI file does not have the following WebService directive: <%@ WebService class=&quot;SampleRemotedClassLib.Class1&quot; %> 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

<div class="references_section">