WebReference.com - Part 3 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (5/5)
[previous] |
Professional C# Web Services
Tracking Services
A great feature for analyzing a running application is Tracking Services. This service can be
used to observe the marshaling process of MBR objects. Indeed it can be very simply activated:
we have only to create a tracking handler and register this handler with the utility class
TrackingServices.
Tracking Handler
A tracking handler implements the interface ITrackingHandler. This interface defines
three methods that are called by the remoting infrastructure when marshaling and unmarshaling occurs:
| ITrackingHandler methods | Description |
MarshaledObject | This method is called when an MBR object is marshaled |
UnmarshaledObject | This method is called when an MBR object is unmarshaled |
DisconnectedObject | DisconnectedObject() is called when an MBR object is disconnected, for example when the lease time of an client-activated object expires |
The class MyTrackingHandler is implemented in the assembly MyTrackingHandler
that is available both for the client and the server to make this tracking available both for
clients and servers. This class implements the interface ITrackingHandler, so we have to
implement the methods MarshaledObject(), UnmarshaledObject(), and
DisconnectedObject(). We use the URI property of the ObjRef that
is passed to output the path to the object; in addition, we output the type of the marshaled object.
With the first argument, it would also be possible to access the properties of the object to marshal:
// MyTrackingHandler.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
namespace Wrox.Samples
{
public class MyTrackingHandler: ITrackingHandler
{
public void MarshaledObject(object obj, ObjRef or)
{
Console.WriteLine();
Console.WriteLine("The object " + or.URI + " is marshaled");
Console.WriteLine("Type: " + or.TypeInfo.TypeName);
}
public void UnmarshaledObject(object obj, ObjRef or)
{
Console.WriteLine();
Console.WriteLine("The object " + or.URI + " is unmarshaled");
Console.WriteLine("Type: " + or.TypeInfo.TypeName);
}
public void DisconnectedObject(object obj)
{
Console.WriteLine(obj.ToString() + " is disconnected");
}
}
}
Register Tracking Handler
Both in the server and in the client application, in the files SimpleClient.cs and
SimpleServer.cs, we can create a new instance of the MyTrackingHandler
class and register it with the tracking service:
static void Main(string[] args)
{
RemotingConfiguration.Configure("SimpleServer.exe.config");
TrackingServices.RegisterTrackingHandler(new MyTrackingHandler());
Running the Program
We run the application with a client that will:
- Create the remote object
MyRemoteObject - Call the method
obj.GetB()to return the MBR object of classBto the client - Call the method
obj.SetB()to pass the MBR object of classBto the server
We can see the output in the following screenshots. The first picture shows the server output where
MyRemoteObject and B are marshaled, and with the call to SetB(),
B is unmarshaled. The last line of the output shows that the lease time of the remote
object expired, so it was disconnected:

In the client output MyRemoteObject and B are unmarshaled before the
call to SetB(), where B is marshaled:

[previous] |
Created: March 26, 2002
Revised: March 26, 2002
URL: http://webreference.com/programming/csharp/webservices/chap6/3/5.html

Find a programming school near you