Archive for April, 2008

Web proxy server - CHAPTER 18 THE .NET REMOTING LAYER 601

Wednesday, April 30th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 601 Figure 18-10. The Windows Services applet That s it! Now compile your project. Installing the CarWinService Installing CarService.exe on a given machine (local or remote) requires two steps: 1. Move the compiled service assembly (and any necessary external assemblies; CarGeneralAsm. dll in this example) to the remote machine. 2. Run the installutil.exe command-line tool, specifying your service as an argument. Assuming step 1 is complete, open a Visual Studio 2005 command window, navigate to the location of the CarWinService.exe assembly, and issue the following command (note that this same tool can be used to uninstall a service as well): installutil carwinservice.exe Once this Windows service has been properly installed, you are now able to start and configure it using the Services applet, which is located under the Administrative Tools folder of your system s Control Panel. Once you have located your CarService (see Figure 18-10), click the Start link to load and run the binary. Source Code The CarWinService project is located under the Chapter 18 subdirectory. Hosting Remote Objects Using IIS Hosting a remote assembly under IIS is even simpler than building aWindows service, as IIS is preprogrammed to allow incoming HTTP requests via port 80. Now, given the fact that IIS is a web server, it should stand to reason that IIS is only able to host remote objects using the HttpChannel type (unlike aWindows service, which can also leverage the TcpChannel type). Assuming this is not perceived as a limitation, follow these steps to leverage the remoting support of IIS:
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

CHAPTER 18 600 THE .NET REMOTING LAYER (Web hosting solutions)

Tuesday, April 29th, 2008

CHAPTER 18 600 THE .NET REMOTING LAYER Figure 18-8. Including an installer for the custom Windows service Figure 18-9. Establishing the identity of the CarService Adding a Service Installer Before you can install your service on a given machine, you need to add an additional type into your current CarWinService project. Specifically, any Windows service (written using .NET or the Win32 API) requires a number of registry entries to be made to allow the OS to interact with the service itself. Rather than making these entries manually, you can simply add an Installer type to aWindows service project, which will configure your ServiceBase-derived type correctly when installed on the target machine. To add an installer for the CarService, open the design-time service editor (by double-clicking the CarService.cs file from Solution Explorer), right-click anywhere within the designer, and select Add Installer (see Figure 18-8). This selection will add a new component that derives from the System.Configuration.Install. Installer base class. On your designer will be two components. The serviceInstaller1 type represents a specific service installer for a specific service in your project. If you select this icon and view the Properties window, you will find that the ServiceName property has been set to the CarService class type. The second component (serviceProcessInstaller1) allows you to establish the identity under which the installed service will execute. By default, the Account property is set to User. Using the Properties window of Visual Studio 2005, change this value to LocalService (see Figure 18-9).
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

CHAPTER 18 THE .NET REMOTING LAYER 599 (Post office web site)

