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...
CallContext Class
The CallContext class allows you to carry additional properties within the message exchanged between the client and the server. Therefore, you can include some additional metadata that can be used either by the server or by the client. This metadata has nothing to do with the actual business logic but more with some infrastructural topics. Typical examples are things like security-related information as authentication method and encryption method. Another example is including some message IDs...
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...
Wrapping the Channel
As you've seen with the default .NET Remoting channels, you don't have to manually create and register HttpClientChannel and HttpServerChannel but can instead use the combination in the form of HttpChannel. This isn't strictly needed for compatibility with the .NET Remoting framework, but it does provide more comfort for the developers using this channel. An additional feature you might want to implement is the default assignment of a formatter to this channel. I'm now going to show you how to...
IServerChannelSinkProvider Interface
public interface IServerChannelSinkProvider IServerChannelSink CreateSink IChannelReceiver channel void GetChannelData IChannelDataStore channelData IServerChannelSinkProvider Next get set Although fulfilling the same purpose for server sinks as IClientChannelSinkProvider for client-side sinks, the interface is differently structured. Remember that the server side doesn't need the object URI because it is receiving messages only sending processes will be done through the client sink...
Using ServerSide 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....
IClientChannelSinkProvider Interface
Sink implementations are always connected to channels through sink providers. This can also be seen in configuration files where you always configure new sinks through the lt provider gt tag and specify corresponding sink provider classes. A sink provider can be seen as the factory for a sink implementation itself. This means the sink provider is responsible for creating, initializing, and returning the actual channel sink instance. public interface IClientChannelSinkProvider IClientChannelSink...
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 [OneWay 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...
Using the CallContext
When creating distributed applications, one of the recurring requirements is to transfer runtime information between server and client. This is information that allows you to determine the context in which a piece of code is running. When you call a method locally, for example, the called code will immediately know about the caller's security permissions based on the thread's security token, and based on .NET's own code access security. When you call code remotely, however, the called code...
CrossProcess on a Single Machine
Let's assume you have two Windows Forms applications running on a single machine, and you want the two applications to be able to communicate with each other. Or suppose you have a Windows service that should exchange data with your GUI application. Which protocol can you use Remoting This is one of the cases where the TCPChannel is extremely helpful as it allows you to specify rejectRemoteRequests true upon its construction, which limits all incoming connections to the ones originating from...
Implementing the ClientSide Sink
The client-side sink extends BaseChannelSinkWithProperties and implements IClientChannelSink. In Listing 13-1, you can see a skeleton client channel sink. The positions at which you'll have to implement the preprocessing and post-processing logic have been marked TODO. This is a fully working sink it simply doesn't do anything useful yet. Listing 13-1. A Skeleton IClientChannelSink using System using System.Runtime.Remoting.Channels using System.Runtime.Remoting.Messaging using System.IO public...
Custom Exceptions
Even though remoting supports high type fidelity, transferring custom exceptions over remoting boundaries comes with its very own challenge using Serializable is not enough because the base class System.Exception already implements ISerializable. If you want to pass your custom exceptions over remoting boundaries, you therefore have to override GetObjectData and provide a custom constructor for deserialization. And don't forget to call base.GetObjectData . Please also note that your custom...
TcpClientChannel Class
While the TcpChannel class implements both the client- and the server-side part of the channel, this implementation can be used on clients only. In configuration files, it is used together with the lt clientProviders gt part for configuration of the message sinks and formatters of the channel. TcpClientChannel channel new TcpClientChannel My Tcp Channel, null lt configuration gt lt system.runtime.remoting gt lt application name FirstServer gt lt channels gt lt channel ref tcp gt lt channels gt...
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...
class MyRemoteObject MarshalByRefObject IRemoteObject
called myvalue val called old 0 new 1 , myvalue,newval current 0 ,myvalue return myvalue
Note If you want to run the client as well as the server in different logon
Next, take a look at the configuration for the server, which looks similar to the client's configuration lt configuration gt lt system.runtime.remoting gt lt application gt lt channels gt lt channel ref tcp port 1234 gt lt serverProviders gt lt provider securityPackage negotiate gt formatter ref binary gt lt serverProviders gt lt channel gt lt wellknown type Server.PersonManager, Server mode Singleton gt lt service gt lt application gt lt system.runtime.remoting gt lt configuration gt The only...
More information on MSDN Cyi
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...
Using Dynamic Sinks
As you've seen in the previous chapter, both the client-side and the server-side sink chain can call dynamic sinks. On the server side this is done by the CrossContextChannel and on the client side by the ClientContextTerminatorSink. Dynamic sinks are associated with a specific context you can read more about contexts in Chapter 11 and therefore will be called for all calls passing a context boundary. They cannot be assigned to a specific channel and will even be called for local cross-context...
BinaryFormatter 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....
ClientProvidersServerProviders
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...
Local or Remote
To verify whether or not a certain instance contains a real local object or a proxy pointing to a server, you can call RemotingServices.IsTransparentProxy as illustrated in the following snippet String filename CustomerManager mgr new CustomerManager if throw new Exception CustomerManager has not been correctly configured Checking each object after a call to new is, however, rather cumbersome and error prone, so you may want to use an alternative solution that checks the configured types after...
new BinaryServerFormatterSinkProvider formatterBinTypeFilterLevel
register the channels IDictionary dict new Hashtable dict.Add port, 1234 TcpChannel channel new TcpChannel dict, null, formatterBin lt configuration gt lt system.runtime.remoting gt lt application gt lt channels gt lt channel ref tcp port 1234 gt lt serverProviders gt lt formatter ref binary typeFilterLevel Low gt lt serverProviders gt lt channel gt lt channels gt lt service gt lt wellknown type Server.ServerImpl, Server objectUri MyServer.rem mode Singleton gt lt service gt lt application gt...
Channel
The lt channel gt tag allows you to specify a port number for the server application, to reference custom channels, and to set additional attributes on channels. When you want to use the default HTTP channel or TCP channel, this tag does not have to be specified on the client because these channels will be registered automatically by the framework. On the server, you have to specify at least a port number on which the server-side channel will listen. You have basically two ways of referencing...
SingleCall 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....
TypeFilterLevel Enumeration
The TypeFilterLevel enumeration was introduced with .NET Framework 1.1 for security reasons in the deserialization process. Theoretically, an attacker could leverage the process of deserialization. To avoid this possible security problem, TypeFilterLevel has been added to the serialization infrastructure. With a TypeLevelFilter set to low, not all types will be deserialized by the serialization runtime. If some types such as delegate types are included in the messages transmitted between the...
Encrypting the Transfer
Even though using an asymmetric symmetric combination such as HTTPS SSL for the encryption of the network traffic provides the only real security, in some situations HTTPS isn't quite helpful. First, .NET Remoting by default only supports encryption when using an HTTP channel and when hosting the server-side components in IIS. If you want to use a TCP channel or host your objects in a Windows service, there's no default means of secure communication. Second, even if you use IIS to host your...
Using the IPC Channel in NET Remoting 20
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...
OneWay Calls
One-way calls are a little different from asynchronous calls in the respect that the .NET Framework does not guarantee their execution. In addition, the methods used in this kind of call cannot have return values or out parameters. You can also use delegates to call one-way methods asynchronously, but the EndInvoke function will exit immediately without checking whether the server has finished processing yet. No exceptions are thrown, even if the remote server is down or the method call is...
MarshalByRefObjects
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...
Quit
lt 221 MyRemotingServer Service closing channel. As you can see here, several commands can be sent by the client. At the beginning of the session, after the server announces itself with 220 servername message, the client will send HELO hostname. This starts the SMTP session, and the server responds with 250 servername message. For each e-mail the client wants to send via this server, the following process takes place. First, the client starts with MAIL FROM lt e-mail address gt note that the...
Troubleshooting NET Remoting
Whenever you develop applications spanning multiple processes, machines, networks, and maybe even versions of the .NET runtime, you might have to deal with a number of potential development or deployment issues. In this chapter, I present to you the most common problems, their causes, and how to resolve them. Some of these issues and workarounds will also be mentioned in chapters covering related topics throughout the book, but here I want to provide an additional concise section that you can...
LogicalCallContext Class
The LogicalCallContext class is a special version of the CallContext class that is used during method calls to remote objects. The CallContext class itself is used for sharing data across method calls in a single logical thread but not across logical threads or across application domains. As soon as it comes to communication with other threads or remote application domains, the CallContext class automatically creates a LogicalCallContext, which is used for transmitting data to the other...
ctorContextPropertiesAddnew CheckableContextProperty
An IContextProperty on its own doesn't provide you with a lot of functionality, as you can see in Listing 15-3. Listing 15-3. The IContextProperty Interface void Freeze Context newContext bool IsNewContextOK Context newCtx Freeze is called when the context itself is frozen. This indicates that no change of context properties is allowed afterwards. IsNewContextOk is called after all context attributes have added their context properties to allow your property to check for dependencies. If...
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...
IRemoteObject GetNewInstance IRemoteObject GetNewInstanceint 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...





