Mediar.framework--使用反射来声明Remoting wellknown服务
使用Remoting时,每有一个服务,就要声明一次。虽然Remoting 支持配置文件方式声明,但是写起来依然很麻烦。象我这样的懒人最不想做的就是这种重复的机械劳动。于是寻找出一种简洁的方式——使用反射。反射只有在启动时执行,所以也不会影响效率。
static TcpChannel chan;
public static void RegisterServer(int port)
{
chan = new TcpChannel(port);
RemotingConfiguration.ApplicationName = "DataAccess";
ChannelServices.RegisterChannel(chan, false /*ensureSecurity*/);
foreach (string var in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
{
if (var.EndsWith(".dll"))
{
RegisterServer(var);
}
}
}
private static void RegisterServer(string path)
{
Assembly Ass;
Ass = Assembly.LoadFrom(path);
Type[] t = Ass.GetTypes();
foreach (Type tt in t)
{
if (typeof(DataAccess).IsAssignableFrom(tt))
{
Console.WriteLine(" Register Class: " + tt.FullName);
RemotingConfiguration.RegisterWellKnownServiceType(tt, tt.FullName, WellKnownObjectMode.SingleCall);
}
}
}
public static void RegisterServer(int port)
{
chan = new TcpChannel(port);
RemotingConfiguration.ApplicationName = "DataAccess";
ChannelServices.RegisterChannel(chan, false /*ensureSecurity*/);
foreach (string var in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
{
if (var.EndsWith(".dll"))
{
RegisterServer(var);
}
}
}
private static void RegisterServer(string path)
{
Assembly Ass;
Ass = Assembly.LoadFrom(path);
Type[] t = Ass.GetTypes();
foreach (Type tt in t)
{
if (typeof(DataAccess).IsAssignableFrom(tt))
{
Console.WriteLine(" Register Class: " + tt.FullName);
RemotingConfiguration.RegisterWellKnownServiceType(tt, tt.FullName, WellKnownObjectMode.SingleCall);
}
}
}
这里的DataAccess 类是继承MarshalByRefObject,在程序里是基类,其它的业务类,可以必需继承DataAccess。
客户端
remoteURL = "tcp://" + mRemoteServer + ":" + mRemotePort + "/" + mDataAccessTypeName;
da = Activator.GetObject(t, remoteURL);
DataAccess mDataAccess = ((DataAccess)(da));
相关:
Mediar.Framework --一个MVC架构
Mediar.Framework --对象mapping
Mediar.Framework--使用反射来声明Remoting wellknown服务
Mediar.Framework -- 业务的实现1(UI的绑定)