Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了。到底选择那种,当然还有访问效率上的考虑,同时在Remoting中又有三中信道Http,Tcp,Ipc,它们又各有差别。HTTP方式的信道在跨越防火墙上有优势;TCP方式的信道常用在局域网内通信,速度比HTTP快很多;IPC信道用于同一台机器的进程间通信,通信不占用网络资源,速度又比TCP快很多。为了能够实际的比较一下这四者的实际访问速度,我写了个小程序用测试。这个程序的实现很简单利用Remoting三种信道和Web Service 访问同一个对象(相当于实际项目中的业务层),而这个对象实现返回系统的时间。就这么简单。如果有对Remoting和Web Service不太了解的,也可以通过我这个例子熟悉一下Remoting三种信道的写法差别和Web Service的调用。
下面是程序运行的界面,我使用.net中的最小时间度量:刻度(用毫秒在本机上可能都很难测出它们之间的差别),来测试每次调用所发的时间,并通过多次调用来测的一个平均时间来比较访问的速度。通过测试可以看得出他们四者得访问速度:ipc>tcp>http>Web Service.(其实Remoting的http信道和Web Service的访问速度还有待比较,跟测试的主机还有一定关系,在我办公室里的一台电脑上好像Web service的访问速度更快于http信道),大家可以自己测试一下,或研究一个比较好的方法。
![TestRemotingWebService.JPG](https://i-blog.csdnimg.cn/blog_migrate/8245bf7cc9a46a31eb6dfe8862a1d6ea.jpeg)
使用三种信道调用远程对象和访问Web Service
//使用Http信道
public void Http()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "http://localhost:9001/MyObject");
myObj.GetServerTime();
stopWatch.Stop();
lsbHttp.Items.Add(stopWatch.ElapsedTicks);
}
//使用Tcp信道
public void Tcp()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "tcp://localhost:9002/MyObject");
myObj.GetServerTime();
stopWatch.Stop();
lsbTcp.Items.Add(stopWatch.ElapsedTicks);
}
//使用Ipc信道
public void Ipc()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "Ipc://MyHost/MyObject");
myObj.GetServerTime();
stopWatch.Stop();
lsbIpc.Items.Add(stopWatch.ElapsedTicks);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](/Images/OutliningIndicators/None.gif)
//访问Web Service
public void WebService()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
localhost.Service ws = new localhost.Service();
ws.GetServerTime();
stopWatch.Stop();
lsbWeb.Items.Add(stopWatch.ElapsedTicks);
}
private void btnHttp_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Http();
}
![None.gif](/Images/OutliningIndicators/None.gif)
private void btnTcp_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Tcp();
}
![None.gif](/Images/OutliningIndicators/None.gif)
private void btnWebService_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
WebService();
}
![None.gif](/Images/OutliningIndicators/None.gif)
private void btnIpc_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Ipc();
}
![None.gif](/Images/OutliningIndicators/None.gif)
//开始测试
private void btnStat_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Int32 Times = int.Parse(txtTimes.Text);
Int64 Sum = 0;
double Ave=0;
lsbHttp.Items.Clear();
lsbIpc.Items.Clear();
lsbTcp.Items.Clear();
lsbWeb.Items.Clear();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
for (Int32 i = 0; i < Times; i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Http();
Tcp();
Ipc();
WebService();
}
//计算平均时间
for(Int32 i=0;i<Times;i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Sum += int.Parse(lsbHttp.Items[i].ToString ());
}
Ave = Sum / Times;
txtHttp.Text = Ave.ToString();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Sum = 0;
for (Int32 i = 0; i < Times; i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Sum += int.Parse(lsbTcp.Items[i].ToString());
}
Ave = Sum / Times;
txtTcp.Text = Ave.ToString();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Sum = 0;
for (Int32 i = 0; i < Times; i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Sum += int.Parse(lsbWeb.Items[i].ToString());
}
Ave = Sum / Times;
txtWebService.Text = Ave.ToString();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Sum = 0;
for (Int32 i = 0; i < Times; i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Sum += int.Parse(lsbIpc.Items[i].ToString());
}
Ave = Sum / Times;
txtIpc.Text = Ave.ToString();
}
HttpChannel httpChannel
=
new
HttpChannel(
9001
);
ChannelServices.RegisterChannel(httpChannel,
false
);
![None.gif](/Images/OutliningIndicators/None.gif)
TcpChannel tcpChannel
=
new
TcpChannel(
9002
);
ChannelServices.RegisterChannel(tcpChannel,
false
);
![None.gif](/Images/OutliningIndicators/None.gif)
IpcChannel ipcChannel
=
new
IpcChannel(
"
MyHost
"
);
ChannelServices.RegisterChannel(ipcChannel,
false
);
![None.gif](/Images/OutliningIndicators/None.gif)
RemotingConfiguration .RegisterWellKnownServiceType (
typeof
(RemoteObject .MyObject ),
"
MyObject
"
,WellKnownObjectMode.SingleCall);
Console.ReadLine();
/Files/zhang3533/RemotingAndWebService.rar