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. - 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(); } } }
- On the Build menu, click Build Solution.
Host a Remote Object in Microsoft Internet Information Services (IIS)
- Create a directory in the Inetpub\wwwroot folder, and then name the directory RemComponent.
- Create a directory in the RemComponent directory, and then name the directory bin.
- Copy the SampleRemotedClassLib.dll file from the C:\MyApps\SampleRemoting\bin directory to the RemComponent\bin directory.
- Use Notepad to create a text file, and then name the text file Web.config.
- 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>
- Click Start, and then point to Programs.
- Point to Administrative Tools, and then click Internet Services Manager to open the Internet Information Services window.
- 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
- Start Visual Studio .NET.
- 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. - Add a Button control to the WebForm1 Web form, and then add a Label control to the WebForm1.
- In Solution Explorer, right-click References, and then click Add Reference.
- Click Browse, and then locate the C:\Inetpub\wwwroot\RemComponent\bin folder.
- Click the SampleRemotedClassLib.dll file, and then click OK.
- In Solution Explorer, right-click the RemotingClientApp project.
- Point to Add, and then click Add New Item.
- In the Add New Item - RemotingClientApp dialog box, click Text File under Templates.
- Type Remoting.config in the Name text box, and then click Open.
- 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>
- Open the code-behind file for the Global.asax file.
- To use the "System.Runtime.Remoting" namespace, add the
following code at the top of the Global.asax.cs file:
using System.Runtime.Remoting;
- 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); }
- On the Debug menu, click Start to run the application.
- Click Button.
The current date and the current time are displayed.
Verify That Remoting Occurs
- Locate the %windir%\system32\Logfiles\W3SVC1 folder.
- Open the log file for the current day.
- 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:- Locate the C:\Inetpub\wwwroot\RemComponent folder.
- Use Notepad to create a file named Class1.rem in the C:\Inetpub\wwwroot\RemComponent folder.
- Add the following line of code to the Class1.rem URI file,
and then save the file:
<%@ WebService class="SampleRemotedClassLib.Class1" %>
- Type the following URL in the Web browser:http://localhost/RemotingClientApp/WebForm1.aspx
- Click Button.
- 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:
- Start Windows Explorer.
- Locate the Class1.rem file, and then click Class1.rem.
- On the File menu, click Properties.
- In the Properties dialog box, click the Security tab.
- 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" %>
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.
--------------------------------------------------------------------------------
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.