简体   繁体   English

在.net远程处理中初始化服务器激活的对象

[英]Initialising server activated objects in .net remoting

I am using single-call server activated objects in .net remoting, and have a question about how to initialise the remoted objects. 我在.net远程处理中使用了单呼叫服务器激活的对象,并且对如何初始化远程对象有疑问。

When my server calls RemotingConfiguration.RegisterWellKnownServiceType() it passes a reference to the Type of the object to be instantiated to service client requests. 当我的服务器调用RemotingConfiguration.RegisterWellKnownServiceType()时,它将对要实例化的对象的类型的引用传递给服务客户端请求。 How do I initialise these 'remote objects' after thay have been created and before the client uses them? 在创建Thay之后以及客户端使用它们之前,如何初始化这些“远程对象”?

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.) (目前,我的解决方法是将连接字符串存储在一个静态字段中,因为所有远程对象当前都使用同一数据库。但是,这不是很灵活,对我来说似乎是一种黑客。)

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 (ie if connectionString = null, then GetConnectionString();) There are plenty of options really. 您也可以延迟加载该对象中标出的变量(即,如果connectionString = null,则GetConnectionString();)。确实有很多选择。

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. 请记住,我不建议在SingleCall SAO中进行初始化工作,因为必须在每次对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 :-) 请记住,.NET远程处理已被WCF取代,但是我们中的某些人仍然在旧版代码中进行了远程处理:-)

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM