.net Remoting简单实例

 

一、实用类:

1、System.MarshalByRefObject :
    
系统中远程调用的对象必须是从MarshalByRefObject对象中派生出来的;

2System.Runtime.Remoting.Channels.Tcp.TcpServerChannel :
     服务器端的Tcp信道;

3System.Runtime.Remoting.Channels.Http.HttpServerChannel :
     服务器端的Http信道;

4System.Runtime.Remoting.Channels.ChannelServices :
     注册信道,使之可用于远程对象;

5System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnowServiceType :
     指定远程对象中类的类的类型,客户机使用的URI和模式;
6System.Runtime.Remoting.Channels.Tcp.TcpClientChannel :
     客户端的Tcp信道;

7System.Runtime.Remoting.Channels.Http.HttpClientChannel :
     客户端的Http信道。


二、简单的示例

1、创建远程对象,在这里创建一个dll程序集,这个dll在服务器和客户机代码中都会用到。
     创建名为RemoteHello.dll的程序集

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  RemoteHello
{
    
public class Hello:System.MarshalByRefObject
    
{
        
public Hello()
        
{
            Console.WriteLine(
"Constructor called");
        }


        
~Hello()
        
{
            Console.WriteLine(
"Destructor called");
        }


        
public string HelloWorld(string name)
        
{
            Console.WriteLine(
"Hello World!");
            
return "Hi," + name;
        }

    
    }

}

 

2、创建服务器。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集
     在此创建名为HelloServer的Console Application

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.Remoting;
using  System.Runtime.Remoting.Channels;
using  System.Runtime.Remoting.Channels.Tcp;
using  RemoteHello;
namespace  HelloService
{
    
class HelloServer
    
{
        
static void Main(string[] args)
        
{
            TcpServerChannel channel 
= new TcpServerChannel(6666);
            
//ChannelServices.RegisterChannel(channel);
            ChannelServices.RegisterChannel(channel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
            System.Console.WriteLine(
"Press Any Key to Exit ! ");
            System.Console.ReadLine();
        }

    }

}

上面代码中可以用

ChannelServices.RegisterChannel(channel);
来替换
ChannelServices.RegisterChannel(channel, false );
但是在 .NET Framework 2.0中编译时会提示
' System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(System.Runtime.Remoting.Channels.IChannel) '   is  obsolete:  ' Use System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl, bool ensureSecurity) instead. '

原因是.NET Framework 2.0新增的函数

System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl,  bool  ensureSecurity)

 

其中参数:ensureSecurity

如果启用了安全,则为 true;否则为 false。将该值设置为 false 将不会使在 TCP IPC 信道上所做的安全设置无效。

此外 MSDN 中注释:对于 TcpServerChannel,将 esureSecurity 设置为 true 将在 Win98 上引发异常(因为 Wi9x 上不支持安全 tcp 信道);对于 Http 服务器信道,这样会在所有平台上引发异常(如果想要安全的 http 信道,用户需要在 IIS 中承载服务)。

3、创建客户机。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集
     在此创建名为HelloClient的Console Application

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.Remoting.Channels;
using  System.Runtime.Remoting.Channels.Tcp;
using  RemoteHello;

namespace  HelloClient
{
    
class HelloClient
    
{
        
static void Main(string[] args)
        
{
            
            ChannelServices.RegisterChannel(
new TcpClientChannel(), false);
            Hello obj 
= (Hello)Activator.GetObject(typeof(Hello), "tcp://zjx:6666/HelloWorld");
            
if (obj == null)
            
{
                Console.WriteLine(
"False to Link Server.");
                
return;
            }

            
for (int i = 0; i < 6; i++)
            
{
                Console.WriteLine(obj.HelloWorld(
"MadRam.neo"));
            }

        }

    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值