Remoing
例程
1. 创建
Remoting Server
端
(RemotingSrvConsole)
、
Client
端
(RemotingClient)
、以及公用函数库
(RemotingServerLib)
项目。
a)RemotingServerLib 项目
添加类RemoteObject 继承 MarshalByRefObject添加代码
public RemoteObject()
{
Console.WriteLine("Constructor called");
}
public string Hello(string name)
{
Console.WriteLine(name);
return "Hello " + name;
}
public DataSet GetDataSet(String SQLStr)
{
DataSet ReturnDs=new DataSet();
String ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;";
using (OleDbDataAdapter OleAp = new OleDbDataAdapter(SQLStr, ConnStr))
{
OleAp.Fill(ReturnDs);
Console.WriteLine("执行了SQL " + SQLStr);
return ReturnDs;
}
}
b)RemotingSrvConsole 项目
i. 添加
Northwind.mdb
数据库设置并复制到输入目录
.
ii. 添加
RemotingConfiguration.Configure
配置文件内容为
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteClassLibServer">
<service>
<wellknown mode="SingleCall" type="RemotingServerLib.RemoteObject,RemotingServerLib"
objectUri="RemoteObject">
</wellknown>
</service>
<channels>
<channel ref="tcp server" port="9000"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
iii. 添加对
RemotingSrvConsole
项目的引用
c)RemotingClient 项目
i. 添加对
.net System.Runtime.Remoting
的引用
,
以及
RemotingServerLib
项目引用
.
合适的地方初始化
Remoting
通讯连接,我的窗体为
代码如下 :
public partial class RemotingClientFrm : Form
{
public RemotingClientFrm()
{
InitializeComponent();
this.InitCommReote();
}
RemoteObject loader;
void InitCommReote()
{
ChannelServices.RegisterChannel(new TcpClientChannel(),true);
loader = (RemoteObject)Activator.GetObject(
typeof(RemoteObject), "tcp://localhost:9000/RemoteObject");
loader.Hello("Test Action");
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = loader.GetDataSet("Select * From Customers").Tables[0];
}
}
2. 新建两个解决方案
SoluRemotingServer
以及
SoluRemotingClient
其中 SoluRemotingServer 添加项目
RemotingSrvConsole
、以及
RemotingServerLib
其中 SoluRemotingServer 添加项目
RemotingClient
、以及
RemotingServerLib
(这个是为了方便调试)其中两解决方案中 RemotingServerLib 物理位置一致
如下图所示
RemotingSrvConsole 解决方案
RemotingClient 解决方案
启动 RemotingSrvConsole 再启动
RemotingClient
(RemotingSrvConsole)
(RemotingClient 点击了
button1)
此时 RemotingSrvConsole 已经有客户端 显示
OK! 我们的简单的
Remoting
构建成功了!
1.
a)RemotingServerLib
添加类RemoteObject 继承 MarshalByRefObject添加代码
public RemoteObject()
{
Console.WriteLine("Constructor called");
}
public string Hello(string name)
{
Console.WriteLine(name);
return "Hello " + name;
}
public DataSet GetDataSet(String SQLStr)
{
DataSet ReturnDs=new DataSet();
String ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;";
using (OleDbDataAdapter OleAp = new OleDbDataAdapter(SQLStr, ConnStr))
{
OleAp.Fill(ReturnDs);
Console.WriteLine("执行了SQL " + SQLStr);
return ReturnDs;
}
}
b)RemotingSrvConsole
i.
ii.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteClassLibServer">
<service>
<wellknown mode="SingleCall" type="RemotingServerLib.RemoteObject,RemotingServerLib"
objectUri="RemoteObject">
</wellknown>
</service>
<channels>
<channel ref="tcp server" port="9000"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
iii.
c)RemotingClient
i.
代码如下 :
public partial class RemotingClientFrm : Form
{
public RemotingClientFrm()
{
InitializeComponent();
this.InitCommReote();
}
RemoteObject loader;
void InitCommReote()
{
ChannelServices.RegisterChannel(new TcpClientChannel(),true);
loader = (RemoteObject)Activator.GetObject(
typeof(RemoteObject), "tcp://localhost:9000/RemoteObject");
loader.Hello("Test Action");
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = loader.GetDataSet("Select * From Customers").Tables[0];
}
}
2.
其中 SoluRemotingServer
其中 SoluRemotingServer
(这个是为了方便调试)其中两解决方案中 RemotingServerLib
如下图所示
RemotingSrvConsole
RemotingClient
启动 RemotingSrvConsole
(RemotingSrvConsole)
(RemotingClient
此时 RemotingSrvConsole 已经有客户端
OK!
本文介绍了一种基于.NET平台的简单Remoting应用构建过程,包括服务端与客户端项目的搭建、配置及实现远程调用的方法。通过示例代码展示了如何进行远程对象的创建、配置与调用,以及如何从远程服务器获取数据集。
4185





