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.
![](https://i-blog.csdnimg.cn/blog_migrate/afc94224b809e29e9fdd82bd366e62ec.jpeg)
代码如下 :
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
![](https://i-blog.csdnimg.cn/blog_migrate/a0789431adfa3fcd87af2bbe2ad9a362.jpeg)
RemotingClient
![](https://i-blog.csdnimg.cn/blog_migrate/a1ace734e5c7befdcdf9cea6e7280f28.jpeg)
启动 RemotingSrvConsole
![](https://i-blog.csdnimg.cn/blog_migrate/22b36a299a24b7a4fa0465e60323d0d0.jpeg)
(RemotingSrvConsole)
![](https://i-blog.csdnimg.cn/blog_migrate/fbd794e9a6f2a9139b4a6fad0e7870db.jpeg)
(RemotingClient
此时 RemotingSrvConsole 已经有客户端
![](https://i-blog.csdnimg.cn/blog_migrate/5fabdf4fc22cdcbad6dfec0cb879b324.jpeg)
OK!