Examining a Basic Lifetime Example

In the following example, I show you how to implement the different changes in an object's lifetime in one application. The server will therefore export three MarshalByRefObjects as Singletons DefaultLifeTimeSingleton, which will use the base lifetime set by a configuration file LongerLivingSingleton, which will override InitializeLifetimeService() to return a different lease time and finally InfinitelyLivingSingleton, which will just return null from InitializeLifetimeServices(). As you can...

Property and manually assign this principal to System ThreadingThread CurrentPrincipal But as you will see later when

The reason for selecting the TCP and not the (faster) IPC channel for this sample was to get an easy means for tracing the traffic between the client and the server to see how encryption works. Before changing any settings, let's use a trace tool for sniffing the traffic between the two applications. I have selected the SOAP Trace Utility included in the Microsoft SOAP Toolkit 3.0 because it makes it really very easy to set up a tracing environment that works on the local machine. The SOAP...

Multihomed Machines and Firewalls

A multihomed machine is a computer that contains more than one network interface, or, in the context of remoting, more than one IP address. This includes routers, firewalls, and also servers that act as VPN gateways or that are connected to a dial-up line. If your remoting services run on a multihomed machine, you might have to take some extra precautions to enable correct functionality. If your application only contains server-activated objects and if you never pass MarshalByRefObjects method...

Using the Sinks

To use the sinks on the client and server side of a channel, you simply have to include them in your configuration files. In the client-side configuration file, you have to incorporate the information shown in the following code. If you place the CompressionSink assembly in the GAC, mind that you have to specify the complete strong name in the type attribute < configuration> < system.runtime.remoting> < application> < channels> < channel ref http> formatter ref soap > <...

Remoting Components Hosted in IIS As Clients

As you already know from the previous chapters, it is possible to host .NET Remoting components in IIS, too. In this case, the ASP.NET runtime itself hosts the server component, and configuration of the server is done through web.config. Any < service> configuration found in web.config is automatically handled by ASPNET, which means that you don't need to call RemotingConfiguration.Configure() for your server configuration. If a client configuration is found in web.config, it will be...

Creating the Providers

Contrary to the previous sink, the EncryptionSink expects certain parameters to be present in the configuration file. The first one is algorithm, which specifies the cryptographic algorithm that should be used (DES, TripleDES, RC2, or Rijndael). The second parameter, keyfile, specifies the location of the previously generated symmetric keyfile. The same file has to be available to both the client and the server sink. The following excerpt from a configuration file shows you how the client-side...

Passing Runtime Information

The previous sinks were IClientChannelSinks and IServerChannelSinks. This means that they work on the resulting stream after the formatter has serialized the IMessage object. IMessageSinks, in contrast, can work directly on the message's contents before they are formatted. This means that any changes you make to the IMessage's contents will be serialized and therefore reflected in the resulting stream. Caution Even though you might be tempted to change the IMessage object's content in an...

Avoiding the Binary Formatter Version Mismatch

Custom .NET Remoting sinks can also be used as a workaround for certain glitches inside the framework. You can use them to change the way the .NET Remoting framework treats several exception conditions.2 As you've read in Chapter 10, there is a misleading exception that might be triggered from time to time when you use the HttpChannel with the BinaryFormatter while hosting your server-side components in IIS. In some cases, IIS sends back detailed information for some errors in HTML format. It...

Creating Server Side Sinks

The creation of server-side sinks works a little differently from the creation of the client-side sinks. As you've seen previously, on the client side the necessary sinks are created when a reference to a remote object is acquired. Contrary to this, server-side sinks are created as soon as a channel is registered. When the server-side channel is created from a definition in a configuration file, the following constructor will be used public HttpServerChannel(IDictionary properties,...

Direct Transparent Creation

The .NET Remoting framework can be configured to allow client-activated objects to be created like normal objects using the new operator. Unfortunately, this manner of creation has one serious drawback you cannot use shared interfaces or base classes. This means that you either have to ship the compiled objects to your clients or use SoapSuds to extract the metadata. This tool allows you to extract a metadata-only assembly out of a running server or a serverside implementation assembly. In the...

Changing the Programming Model

