only for RuBoard |
This chapter discusses two methods of hosting remote objects. The first method is Windows Services , which are background processes that lack a user interface. They are started either manually or automatically when Windows first loads. They are ideal for hosting remote objects because they do not interfere with other users on the machine. All interaction with services is done through the Service Control Manager (SCM), as shown in Figure 9-1. This program is run from the Administrative Tools directory in the Control Panel and is simply called "Services." Typically, Windows Services can be started, stopped , and paused from within the SCM. When these actions occur, a corresponding method within the service is called, allowing reciprocating action to occur.
The second method involves the use of an Internet Information Server (IIS) to host remotable objects. The advantages of this method are that no code is required and it only takes a few moments to configure. However, the configuration options are limited, compared to what can be done with a Windows Service, and the only available channel is HttpChannel . Windows Services can also use TcpChannel , which is considerably faster.
A remotable object is an essential ingredient for hosting a remote object. Example 9-1 contains a remotable version of the ServerInfo class from Chapter 7 (minus a few superfluous methods). The class provides rudimentary information regarding a machine, such as IP address and processor utilization. It is used to demonstrate various remoting concepts in this chapter. Compile it to an assembly called ServerInfo.dll , which will be used throughout the chapter (but be ready to modify it at a moment's notice).
'vbc /t:library /r:System.dll serverinfo.vb Imports System Imports System.Diagnostics Imports System.Net Imports System.Threading Namespace ObjectServerSpace Public Class ServerInfo : Inherits MarshalByRefObject Private machine As String Private ip As IPAddress Private Shared calls As Integer Public Sub New( ) 'Get machine info when object is created machine = Dns.GetHostName( ) Dim ipHost As IPHostEntry = Dns.GetHostByName(machine) ip = ipHost.AddressList(0) calls = 0 End Sub 'Shared method Public Function GetMachineTime( ) As DateTime calls += 1 Return DateTime.Now End Function 'Get % of process currently in use Public Function GetProcessorUsed( ) As Single calls += 1 If PerformanceCounterCategory.Exists("Processor") Then Dim pc As New PerformanceCounter("Processor", _ "% Processor Time", "_Total", True) Dim sampleA As CounterSample Dim sampleB As CounterSample sampleA = pc.NextSample( ) Thread.Sleep(1000) sampleB = pc.NextSample( ) Return CounterSample.Calculate(sampleA, sampleB) End If End Function 'Get MBytes of free memory Public Function GetAvailableMemory( ) As Long calls += 1 If PerformanceCounterCategory.Exists("Memory") Then Dim pc As New PerformanceCounter("Memory", "Available MBytes") Return pc.RawValue( ) End If Return 0 End Function Public ReadOnly Property MachineName( ) As String Get calls += 1 Return machine End Get End Property Public ReadOnly Property IPAddress( ) As IPAddress Get calls += 1 Return ip End Get End Property Public ReadOnly Property CallCount( ) As Integer Get Return calls End Get End Property End Class End Namespace
only for RuBoard |