码迷,mamicode.com
首页 > Web开发 > 详细

NET Remoting 示例

时间:2014-12-18 18:32:17      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   使用   sp   

 

 

    .NET Remoting是.NET平台上允许存在于不同应用程序域中的对象相互知晓对方并进行通讯的基础设施。调用对象被称为客户端,而被调用对象则被称为服务器或者服务器对象。简而言之,它就是.NET平台上实现分布式对象系统的框架。只要是跨越AppDomain的访问,都属于Remoting。Remoting编程基本原理:当客户端创建远程RemotableClass的一个实例,.NET框架在客户端应用程序域中产生一个代理。该代理看起来就像实际对象。代理收到调用后,通过通道连接到远程的对象。

Remoting通过通道(channel)来传输消息。Remoting的通道主要有两种:Tcp和Http。在.Net中,System.Runtime.Remoting.Channel中定义了IChannel接口。IChannel接口包括了TcpChannel通道类型和Http通道类型。它们分别对应Remoting通道的这两种类型。

TcpChannel类型放在名字空间System.Runtime.Remoting.Channel.Tcp中。Tcp通道提供了基于Socket的传输工具,使用Tcp协议来跨越Remoting边界传输序列化的消息流。TcpChannel类型默认使用二进制格式序列化消息对象,因此它具有更高的传输性能。HttpChannel类型放在名字空间System.Runtime.Remoting.Channel.Http中。它提供了一种使用Http协议,使其能在Internet上穿越防火墙传输序列化消息流。默认情况下,HttpChannel类型使用Soap格式序列化消息对象,因此它具有更好的互操作性。通常在局域网内,我们更多地使用TcpChannel;如果要穿越防火墙,则使用HttpChannel。

 

 

bubuko.com,布布扣

 

 

 

 

我自己写了一个示例,工程结构如下:

bubuko.com,布布扣

 

1,创建一个类库,用来创建我们需要的接口

 

namespace RemotingInterface
{
    public interface MyInterface 
    {
        string Greeting(string sName);
    }
}

 

 

2,创建接口的实现,需要添加上面工程的引用

namespace RemotingImp
{
    public class MyClass : MarshalByRefObject, MyInterface
    {

        #region MyInterface 成员

        public string Greeting(string sName)
        {
            return "Hello!" + sName;
        }

        #endregion
    }
}

3, 服务器端实现,需要添加上面两个的引用

namespace RemotingSever
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingImp.MyClass), "RemotingPersonService", WellKnownObjectMode.SingleCall);

            System.Console.WriteLine("Server:Press Enter key to exit");
            System.Console.ReadLine();
    


        }
    }
}

 

4,客户端,需要添加接口所在的类库

namespace RemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {

            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);
            MyInterface obj = (MyInterface)Activator.GetObject(typeof(RemotingInterface.MyInterface), "tcp://localhost:8080/RemotingPersonService");
            if (obj == null)
            {
                Console.WriteLine("Couldn‘t crate Remoting Object .");
            }

            Console.WriteLine("Please enter your name:");
            String name = Console.ReadLine();
            try
            {
                Console.WriteLine(obj.Greeting(name));
            }
            catch (System.Net.Sockets.SocketException e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }

    }
}

 

NET Remoting 示例

标签:style   blog   http   ar   io   color   os   使用   sp   

原文地址:http://www.cnblogs.com/zuiyirenjian/p/4172214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!