The previous sinks all add functionality to both the client- and the server-side of a .NET Remoting application. The pluggable sink architecture nevertheless also allows the creation of sinks, which change several aspects of the programming model. In Chapter 5, for example, you've seen that passing custom credentials such as username and password involves manual setting of the channel sink's properties for each object. CustomerManager mgr new CustomerManager() IDictionary props props username...

Security with Remoting in NET 20 Beta

With the next version of the .NET Framework, version 2.0 codename Whidbey , security is an integral part of the .NET Remoting framework. The security infrastructure for .NET Remoting 2.0 includes authentication as well as channel security similar to the SSPI sample solution described earlier in this chapter. Security in .NET Remoting 2.0 will be configured directly for the channel. Therefore, all you have to know about for leveraging the security infrastructure is a couple of new channel...

Using Client Side Sponsors

When using client-side sponsors, you are basically mimicking the DCOM behavior of pinging, although you have more control over the process here. After acquiring the reference to a remote object you'll do this mostly for CAOs, as for SAOs the lifetime should normally be managed only by the server , you contact its lifetime service and register the sponsor with it. You can get an object's LifetimeService, which will be an ILease object, using the following line of code ILease lease ILease...

Using a Custom Proxy

In the previous parts of this chapter, you read about the possible ways you can extend the .NET Remoting framework using additional custom message sinks. There is another option for changing the default behavior of the remoting system custom proxy objects. Figure 13-12 shows you the default proxy configuration. Figure 13-12. The default combination of proxy objects Figure 13-12. The default combination of proxy objects You can change this by replacing RemotingProxy with a custom proxy that...

Understanding Leases

A lease holds the time-to-live information for a given object. It is therefore directly associated with a certain MarshalByRefObject's instance. At the creation of a lease, the following information is set all of the following are of type TimeSpan The initial TTL after an object's creation. The grace time for a method call that is placed on the object. Mind, though, that these times are not additive for instance, calling a method a thousand times will not result in a TTL of 2,000 minutes, but...

Using Server Side Sponsors

Server-side sponsors that are running in the same process as the target CAOs can constitute a solution to the preceding problem, but you have to keep in mind several things to make your application run stably. First, remote sponsors are MarshalByRefObjects themselves. Therefore, they also have an assigned lifetime, and you may want to manage this yourself to provide a consistent behavior. Generally you will want your server-side sponsor to be active as long as the client application is online....

NET Remoting Java RMI Bridges

Interoperability between Java and .NET becomes more and more important by now. Many large enterprises using applications based on Java as well as .NET need to integrate those applications. Although Web Services should be the primary technology because it provides the foundation for loose coupling which makes the applications more independent of each other , in some cases you might need tight coupling for performance reasons, stateful work, or similar things . When it comes to interoperability...

Creating a Console Client

The simplest .NET Remoting clients you can create are console applications. That was the reason for using them in the previous chapters. Configuration is put into the application configuration Exename.exe.config but can be put in any other file, too. In your console application project, which you can create with Visual Studio, just add references to System.Runtime.Remoting.dll and to your shared assembly General.dll. Afterwards, add a new item to your application an application configuration...

