码迷,mamicode.com
首页 > 其他好文 > 详细

Remoting 简介入门例子

时间:2016-05-12 17:28:59      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

  1. 简介

    .NET Remoting是微软随.NET推出的一种分布式应用解决方案,被誉为管理应用程序域之间的 RPC 的首选技,它允许不同应用程序域之间进行通信(这里的通信可以是在同一个进程中进行、一个系统的不同进程间进行、不同系统的进程间进行)。

      更具体的说,Microsoft .NET Remoting 提供了一种允许对象通过应用程序域与另一对象进行交互的框架。也就是说,使用.NET Remoting,一个程序域可以访问另外一个程序域中的对象,就好像这个对象位于自身内部,只不过,对这个远程对象的调用,其代码是在远程应用程序域中进行的,例如在本地应用程序域中调用远程对象上一个会弹出对话框的方法,那么,这个对话框,则会在远程应用程序域中弹出。

.NET Remoting框架提供了多种服务,包括激活和生存期支持,以及负责与远程应用程序进行消息传输的通讯通道。格式化程序用于在消息通过通道传输之前,对其进行编码和解码。应用程序可以在注重性能的场合使用二进制编码,在需要与其他远程处理框架进行交互的场合使用 XML 编码。在从一个应用程序域向另一个应用程序域传输消息时,所有的 XML 编码都使用 SOAP 协议。出于安全性方面的考虑,远程处理提供了大量挂钩,使得在消息流通过通道进行传输之前,安全接收器能够访问消息和序列化流。

在Remoting中是通过通道(channel)来实现两个应用程序域之间对象的通信的。如图所示:

Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RemotingObjects
{
public interface IPerson
{
String getName(String name);

}

public class Person : MarshalByRefObject, IPerson
{
    public Person()
    {
        Console.WriteLine("[Person]:Remoting Object ‘Person‘ is activated.");
    }

    public String getName(String name)
    {
        return name;
    }
}

}

Step2:创建控制台工程RemotingServer(添加项目引用RemotingObjects),类Server代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace RemotingServer
{
class Server
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Person), “RemotingPersonService”, WellKnownObjectMode.SingleCall);

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

}
Step3:创建控制台工程RemotingClient(添加项目引用RemotingObjects及必要类库),类Client代码如下:
using RemotingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace RemotingClient
{
class Client
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel, false);
IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), “tcp://localhost:8080/RemotingPersonService”);
if (obj == null)
{
Console.WriteLine(“Couldn’t crate Remoting Object ‘Person’.”);
}

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

}

Remoting 简介入门例子

标签:

原文地址:http://blog.csdn.net/xueliang19/article/details/51361233

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