在 .net 远程处理中初始化服务器激活的对象
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没有一种简单而干净的方法来创建对象。当远程调用进入时,该对象就会被创建。
但我认为也没有必要这样做。您可以创建另一个处理所有初始化的类(它从任何需要的地方获取连接字符串等),然后您可以从远程处理对象中提取连接字符串。您可以在流程启动时自行创建该对象。
否则,您可以在对象的构造函数中放置一些逻辑。它也许可以从配置文件或您喜欢的任何文件中读取连接字符串。您还可以延迟加载该对象中声明的变量(即,如果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.
请记住,我不建议在 SingleCall SAO 中进行初始化工作,因为这项工作必须在每次调用 SAO 时完成。
为了管理连接字符串,我使用连接管理器类。
请考虑以下代码伪代码,因为我刚刚写出来是为了说明一个想法。我已将这种模式用于我编写的各种系统。
您的数据库代码将使用连接管理器类来检索连接字符串。
请记住,.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.
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!