How To: Host a Remote Object in a Windows Service
J.D. Meier, Alex Mackman, Michael Dunner, and Srinath Vasireddy
Microsoft Corporation
November 2002
Microsoft® ASP.NET
Microsoft Visual Studio® .NET
Microsoft Windows® 2000
See the Landing Page for a starting point and complete overview of Building Secure ASP.NET Applications.
Summary: Objects called using the .NET Remoting infrastructure can be hosted by ASP.NET, custom executables or Windows services. This How To shows you how to host a remote object in a Windows service and call it from an ASP.NET Web application. (7 printed pages)
Contents
Notes
Requirements
Summary
References
This How To describes how to host a remote object in a Windows service and call it from an ASP.NET Web application.
Notes
- Remote objects (that is, .NET objects accessed remotely using .NET Remoting technology) can be hosted in Windows services, custom executables, or ASP.NET.
- Clients communicate with remote objects hosted in custom executables or Windows services by using the TCP channel.
- Clients communicate with remote objects hosted in ASP.NET by using the HTTP channel.
- If security is the prime concern, host objects in ASP.NET and use the HTTP channel. This allows you to benefit from the underlying security features of ASP.NET and IIS.
For information about how to host a remote object in ASP.NET (with IIS), see article Q312107, "HOW TO: Host a Remote Object in Microsoft Internet Information Services."
- If performance is the prime concern, host objects in a Windows service and use the TCP channel. This option provides no built-in security.
Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs you will need.
- Microsoft® Windows® 2000 operating system
- Microsoft Visual Studio® .NET development system
The procedures in this article also require that you have knowledge of the Microsoft Visual C#™ development tool.
Summary
This How To includes the following procedures.
- Create the Remote Object Class
- Create a Windows Service Host Application
- Create a Windows Account to Run the Service
- Install the Windows Service
- Create a Test Client Application
1. Create the Remote Object Class
This procedure creates a simple remote object class. It provides a single method called Add that will add two numbers together and return the result.
To create the remote object class
- Start Visual Studio .NET and create a new Visual C# Class Library project called RemoteObject.
- Use Solution Explorer to rename class1.cs as Calculator.cs.
- In Calculator.cs, rename Class1 as Calculator and rename the default constructor accordingly.
- Derive the Calculator class from MarshalByRefObject to make the class remotable.
public class Calculator : MarshalByRefObject
- Add the following public method to the Calculator class.
public int Add( int operand1, int operand2 ) { return operand1 + operand2; }
- On the Build menu, click Build Solution.
2. Create a Windows Service Host Application
This procedure creates a Windows service application, which will be used to host the remote object. When the service is started it will configure the TCP remoting channel to listen for client requests.
Note This procedure uses an Installer class and the Installutil.exe command line utility to install the Windows service. To uninstall the service, run Installutil.exe with the /u switch. As an alternative, you could use a Setup and Deployment Project to help install and uninstall the Windows service.
To create a Windows Service host application
- Add a new Visual C# Windows Service project called RemotingHost to the current solution.
- Use Solution Explorer to rename Service1.cs as RemotingHost.cs.
- In RemotingHost.cs, rename the Service1 class as HostService and rename the default constructor accordingly.
- At the top of the file, add the following using statement beneath the existing using statements.
using System.Runtime.Remoting;
- Locate the Main method and replace the existing line of code that initializes the ServicesToRun variable with the following.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new HostService() };
- Locate the InitializeComponent method and set the ServiceName property to RemotingHost.
this.ServiceName = "RemotingHost";
- Locate the OnStart method and add the following line of code to configure remoting. The fully qualified path to the configuration file will be passed as a start parameter to the service.
RemotingConfiguration.Configure(args[0]);
- Add a new C# class file to the project and name it HostServiceInstaller.
- Add an assembly reference to the System.Configuration.Install.dll assembly.
- Add the following using statements to the top of HostServiceInstaller beneath the existing using statement.
using System.ComponentModel; using System.ServiceProcess; using System.Configuration.Install;
- Derive the HostServiceInstaller class from the Installer class.
public class HostServiceInstaller : Installer
- Add the RunInstaller attribute at the class level as follows.
[RunInstaller(true)] public class HostServiceInstaller : Installer
- Add the following two private member variables to the HostServiceInstaller class. The objects will be used when installing the service.
private ServiceInstaller HostInstaller; private ServiceProcessInstaller HostProcessInstaller;
- Add the following code to the constructor of the HostServiceInstaller class.
HostInstaller = new ServiceInstaller(); HostInstaller.StartType = System.ServiceProcess.ServiceStartMode.Manual; HostInstaller.ServiceName = "RemotingHost"; HostInstaller.DisplayName = "Calculator Host Service"; Installers.Add (HostInstaller); HostProcessInstaller = new ServiceProcessInstaller(); HostProcessInstaller.Account = ServiceAccount.User; Installers.Add (HostProcessInstaller);
- Within Solution Explorer, right-click RemotingHost, point to Add, and then click Add New Item.
- In the Templates list, click Text File and name the file app.config.
Configuration files with the name app.config are automatically copied by Visual Studio .NET as part of the build process to the output folder (for example, <projectdir>\bin\debug) and renamed as <applicationname>.config.
- Click OK to add the new configuration file.
- Add the following configuration elements to the new configuration file.
<configuration> <system.runtime.remoting> <application name="RemoteHostService"> <service> <wellknown type="RemoteObject.Calculator, RemoteObject" objectUri="RemoteObject.Calculator" mode="Singleton" /> </service> <channels> <channel ref="tcp" port="8085"> <serverProviders> <formatter ref="binary" /> </serverProviders> </channel> </channels> </application> </system.runtime.remoting> </configuration>
- On the Build menu, click Build Solution.
3. Create a Windows Account to Run the Service
This procedure creates a Windows account used to run the Windows service.
To create a Windows account to run the service
- Create a new local user account called RemotingAccount. Enter a password and select the Password never expires check box.
- In the Administrative Tools programs group, click Local Security Policy.
- Use the Local Security Policy tool to give the new account the Log on as a service privilege.
4. Install the Windows Service
This procedure installs the Windows service using the installutil.exe utility and then start the service.
To install the Windows service
- Open a command window and change directory to the Bin\Debug directory beneath the RemotingHost project folder.
- Run the installutil.exe utility to install the service.
installutil.exe remotinghost.exe
- In the Set Service Login dialog box, enter the user name and password of the account created earlier in procedure 3 and click OK.
View the output from the installutil.exe utility and confirm that the service is installed correctly.
- Copy the RemoteObject.dll assembly into the RemotingHost project output directory (that is, RemotingHost\Bin\Debug).
- From the Administrative Tools program group, start the Services MMC snap-in.
- In the Services list, right-click Calculator Host Service, and then click Properties.
- Enter the full path to the service's configuration file (remotinghost.exe.config) into the Start parameters field.
Note A quick way to do this is to select and copy the Path to executable field and paste it into the Start parameters field. Then append the ".config" string.
- Click Start to start the service.
- Confirm that the service status changes to Started.
- Click OK to close the Properties dialog box.
5. Create a Test Client Application
This procedure creates a test console application that is used to call the remote object within the Windows service.
To create a test client application
- Add a new Visual C# Console application called RemotingClient to the current solution.
- Within Solution Explorer, right-click RemotingClient, and then click Set as StartUp Project.
- Add an assembly reference to the System.Runtime.Remoting.dll assembly.
- Add a project reference to the RemoteObject project.
- Add the following using statements to the top of class1.cs beneath the existing using statements.
using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemoteObject;
- Add the following test code to the Main method to call and invoke the Calculator object hosted by the Windows service.
TcpChannel chan = new TcpChannel(); ChannelServices.RegisterChannel(chan); Calculator calc = (Calculator)Activator.GetObject( typeof(RemoteObject.Calculator), "tcp://localhost:8085/RemoteObject.Calculator"); if (calc == null) System.Console.WriteLine("Could not locate server"); else Console.WriteLine("21 + 21 is : " + calc.Add(21,21) );
- On the Build menu, click Build Solution.
- Run the client application and confirm that the correct result is displayed in the console output window.
References
For information about how to host a remote object in ASP.NET (with IIS), see article Q312107, "HOW TO: Host a Remote Object in Microsoft Internet Information Services".