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

发布于 2024-08-03 00:49:55 字数 361 浏览 3 评论 0原文

我在 .net 远程处理中使用单调用服务器激活的对象,并且有一个关于如何初始化远程对象的问题。

当我的服务器调用 RemotingConfiguration.RegisterWellKnownServiceType() 时,它会传递对要实例化的对象类型的引用以服务客户端请求。在创建这些“远程对象”之后、客户端使用它们之前,如何初始化它们?

就我而言,远程对象需要连接到数据库,因此它们需要一个连接字符串。当包含服务器不知道它们何时创建并且远程对象没有对包含服务器的引用时,它们应该如何获取此信息?我在这里缺少什么?

(目前我的解决方法是将连接字符串存储在静态字段中,因为所有远程对象当前都使用相同的数据库。但这不是很灵活,对我来说看起来像是一种黑客攻击。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-08-10 00:49:55

我认为没有一种简单而干净的方法来创建对象。当远程调用进入时,该对象就会被创建。

但我认为也没有必要这样做。您可以创建另一个处理所有初始化的类(它从任何需要的地方获取连接字符串等),然后您可以从远程处理对象中提取连接字符串。您可以在流程启动时自行创建该对象。

否则,您可以在对象的构造函数中放置一些逻辑。它也许可以从配置文件或您喜欢的任何文件中读取连接字符串。您还可以延迟加载该对象中声明的变量(即,如果connectionString = null,则GetConnectionString();)确实有很多选择。

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.

指尖凝香 2024-08-10 00:49:55

请记住,我不建议在 SingleCall SAO 中进行初始化工作,因为这项工作必须在每次调用 SAO 时完成。

为了管理连接字符串,我使用连接管理器类。

请考虑以下代码伪代码,因为我刚刚写出来是为了说明一个想法。我已将这种模式用于我编写的各种系统。


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];
   }
}

您的数据库代码将使用连接管理器类来检索连接字符串。

请记住,.NET 远程处理已被 WCF 取代,但我们中的一些人在遗留代码中仍然有远程处理:-)

我希望这会有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文