Monday, April 28th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 599 static class Program { static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new CarService() }; ServiceBase.Run(ServicesToRun); } } Implementing CarService.OnStart() You can likely already assume what sort of logic should happen when your custom service is started on a given machine. Recall that the role of CarService is to perform the same tasks as your custom console-based service. Thus, if you wish to register CarService as aWKO-singleton type that is available via HTTP, you could add the following code to the OnStart() method (as you would hope, you may make use of the RemotingConfiguration type to load up a server-side remoting *.config file, rather than hard-coding your implementation, when hosting remote objects using aWindows service): protected override void OnStart(string[] args) { // Create a new HttpChannel. HttpChannel c = new HttpChannel(32469); ChannelServices.RegisterChannel(c); // Register as single call WKO. RemotingConfiguration.RegisterWellKnownServiceType( typeof(CarGeneralAsm.CarProvider), “CarProvider.soap”, WellKnownObjectMode.SingleCall); // Log successful startup. EventLog.WriteEntry(”CarWinService”, “CarWinService started successfully!”, EventLogEntryType.Information); } Note that once the type has been registered, you log a custom message to the Windows event log (via the System.Diagnostics.EventLog type) to document that the host machine successfully started your service. Implementing OnStop() Technically speaking, the CarService does not demand any sort of shutdown logic. For illustrative purposes, let s post another event to the EventLog to log the termination of the custom Windows service: protected override void OnStop() { EventLog.WriteEntry(”CarWinService”, “CarWinService stopped”, EventLogEntryType.Information); } Now that the service is complete, the next task is to install this service on the remote machine.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Business web hosting - CHAPTER 18 598 THE .NET REMOTING LAYER

Sunday, April 27th, 2008

CHAPTER 18 598 THE .NET REMOTING LAYER Figure 18-7. Creating a new Windows Service project workspace As luck would have it, building a custom Windows service using the .NET platform is extremely simple when contrasted to the raw Win32 API. To illustrate, let s create aWindows Service project named CarWinService (see Figure 18-7) that will be in charge of hosting the remote types contained within the CarGeneralAsm.dll. Visual Studio 2005 responds by generating a partial class (named Service1 by default), which derives from System.ServiceProcess.ServiceBase, and another class (Program), which implements the service s Main() method. Given that Service1 is a rather nondescript name for your custom service, the first order of business is to change the values of the (Name) and ServiceName properties to CarService using the IDE s Properties window. The distinction between these two settings is that the (Name) value is used to define the name used to refer to your type in the code base, while the ServiceName property marks the name to display to Windows service centric configuration tools. Before moving on, be sure you set a reference to the CarGeneralAsm.dll and System.Remoting. dll assemblies, and specify the following additional using directives to the file containing the CarService class definition: using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels; using System.Diagnostics; Implementing the Main() Method The Main() method of the Program class is in charge of running each service defined in the project by passing an array of ServiceBase types into the static Service.Run() method. Given that you have renamed your custom service from Service1 to CarService, you should find the following class definition (comments deleted for clarity):
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

CHAPTER 18 THE (Medical web site) .NET REMOTING LAYER 597

Saturday, April 26th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 597 In either case, if a client or server wishes to revoke sponsorship, it may do so using the ILease. Unregister() method, for example: // Remove the sponsor for a given object. itfLeaseInfo.Unregister(mySponsor); Note Client-side sponsored objects, in addition to implementing ISponsor, must also derive from MarshalByRefObject, given that the client must pass the sponsor to the remote application domain! So, as you can see, the lifetime management of stateful MBR types is a bit more complex than a standard garbage collection. On the plus side, you have a ton of control regarding when a remote type is destined to meet its maker. However, as you may have gathered, there is the chance that a remote type may be removed from memory without the client s knowledge. Should a client attempt to invoke members on a type that has already been removed from memory, the runtime will throw a System.Runtime.Remoting.RemotingException, at which point the client may create a brand-new instance of the remote type or simply take an alternative course of action. Source Code The CAOCarGeneralAsmLease, CAOCarProviderServerLease, and CAOCarProviderClientLease projects are located under the Chapter 18 subdirectory. Alternative Hosts for Remote Objects Over the course of this chapter, you have constructed numerous console-based server hosts, which provide access to some set of remote objects. If you have a background in the classic Distributed Component Object Model (DCOM), this step may have seemed a bit odd. In the world of DCOM, it was not unusual to build a single server-side COM server that contained the remote objects and was also in charge of receiving incoming requests from some remote client. This single *.exe DCOM application would quietly load in the background without presenting a looming command window. When you are building a .NET server assembly, the chances are quite good that the remote machine does not need to display any sort of UI. Rather, all you really wish to do is build a server-side entity that opens the correct channel(s) and registers the remote object(s) for client-side access. Moreover, when you build a simple console host, you (or someone) is required to manually run the server-side *.exe assembly, due to the fact that .NET remoting will not automatically run a server-side *.exe when called by a remote client. Given these two issues, the question then becomes, how can you build an invisible listener that loads automatically? .NET programmers have two major choices at their disposal when they wish to build a transparent host for various remote objects: Build a .NET Windows service application to host the remote objects. Allow IIS to host the remote objects. Hosting Remote Objects Using a Windows Service Perhaps the ideal host for remote objects is aWindows service, given that it Can be configured to load automatically on system startup Runs as an invisible background process Can be run under specific user accounts
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

CHAPTER 18 596 THE .NET REMOTING LAYER (Free php web host)

Saturday, April 26th, 2008

CHAPTER 18 596 THE .NET REMOTING LAYER Client-Side Lease Adjustment On an additional ILease-related note, it is possible for the client s application domain to adjust the current lease properties for a CAO/WKO-singleton type it is communicating with across the wire. To do so, the client makes use of the static RemotingServices.GetLifetimeService() method. As a parameter to this member, the client passes in the reference to the remote type as follows: // Client-side lease adjustment. CarProvider cp = new CarProvider(cars); ILease itfLeaseInfo = (ILease)RemotingServices.GetLifetimeService(cp); if(itfLeaseInfo.CurrentLeaseTime.TotalMinutes < 10.0) itfLeaseInfo.Renew(TimeSpan.FromMinutes(1000)); This approach can be helpful if the client s application domain is about to enter a lengthy process on the same thread of execution that is using the remote type. For example, if a single-threaded application is about to print out a 100-page document, the chances are quite good that a remote CAO/WKOsingleton type may time out during the process. The other (more elegant) solution, of course, is to spawn a secondary thread of execution, but I think you get the general idea. Server-Side (and Client-Side) Lease Sponsorship The final topic regarding the lease-based lifetime of a CAO/WKO-singleton object to consider is the notion of lease sponsorship. As you have just seen, every CAO/WKO-singleton entity has a default lease, which may be altered in a number of ways on both the server side as well as the client side. Now, regardless of the type s lease configuration, eventually an MBR object s time will be up. At this point, the runtime will garbage-collect the entity . . . well, almost. The truth of the matter is that before an expired type is truly marked for garbage collection, the runtime will check to see if the MBR object in question has any registered lease sponsors. Simply put, a sponsor is a type that implements the ISponsor interface, which is defined as follows: public interface System.Runtime.Remoting.Lifetime.ISponsor { TimeSpan Renewal(ILease lease); } If the runtime detects that an MBR object has a sponsor, it will not garbage-collect the type, but rather call the Renewal() method of the sponsor object to (once again) add time to the current lease. On the other hand, if the MBR has no sponsor, the object s time is truly up. Assuming that you have created a custom class that implements ISponsor, and thus implements Renewal() to return a specific unit of time (via the TimeSpan type), the next question is how exactly to associate the type to a given remote object. Again, this operation may be performed by either the server s application domain or the client s application domain. To do so, the interested party obtains an ILease reference (via the inherited GetLifetimeService() method on the server or using the static RemotingServices.GetLifetimeService() method on the client) and calls Register(): // Server-side sponsor registration. CarSponsor mySponsor = new CarSponsor(); ILease itfLeaseInfo = (ILease)this.GetLifetimeService(); itfLeaseInfo.Register(mySponsor); // Client-side sponsor registration. CarSponsor mySponsor = new CarSponsor(); CarProvider cp = new CarProvider(cars); ILease itfLeaseInfo = (ILease)RemotingServices.GetLifetimeService(cp); itfLeaseInfo.Register(mySponsor);
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Top ten web hosting - CHAPTER 18 THE .NET REMOTING LAYER 595

Friday, April 25th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 595 Notice how the leaseTime and renewOnCallTime properties have been marked with the M suffix, which as you might guess stands for the number of minutes to set for each lease-centric unit of time. If you wish, your
element may also suffix the numerical values with MS (milliseconds), S (seconds), H (hours), or even D (days). Now recall that when you update the server s *.config file, you have effectively changed the leasing characteristics for each CAO/WKO-singleton object hosted by the server. As an alternative, you may choose to programmatically override the InitializeLifetime() method in a specific remote type: public class CarProvider : MarshalByRefObject { public override object InitializeLifetimeService() { // Obtain the current lease info. ILease itfLeaseInfo = (ILease) base.InitializeLifetimeService(); // Adjust settings. itfLeaseInfo.InitialLeaseTime = TimeSpan.FromMinutes(50); itfLeaseInfo.RenewOnCallTime = TimeSpan.FromMinutes(10); return itfLeaseInfo; } … } Here, the CarProvider has altered its InitialLeaseTime value to 50 minutes and its RenewOn- CallTime value to 10. Again, the benefit of overriding InitializeLifetimeServices() is the fact that you can configure each remote type individually. Finally, on an odd note, if you wish to disable lease-based lifetime for a given CAO/WKO-singleton object type, you may override InitializeLifetimeServices() and simply return null. If you do so, you have basically configured an MBR type that will never die as long as the hosting server application is alive and kicking. Server-Side Lease Adjustment As you have just seen, when an MBR type overrides InitializeLifetimeServices(), it is able to change its default leasing behavior at the time of activation. However, for the sake of argument, what if a remote type desires to change its current lease after its activation cycle? For example, assume the CarProvider has a new method whose implementation requires a lengthy operation (such as connecting to a remote database and reading a large set of records). Before beginning the task, you may programmatically adjust your lease such that if you have less than one minute, you renew the lease time to ten minutes. To do so, you can make use of the inherited MarshalByRefObject.GetLifetimeService() and ILease.Renew() methods as follows: // Server-side lease adjustment. // Assume this new method is of the CarProvider type. public void DoLengthyOperation() { ILease itfLeaseInfo = (ILease)this.GetLifetimeService(); if(itfLeaseInfo.CurrentLeaseTime.TotalMinutes < 1.0) itfLeaseInfo.Renew(TimeSpan.FromMinutes(10)); // Do lengthy task... }
You want to have a cheap webhost for your apache application, then check apache web hosting services.

CHAPTER 18 594 THE .NET REMOTING LAYER (Web server logs)

Thursday, April 24th, 2008

CHAPTER 18 594 THE .NET REMOTING LAYER Figure 18-6. The default lease information for CarProvider itfLease.RenewOnCallTime.Seconds); Console.WriteLine(); } } Now that you have this helper type in place, assume LeaseInfo.LeaseStats() is called within the GetJBCByIndex() and GetAllAutos() methods of the CarProvider type. Once you recompile the server and client assemblies (again, simply to ensure Visual Studio 2005 copies the latest and greatest version of the CarGeneralAsm.dll to the client and server application directories), run the application once again. Your server s console window should now look something like Figure 18-6. Altering the Default Lease Characteristics Obviously, the default lease characteristics of a CAO/WKO-singleton type may not be appropriate for each and every CAO or WKO-singleton remote object. If you wish to alter these default settings, you have two approaches: You can adjust the default lease settings using a server-side *.config file. You can programmatically alter the settings of a type s default lease by overriding members of the MarshalByRefObject base class. While each of these options will indeed alter the default lease settings, there is a key difference. When you make use of a server-side *.config file, the lease settings affect all objects hosted by the server process. In contrast, when you override select members of the MarshalByRefObject type, you are able to change lease settings on an object-by-object basis. To illustrate changing the default lease settings via a remoting *.config file, assume you have updated the server-side XML data with the following additional
element:

We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

CHAPTER 18 THE .NET REMOTING LAYER (Web server on xp) 593

Wednesday, April 23rd, 2008

CHAPTER 18 THE .NET REMOTING LAYER 593 LeaseState CurrentState { get; } TimeSpan InitialLeaseTime { get; set; } TimeSpan RenewOnCallTime { get; set; } TimeSpan SponsorshipTimeout { get; set; } void Register(System.Runtime.Remoting.Lifetime.ISponsor obj); void Register(System.Runtime.Remoting.Lifetime.ISponsor obj, TimeSpan renewalTime); TimeSpan Renew(TimeSpan renewalTime); void Unregister(System.Runtime.Remoting.Lifetime.ISponsor obj); } The ILease interface not only allows you to obtain information regarding the current lease (via CurrentLeaseTime, CurrentState, and InitialLeaseTime), but also provides the ability to build lease sponsors (more details on this later). Table 18-6 documents role of each ILease member. Table 18-6. Members of the ILease Interface Member Meaning in Life CurrentLeaseTime Gets the amount of time remaining before the object deactivates, if it does not receive further method invocations. CurrentState Gets the current state of the lease, represented by the LeaseState enumeration. InitialLeaseTime Gets or sets the initial amount of time for a given lease. The initial lease time of an object is the amount of time following the initial activation before the lease expires if no other method calls occur. RenewOnCallTime Gets or sets the amount of time by which a call to the remote object increases the CurrentLeaseTime. SponsorshipTimeout Gets or sets the amount of time to wait for a sponsor to return with a lease renewal time. Register() Overloaded. Registers a sponsor for the lease. Renew() Renews a lease for the specified time. Unregister() Removes a sponsor from the sponsor list. To illustrate the characteristics of the default lease of a CAO or WKO-singleton remote object, assume that your current CAOCarGeneralAsm project has defined a new internal class named LeaseInfo. LeaseInfo supports a static member named LeaseStats(), which dumps select statistics regarding the current lease for the CarProvider type to the server-side console window (be sure to specify a using directive for the System.Runtime.Remoting.Lifetime namespace to inform the compiler where the ILease type is defined): internal class LeaseInfo { public static void LeaseStats(ILease itfLease) { Console.WriteLine(”***** Lease Stats *****”); Console.WriteLine(”Lease state: {0}”, itfLease.CurrentState); Console.WriteLine(”Initial lease time: {0}:{1}”, itfLease.InitialLeaseTime.Minutes, itfLease.InitialLeaseTime.Seconds); Console.WriteLine(”Current lease time: {0}:{1}”, itfLease.CurrentLeaseTime.Minutes, itfLease.CurrentLeaseTime.Seconds); Console.WriteLine(”Renew on call time: {0}:{1}”, itfLease.RenewOnCallTime.Minutes,
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

CHAPTER 18 592 THE .NET REMOTING LAYER (Cedant web hosting)

Tuesday, April 22nd, 2008

CHAPTER 18 592 THE .NET REMOTING LAYER Source Code The CAOCarGeneralAsm, CAOCarProviderServer, and CAOCarProviderClient projects are located under the Chapter 18 subdirectory. The Lease-Based Lifetime of CAO/WKO-Singleton Objects As you have seen, WKO types configured with single call activation are alive only for the duration of the current method call. Given this fact, WKO single call types are stateless entities. As soon as the current invocation has completed, the WKO single call type is a candidate for garbage collection. On the other hand, CAO types and WKO types that have been configured to use singleton activation are both, by their nature, stateful entities. Given these two object configuration settings, the question that must be asked is, how does the server process know when to destroy these MBR objects? Clearly, it would be a huge problem if the server machine garbage-collected MBR objects that were currently in use by a remote client. If the server machine waits too long to release its set of MBR types, this may place undo stress on the system, especially if the MBR object(s) in question maintain valuable system resources (database connections, unmanaged types, and whatnot). The lifetime of a CAO or WKO-singleton MBR type is governed by a lease time that is tightly integrated with the .NET garbage collector. If the lease time of a CAO or WKO-singleton MBR type expires, the object is ready to be garbage-collected on the next collection cycle. Like any .NET type, if the remote object has overridden System.Object.Finalize() (via the C# destructor syntax), the .NET runtime will indeed trigger the finalization logic. The Default Leasing Behavior CAO and WKO-singleton MBR types have what is known as a default lease, which lasts for five minutes. If the runtime detects five minutes of inactivity have passed for a CAO or WKO-singleton MBR type, the assumption is that the client is no longer making use of the object and therefore the remote object may be garbage-collected. However, when the default lease expires, this does not imply that the object is immediately marked for garbage collection. In reality, there are many ways to influence the behavior of the default lease. First and foremost, anytime the remote client invokes a member of the remote CAO or WKOsingleton MBR type, the lease is renewed back to its five-minute limit. In addition to the automatic client-invocation-centric renew policy, the .NET runtime provides three additional alternatives: *.config files can be authored that override the default lease settings for remote objects. Server-side lease sponsors can be used to act on behalf of a remote object whose lease time has expired. Client-side lease sponsors can be used to act on behalf of a remote object whose lease time has expired. We will check out these options over the next several sections, but for the time being let s examine the default lease settings of a remote type. Recall that the MarshalByRefObject base class defines amember named GetLifetimeService(). This method returns a reference to an internally implemented object that supports the System.Runtime.Remoting.Lifetime.ILease interface. As you would guess, the ILease interface can be used to interact with the leasing behavior of a given CAO or WKO-singleton type. Here is the formal definition: public interface ILease { TimeSpan CurrentLeaseTime { get; }
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.