Why [One Way Events Are a Bad Idea

You might have read in some documents and articles that remoting event handlers should be defined as OneWay methods. The reason is that without defining remote event handlers this way, an exception will occur whenever a client is unreachable or has been disconnected without first unregistering the event handler. When just forwarding the call to your event's delegate, as shown in the previous server-side example, two things will happen the event will not reach all listeners, and the client that...

Porting to Windows Services

In the .NET Framework, a Windows service simply is a class that extends System.ServiceProcess. ServiceBase. You basically only have to override OnStart to do something useful. A baseline Windows service is shown in Listing 4-9. Listing 4-9. A Baseline Windows Service public class DummyService System.ServiceProcess.ServiceBase public static String SVC_NAME Some dummy service public DummyService start the service ServiceBase.Run new DummyService protected override void OnStart string args stop...

More information on MSDN

As with the ActivatedClientTypeEntry class, the WellKnownClientTypeEntry class holds the configuration of a server-activated object used by the client application. Therefore, it is the client's counterpart for the WellKnownServiceTypeEntry. typeof IRemoteComponent , new lt system.runtime.remoting gt lt application gt lt client gt lt wellknown MySharedAssembly gt lt client gt lt application gt lt system.runtime.remoting gt lt configuration gt In Chapters 2 and 3, I use this type of...

What About Interfaces

In the previous chapters, and earlier in this chapter, I stated that using SoapSuds.exe is not the best choice for your real-world applications for a number of reasons. However, if you want to use configuration files in the way I've described them earlier, then you will unfortunately have to resort to shipping your complete implementation assembly or to using SoapSuds.exe. Neither of these solutions is really favorable for most applications. There is, however, a middle-ground solution for this...

Extending the Compression Sink

The server-side sink as presented in the previous section has at least one serious problem when used in real-world applications it doesn't yet detect whether the stream is compressed or not and will always try to decompress it. This will lead to an inevitable exception when the request stream has not been compressed before. In an average remoting scenario, you have two types of users. On the one hand, there are local LAN users who connect to the server via high-speed links. If these users...

Binary Formatter Version Incompatibility

When hosting your server-side components in IIS while communicating with a BinaryFormatter which is the recommended combination , you might experience a strange exception a special form of the SerializationException as shown in Figure 10-3. Figure 10-3. The BinaryFormatter version incompatibility information Figure 10-3. The BinaryFormatter version incompatibility information I have to admit that I was quite stumped when this exception occurred for the first time in one of my applications....

Client ProvidersServer Providers

Underneath each channel property, you can configure nondefault client-side and server-side sink providers and formatter providers. Caution When any of these elements are specified, it's important to note that no default providers will be created by the system. This means that appending WSDL to the URL will only work if you explicitly specify lt provider ref wsdl gt otherwise you'll receive an exception stating that no message has been deserialized. The .NET Remoting framework is based on...

Single Call Objects

For SingleCall objects the server will create a single object, execute the method, and destroy the object again. SingleCall objects are registered at the server using the following statement typeof lt YourClass gt , lt URL gt , WellKnownObjectMode.SingleCall Objects of this kind can obviously not hold any state information, as all internal variables will be discarded at the end of the method call. The reason for using objects of this kind is that they can be deployed in a very scalable manner....

Using the IPC Channel in NET Remoting

With the next version, the .NET Framework 2.0 and Visual Studio 2005, Microsoft adds two very important features to the .NET Remoting infrastructure. First of all, security is an integral part of .NET Remoting 2.0 as you will see in the next chapter, and secondly, a new channel is included. In this chapter, I want to focus on the new channel and the configuration options coming with it. The new channel, the so-called IPC channel, is a channel optimized for interprocess communication between two...

MarshalByRef Objects

A MarshalByRefObject is a remote object that runs on the server and accepts method calls from the client. Its data is stored in the server's memory and its methods executed in the server's AppDomain. Instead of passing around a variable that points to an object of this type, in reality only a pointer-like construct called an ObjRef is passed around. Contrary to common pointers, this ObjRef does not contain the memory address, rather the server name IP address and an object identity that...

Creating the Sinks

The EncryptionClientSink and EncryptionServerSink look quite similar to the previous compression sinks. The major difference is that they have custom constructors that are called from their sink providers to set the specified encryption algorithm and key. For outgoing requests, the sinks will set the X-Encrypt header to yes and store the initialization vector in Base64 coding in the X-EncryptIV header. The complete client-side sink is shown in Listing 13-8. Listing 13-8. The...

Remote Object Get NewInstance IRemote Object Get NewInstanceint initvalue

On the server you now have to implement both interfaces and create a startup code that registers the factory as a SAO. You don't have to register the CAO in this case because every MarshalByRefObject can be returned by a method call the framework takes care of the necessity to remote each call itself, as shown in Listing 3-8. Listing 3-8. The Server-Side Factory Pattern's Implementation using System using System.Runtime.Remoting using System.Runtime.Remoting.Channels.Http using...

Contents

About the About the Technical chapter 1 Introduction to What Is Scenarios for .NET Centralized Business Physical Separation of Accessing Other Evolution of Java Java Web .NET chapter 2 .NET Remoting Advantages of .NET Ease of Extensible Interface Serialization of Lifetime Your First Remoting The First Extending the chapter 3 .NET Remoting in Types of ByValue Types of Synchronous Asynchronous One-Way Multiserver Examining a Sample Multiserver Application 60 Sharing Shared Shared Shared Base...