如下是博客一篇关于讲解remoting的简单实例 自己于是按照楼主说明一一操作还是出差错,最终找到原因
--------------------------------------------------------------------------------------------------------------
三、最简单的Remoting的例子
1、远程对象:
建立类库项目:RemoteObject
namespace RemoteObject
{
public class MyObject:MarshalByRefObject
{
public int Add( int a, int b)
{
return a + b;
}
}
}
2、服务端
建立控制台项目:RemoteServer
using System.Runtime.Remoting;
namespace RemoteServer
{
class MyServer
{
[STAThread]
static void Main( string [] args)
{
RemotingConfiguration.Configure( " RemoteServer.exe.config " );
Console.ReadLine();
}
}
}
建立配置文件:app.config
< system .runtime.remoting >
< application name ="RemoteServer" >
< service >
< wellknown type ="RemoteObject.MyObject,RemoteObject" objectUri ="RemoteObject.MyObject"
mode ="Singleton" />
</ service >
< channels >
< channel ref ="tcp" port ="9999" />
</ channels >
</ application >
</ system.runtime.remoting >
</ configuration >
3、客户端:
建立控制台项目:RemoteClient
namespace RemoteClient
{
class MyClient
{
[STAThread]
static void Main( string [] args)
{
RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject( typeof (RemoteObject.MyObject),System.Configuration.ConfigurationSettings.AppSettings[ " ServiceURL " ]);
Console.WriteLine(app.Add( 1 , 2 ));
Console.ReadLine();
}
}
}
建立配置文件:app.config
< appSettings >
< add key ="ServiceURL" value ="tcp://localhost:9999/RemoteObject.MyObject" />
</ appSettings >
</ configuration >
4、测试
在最后编译的时候会发现编译报错:
1、找不到app.Add()
2、找不到RemoteObject
这是因为客户端RemoteClient没有添加RemoteObject的引用,编译器并不知道远程对象存在哪些成员所以报错,添加引用以后vs.net会在客户端也保存一个dll,可能大家会问这样如果对远程对象的修改是不是会很麻烦?其实不麻烦,对项目编译一次vs.net会重新复制dll。
然后直接运行客户端会出现“目标主机拒绝”的异常,也说明了通道没有打开
运行服务端再运行客户端出现“找不到程序集RemoteObject”!回头想想可以发现我们并在服务端对RemoteObject添加引用,编译的时候通过是因为这个时候并没有用到远程对象,大家可能不理解运行服务端的时候也通过?这是因为没有这个时候还没有激活远程对象。理所当然,对服务端要添加引用远程对象,毕竟我们的对象是要靠远程承载的。
现在再先后运行服务端程序和客户端程序,客户端程序显示3,测试成功。
-------------------------------------------------------------------------------------------------------
上述为楼主内容 【http://www.cnblogs.com/lovecherry/archive/2005/05/19/158784.html】
自己所用操作系统 2003
操作环境 VS2005
操作如下:
1:启动服务端 ----成功
2:启动客户端-----失败【未能加载文件或程序集“RemoteObject”或它的某一个依赖项。系统找不到指定的文件。】
自己今天上午对remoting的原理和步骤是一点不懂,于是请来同事进行问题排查。
同事对上述服务器端代码由
--------------------------------------
using System;
using System.Runtime.Remoting;
namespace RemoteServer
{
class MyServer
{
[STAThread]
static void Main(string[] args)
{
RemotingConfiguration.Configure("RemoteServer.exe.config");
Console.ReadLine();
}
}
}
------------------------------------------------
替换为
--------------------------------------
using System;
using System.Runtime.Remoting;
namespace RemoteServer
{
class MyServer
{
[STAThread]
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel (9999);
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof (RemoteObject.MyObject), "RemoteObject.MyObject", WellKnownObjectMode.Singleton);
Console.ReadLine();
}
}
}
------------------------------------------------
即不再使用配置文件
编译能够通过。
下班后自己上网学习了下关于Remoting的知识。将上述的服务器端的配只文件由
--------------------------------------------------------------------------------------------------
< system .runtime.remoting >
< application name ="RemoteServer" >
< service >
< wellknown type ="RemoteObject.MyObject,RemoteObject" objectUri ="RemoteObject.MyObject"
mode ="Singleton" />
</ service >
< channels >
< channel ref ="tcp" port ="9999" />
</ channels >
</ application >
</ system.runtime.remoting >
</ configuration >
---------------------------------------------------------------------------------------------------------------------------
改写wellknown type="RemoteObject.MyObject,MyObject"
---------------------------------------------------------------------------------------------------------------------------
< system .runtime.remoting >
< application name ="RemoteServer" >
< service >
< wellknown type ="RemoteObject.MyObject,MyObject" objectUri ="RemoteObject.MyObject"
mode ="Singleton" />
</ service >
< channels >
< channel ref ="tcp" port ="9999" />
</ channels >
</ application >
</ system.runtime.remoting >
</ configuration >
----------------------------------------------------------------------