Class EmployeeSite Implements ISite
' A site object associates a container with a component Private mTheContainer As IContainer Private mTheComponent As IComponent ' A site object uses a unique site name for each component in a ' container. Because we're in the EmployeeSite class, we'll use the ' employee's ID as the site name. Private mEmpID As String ' Constructor, to associate a container with a component Public Sub New ByVal TheContainer As IContainer, _ ByVal TheComponent As IComponent mTheContainer TheContainer...
Writing Your Own ThreadSafe Wrappers
The general idea of writing your own wrapper comes from the fact that you might not want to make every class in your library thread-safe, as synchronization has performance penalties associated with it. Instead, you can give the application developer a choice of whether to use a synchronized class or not. The application developer might not want to take the risk of a deadlock nor pay the performance penalty of using thread-safe classes in a single-threaded environment instead, he might prefer...
Implementing the Custom Invert Formatter
It's time to write our formatter class. A formatter class has to implement the class IRemotingFormatter in the System.Runtime.Remoting.Messaging namespace. Following are the properties and methods of the interface which inherits from IFormatter Binder Gets or sets the serialization binder. Context Gets or sets the streaming context used for serialization and deserialization. SurrogateSelector Gets or sets the surrogate selector used by the current formatter. Deserialize Gets invoked when an...
Running the Example Mfq
Let's see what happens on the server when we run it 1.0000000001 A 1000000000 1.10517092721414 3 A 3 27 Enter value 1.0000000001 Enter power 1000000000 Result is lt 0 gt Enter value 3 Enter power 3 Result is lt 0 gt Enter value Result is lt 1.10517092721414 gt Result is lt 27 gt You should note several things here. First, as soon as we enter the first calculation to perform at the client, the client asks what we want calculated next. So by using asynchronous remoting, we have accessed a remote...
Modifying the Host and Client Applications
We don't need to change any of the code for the ExchangeHost application, but we need to include a copy of ExchangeInterface.dll that we get when the solution is built with the host project. We do, however, have to modify the ExchangeClient code a little. It now uses only the IExchange interface rather than the Exchange class Imports System.Runtime.Remoting Imports ExchangeInterface Dim url As String Dim exchange As IExchange _ Activator.GetObject GetType IExchange , url Dim order As Order...
The Remoting Process
Now let's turn our attention to how the clients and servers talk to each other to facilitate method calls on remote objects. Any form of remoting needs a transport mechanism. Given its ubiquity, there's no other option but TCP IP as the base protocol. All you need to do now is define an application-level protocol to go on top. However, as you've seen, a number of conflicting demands must be satisfied when choosing such a protocol. A proprietary binary protocol such as DCOM is not...
Exploring the ThreadPool Class
This section explores the various aspects of the ThreadPool class and shows how they can be employed to create thread pools in your .NET applications. The ThreadPool class provides a pool of threads that can be used for the following Processing asynchronous I O calls Waiting on behalf of other threads The following table lists the methods of the ThreadPool class and their functionality. QueueUserWorkItem RegisterWaitForSingleObject This method binds the OS handle to the thread pool. This method...
The FieldInfo Class
This class provides access to field metadata. Tables 8-7 and 8-8 list some of its most important properties and methods remember that because FieldInfo inherits from MemberInfo, it inherits its members too . Table 8-7. FieldInfo Class Properties Table 8-7. FieldInfo Class Properties Gets a value indicating whether the field is private Gets a value indicating whether the field is public Gets a value indicating whether the field is static Returns a value indicating whether this field has Assembly...
The Code
Let's start analyzing the code of the DataImport example Imports System.IO Imports System.Threading Imports System.Data.SqlClient Imports Microsoft.VisualBasic Class DataImport First of all, we added all the necessary namespaces to use the FileSystemWatcher, Thread, and SQL Server classes ' Global Boolean switch used to activate deactivate Public Shared Sub Main ' Remove the default listener Trace.Listeners.RemoveAt 0 ' Create and add the new listener BS New BooleanSwitch DISwitch, _ DataImport...
Implementing the Invert Server Transport Sink
The transport sink on the server side is very boring. As you can see, it passes everything on to the next sink in the chain, so we don't need to spend any more time discussing it, especially as we've seen channel sinks already. Public Class InvertServerTransportSink Implements IServerChannelSink Dim _next As IServerChannelSink Public Sub New _ ByVal nextSink As IServerChannelSink _next nextSink End Sub Public Sub AsyncProcessResponse ByVal sinkStack _ As IServerResponseChannelSinkStack, _ ByVal...
Implementing a Tracking Handler
A tracking handler can be used to see when the remoting infrastructure marshals, unmarshals, and disconnects an object. You can add one to our ExchangeHost project by creating a new Tracker class, as in the following example, and then registering it Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Services Implements ITrackingHandler Public Sub MarshaledObject _ ByVal obj As Object, ByVal oref As ObjRef _ Implements ITrackingHandler.MarshaledObject Tracker Marshaled 0 ,...
Creating the Client
Before we start building the application, let's take a moment or two to see the UML view of the client form class that is going to contain all the code for the client application see Figure 19-2 . Figure 19-22 UML view of the client form class Figure 19-22 UML view of the client form class The StockClient application simply inherits from the System.Windows.Forms.Form class and contains all the code for the client application, such as the Private member variables and the methods. To create the...
Implementing the Abstract Factory Pattern with Dynamic Assembly Loading in VB
Let's examine a situation where you might apply these techniques. A manufacturing company's accounting department wants to create an EDI Electronic Data Interchange invoicing and billing system. EDI systems are electronic connections to send information between the two companies electronically. To keep material costs low, the purchasing department wants as little barrier to switching suppliers as possible. Unfortunately, different vendors all use different systems for communication email, FTP...
End Sub 1
Next, let's look at the event handler method for the Get Status button. This method reads the account number entered by the user, and calls GetAccountDetails on the remote Bank object. This method returns the requested Account object, and we display its details in a message box. You can probably imagine at least half a dozen things that can go wrong in this sequence of events we'll ignore these potential errors until later in the chapter. Private Sub btnGetStatus_Click ByVal sender As...
InvokeMember Property Value Modification Example
Many IDEs come with integrated property pages that allow you to set initial values and configure object properties at design time. These property pages are interesting because they accommodate any type of object or component and expose their internal properties to be viewed and modified. For example, while developing Windows applications, you probably fill your form with a number of controls. Each time you click on a different control, the property viewer refreshes its contents and exposes all...
InvokeMember Method Invocation Example
Near the start of the chapter, we discussed some very tempting features of dynamic method invocation and suggested its use in a number of situations and scenarios. Now let's see how these features can accent your applications to become more flexible, and easy to extend and implement. We're going to precede this section by modeling a real-world situation, where reflection could prove to be a solution. We'll use a variation on the user input-checking scenario we discussed at the start of the...
Retrieving Metadata from a Running Server
Soapsuds generates the metadata assembly in a two-stage process. The first stage is to obtain an XML-encoded description of the server object's methods, and the second stage is to generate the assembly from the XML description. The XML description can be encoded as either Web Services Description Language WSDL or Services Description Language SDL . WSDL is an increasingly used standard together with SOAP and has largely replaced the proprietary SDL WSDL is the default for Soapsuds. The WSDL...
Tracing Switches
When you're near to the application deployment phase, you'll probably want to remove all the tracing and debugging messages from the code. However, you don't have to look for every trace instruction and remove it. You can use compilation flags during the application building. From the Visual Studio .NET IDE, right-click on the project name within the Solution Explorer window, and select Properties from the context menu. The following dialog box appears. Now to strip all the Trace and Debug...
ObjectOrientation
As you know, however, .NET is not a procedural programming environment, and VB .NET is not a procedural programming language we have to think in terms of objects and methods, not procedures. Object-orientation makes binding more complicated, because instead of procedures, you have methods, and methods are defined in types usually classes . When you define a type and give it a method, that method can be called on any object that is an instance of that type. So, say you have the following type...
Testing the Remoting Application
Now that we've written the remote object, including the server and the client, we can investigate how errors occur in our remoting application. We'll explore six specific scenarios, and show screenshots to illustrate the error messages we get in each case. Scenario 1 Server Application Isn't Currently Running If the server application isn't currently running, the client application will receive an exception when it tries to invoke a remote method. This happens because the client application...
Creating the ExchangeObjects Class Library
Here are the contents of the ExchangeObjects class library, which contains the implementation of the remote MBR Exchange class and the MBV Order class. Recall that the Exchange class contains the NextOrder method that returns an Order object when called by the client. Imports System.Runtime.Remoting.Lifetime Public Class Exchange Inherits System.MarshalByRefObject Public Sub New Console.WriteLine Exchange Started mNextlndex 1000 End Sub Public Function NextOrder As Order mNextlndex mNextlndex 1...
Manipulating Property Values Using InvokeMember
InvokeMember goes beyond just dynamic method invocation. You can access and modify property values of an object by tweaking the binding flags passed to it during invocation. Because there isn't much difference between a call made to modify a property and a call made to invoke a method, let's hit some code straight away. The following code invokes two properties of an object. The first one is a Private Shared property and we try to get its value by using the Bindings.GetProperty flag Dim FooType...






