tags:

views:

292

answers:

2

I am using single-call server activated objects in .net remoting, and have a question about how to initialise the remoted objects.

When my server calls RemotingConfiguration.RegisterWellKnownServiceType() it passes a reference to the Type of the object to be instantiated to service client requests. How do I initialise these 'remote objects' after thay have been created and before the client uses them?

In my case, the remote objects need to connect to a database, so they need a connection string. How should they acquire this when the containing server doesn't known when they are created, and the remote object has no references to the containing server? What am I missing here?

(My workaround for the moment is to store the connection string in a static field, since all remote objects currently use the same database. This isn't very flexible though, and looks like a hack to me.)

A: 

I don't think there is an easy and clean way to just create the object. The object gets created when a remoting call comes in.

I don't think there is a need for that either though. You can create another class that handles all the initialization (it gets the connection string from wherever it wants, etc.) and then you can pull the connection string from the remoting object. You would create that object(s) on process start yourself.

Otherwise, you can put some logic in the constructor of the object. It can maybe read the connection string from a config file or whatever you like. You can also lazy load the variables decalred in that object (i.e. if connectionString = null, then GetConnectionString();) There are plenty of options really.

Mark
A: 

Please keep in mind I would not suggest doing initialization work in a SingleCall SAO as this work has to be done on every call to the SAO.

To manage connection strings I use a connection manager class.

Please consider the following code pseudo code as I just wrote it out to illustrate an idea. I've used this pattern for various systems I've written.


public enum DBConnection
{
   DBTest1,
   DBTest2
}

public static class ConnectionStringManager
{
   static Exception _construtorException = null;
   static Dictionary _connectionMap = new Dictionary();

   static ConnectionStringManager()
   {
      try
      {
         // Load the _connectionMap
         // You can use a custom application configuration section or
         // connection strings defined as described in the following article
         // http://msdn.microsoft.com/en-us/library/bf7sd233.aspx
      }
      catch(Exception ex)
      {
         // Any exception thrown in a static constructor needs to be kept track of because static
         // constructors are called during type initialization and exceptions thrown
         // are not caught by normal application exception handling. 
         // (At least as of .NET 2.0)

         _constructorException = ex;
      }
   }

   public static string GetConnectionString(DBConnection conn)
   {
      if ( _constructorEx != null )
         throw new Exception("Error initializing ConnectionStringManager", _constructorException);

      if ( !_connectionMap.ContainsKey(conn) )
         throw new Exception(string.Format("Connection {0} not found", Enum.GetName(typeof(DBconnection), (int)conn));

      return _connectionMap[conn];
   }
}

You database code will use the connection manager class to retrieve connection strings.

Keep in mind that .NET remoting has been replaced by WCF, but some of us still have remoting in our legacy code :-)

I hope this helps!

Nathan