摘要:出处:http://rickie.cnblogs.com/archive/2004/11/19/65706.html
如何使用基于接口的Remote Objects的配置文件
在开发.Net Remoting Components时,可以通过使用Remoting Configuration file来简化代码并提高配置的灵活性,在article《.Net Remoting配置文件的用法》中有比较详细的介绍。
但是,如果Client端没有Remote Objects的具体实现,只有引用interface,则Client端的configuration file就不行了(Server端应该不存在这种情况)。当然,你可以使用如下的代码得到Remote Object:
IHome objHome = (YourNameSpace.BusinessFacade.IHome)Activator.GetObject(typeof(YourNameSpace.BusinessFacade.IHome), “http://<ip address>/YourApplicationName/remotingObject.soap”);
注:这里IHome假设是由Server和Client端共享的interface定义。
这样将Remote URL编码在代码里面,显然就没有使用Configuration文件配置灵活了。
针对上述问题,由如下几种解决办法:
1. 通过在Client的配置文件<appSettings>增加entry
如:<add key="Home.Location" value="http://<ip address>/ YourApplicationName/remotingObject.soap" />
然后,通过ConfigurationSettings.AppSettings["Home.Location"];来获取Remote URL。
2. Ingo’s RemotingHelper class
Ingo是《Advanced .Net Remoting》一书的作者,RemotingHelper class在Reference 1的URL可以查到。
通过使用RemotingHelper class,你可以像使用正常的Remoting Configuration配置文件一样来配置Remote URL。
下面看看代码实现部分:
(1) RemotingHelper class
using System;
using System.Collections;
using System.Runtime.Remoting;
// 根据传入的type类型,从Hashtable中获取WellKnownClientTypeEntry,然后通过调用Activator.GetObject()方法,得到需要的Remote Object。
class RemotingHelper {
private static bool _isInit;
private static IDictionary _wellKnownTypes;
public static Object GetObject(Type type) {
if (! _isInit) InitTypeCache();
WellKnownClientTypeEntry entr = (WellKnownClientTypeEntry) _wellKnownTypes[type];
if (entr == null) {
throw new RemotingException("Type not found!");
}
return Activator.GetObject(entr.ObjectType,entr.ObjectUrl);
}
// 负责初始化Hashtable, 并根据Remoting Configuration配置文件填入Remote Object Type和WellKnownClientTypeEntry。
public static void InitTypeCache() {
_isInit = true;
_wellKnownTypes=......[
阅读全文]