remoting 常用类

一、实用类:

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

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

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

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

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

7、System.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"));
            }

        }

    }

}
posted @ 2008-12-05 16:25  Winner.Net(2007)  阅读(458)  评论(0编辑  收藏  举报