简单的Remoting实例问题查找

如下是博客一篇关于讲解remoting的简单实例 自己于是按照楼主说明一一操作还是出差错,最终找到原因

--------------------------------------------------------------------------------------------------------------

三、最简单的Remoting的例子
1、远程对象:
建立类库项目:RemoteObject

using  System;

namespace  RemoteObject
{
    
public   class  MyObject:MarshalByRefObject
    {
        
public   int  Add( int  a, int  b)
        {
            
return  a + b;
        }
    }
}

2、服务端
建立控制台项目:RemoteServer

using  System;
using  System.Runtime.Remoting;

namespace  RemoteServer
{
    
class  MyServer
    {
        [STAThread]
        
static   void  Main( string [] args)
        {
            RemotingConfiguration.Configure(
" RemoteServer.exe.config " );
            Console.ReadLine();
        }
    }
}


建立配置文件:app.config

< configuration >
    
< 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

using  System;

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

 

< configuration >
 
< 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的知识。将上述的服务器端的配只文件由

--------------------------------------------------------------------------------------------------

< configuration >
    
< 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"

---------------------------------------------------------------------------------------------------------------------------

< configuration >
    
< 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 >
----------------------------------------------------------------------
同样也能编译通过。
问题根源找到了,需要分析原因了。
原来
type属性分两部分<命名空间.类名>,<程序集>
自己定义的远程对象RemoteObject的<命名空间.类名>,<程序集>如下图
当然会出错了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值