求.NET Remoteing工作方式

goldenwing 2004-03-30 01:45:49
请各位高手讨论一下.NET Remoteting工作方式
...全文
182 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
turnmissile 2004-03-30
  • 打赏
  • 举报
回复
简单的理解就是
发送方:先serialize,然后用channel(tcp,http等方式)传送byte流。
接受方:先接受byte流,然后用相同方法的serialize对这个数据流进行deserialize。

所以remoting 需要你设定serialize的模式(soap或者binary),以及channel(http, tcp)
当然你也可以使用默认的方式。
sunruping 2004-03-30
  • 打赏
  • 举报
回复
呵呵。
就是DCOM 得升级版本
superryu 2004-03-30
  • 打赏
  • 举报
回复
学习
Montaque 2004-03-30
  • 打赏
  • 举报
回复
呵呵。
就是DCOM 得升级版本
gOODiDEA 2004-03-30
  • 打赏
  • 举报
回复
.NET Remoting 体系结构评估


http://www.microsoft.com/china/msdn/library/dndotnet/html/dotnetremotearch.asp

liyoukebit 2004-03-30
  • 打赏
  • 举报
回复
.NET 远程处理框架为开发人员提供先进的分布式对象模型,该模型允许在网络中的不同公共语言运行库之间或者在同一公共语言运行库中的不同 AppDomain 之间进行远程方法调用。与远程对象的所有交互都通过代理发生,而且客户端无法直接访问远程对象,因为该对象仅在其自己的 AppDomain 内才有意义。
CS\server.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace RemotingSamples {
public class Sample {

public static int Main(string [] args) {

TcpChannel chan1 = new TcpChannel(8085);
HttpChannel chan2 = new HttpChannel(8086);
ChannelServices.RegisterChannel(chan1);
ChannelServices.RegisterChannel(chan2);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "SayHello", WellKnownObjectMode.Singleton);
System.Console.WriteLine("点击 <enter> 退出...");
System.Console.ReadLine();
return 0;
}
}
}
CS\client.cs

using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples {
public class Client {

public bool init = false;
public static Thread thread1 = null;
public static Thread thread2 = null;

public static int Main(string [] args)
{
Client c = new Client();
thread1 = new Thread(new ThreadStart(c.RunMe));
thread2 = new Thread(new ThreadStart(c.RunMe));
thread1.Start();
thread2.Start();
return 0;
}


public void RunMe()
{
if (Thread.CurrentThread == thread1) {
HttpChannel chan = new HttpChannel();
ChannelServices.RegisterChannel(chan);
Console.WriteLine("我是线程 1");
HelloServer obj = (HelloServer)Activator.GetObject(typeof(HelloServer), "http://localhost:8086/SayHello");
for (int i = 0; i < 1000; i++) {
Console.WriteLine(obj.CountMe() + " - 来自线程 1 ");
Thread.Sleep(0);
}
}
else if (Thread.CurrentThread == thread2) {
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
Console.WriteLine("我是线程 2");
HelloServer obj = (HelloServer)Activator.GetObject(typeof(HelloServer), "tcp://localhost:8085/SayHello");
for (int i = 0; i < 1000; i++) {
Console.WriteLine(obj.CountMe() + " - 来自线程 2 ");
Thread.Sleep(0);
}
}
}
}
}

wolftop 2004-03-30
  • 打赏
  • 举报
回复
以下我们将举一个使用channel的例子。在这个例子中,我们将可以看到使用HTTP channel把两个应用
连接在一起是如此的简单。以下的服务器应用提供了一个服务,可将一个字符串的字母顺序反转。

  Server.cs using System;
  using System.IO;
  using System.Runtime.Remoting;
  using System.Runtime.Remoting.Channels.HTTP;

  namespace RemotingSample
  {
   public class Reverser : MarshalByRefObject
   {
    public string Reverse(string text)
    {
     Console.WriteLine("Reverse({0})", text);

     string rev = "";

     for (int i=text.Length-1; i>=0; i--)
     {
      rev += text[i];
      }

     Console.WriteLine("returning : {0}", rev);

     return rev;
    }
   }

   public class TheApp
   {
    public static void Main()
    {
     file:// Create a new HTTP channel that
     // listens on port 8000
     HTTPChannel channel = new HTTPChannel(8000);

     // Register the channel with the runtime
     ChannelServices.RegisterChannel(channel);

     // Expose the Reverser object from this server
     RemotingServices.RegisterWellKnownType(
         "server", // assembly name
         "RemotingSample.Reverser", // full type name
         "Reverser.soap", file:// URI
         WellKnownObjectMode.Singleton // instancing mode
      );

     // keep the server running until
     // the user presses enter
     Console.WriteLine("Server.exe");
     Console.WriteLine("Press enter to stop server...");
     Console.ReadLine();
    }
   }
  }

  现在我们已经拥有了一个字符反向服务,以下我们将建立一个客户应用来使用这个服务:

   Client.cs using System;
   using System.Runtime.Remoting;
   using System.Runtime.Remoting.Channels.HTTP;
   using RemotingSample; // reference the server

   public class TheApp
    {
     public static void Main()
     {
      // Create and register a channel
      // to comunicate to the server.
      // The client will use port 8001
      // to listen for callbacks
      HTTPChannel channel = new HTTPChannel(8001);
      ChannelServices.RegisterChannel(channel);

      // create an instance on the remote server
      // and call a method remotely
      Reverser rev = (Reverser)Activator.GetObject(
         typeof(Reverser), // type to create
         "http://localhost:8000/Reverser.soap" file:// URI
         );
      Console.WriteLine("Client.exe");
      Console.WriteLine(rev.Reverse("Hello, World!"));
     }
    }


       
************图一               图二*******************

看,通过远程.NET将两个应用连接在一起是多么的简单。当服务端和客户端程序放在两台不同的机器时,我们可以令两个程序都运行在80端口。这样远程的调用就可通过一个防火墙。你也可将HTTPChannel改为一个TCPChannel试一下。

  你要注意到,客户端是通过“Reverser.soap”来标识它想连接的对象的。这个名字与服务器代码中RegisterWellKnownType的URI参数符合。“.soap”的扩展是不必要的。URI可以是任何的字符串,只要它能唯一标识服务器的对象就可以了。“.soap”的扩展只是用来提醒我们HTTP channel是使用soap来格式化信息的。

  在上面有关channel的例子中,你可能会产生这样的疑问:参数是如何跨网络传送,返回值又是如何送回的呢?答案是,在参数被跨网络传送之前,他们必须经过串行化处理。对于需要传送的所有对象或者结构,都要经过这样的处理。串行化的处理很简单,只是以连续字节的方式建立变量或者对象中的数据的一个持续拷贝。将这些字节还原为一个值或者对象实例的处理被称为反串行化。

  那么参数是如何串行化的呢?远程.NET架构为我们提供了一个称为格式器(formatters)的对象集。格式器可将一个对象变成是一个特定的持续数据格式,也可以将该它还原回来。.NET为我们提供了两种格式器:

  System.Runtime.Serialization.Formatters.Binary
  System.Runtime.Serialization.Formatters.SOAP

binary(二进制)格式器是最简单的。它只是将数据直接转换为一个字节流。SOAP格式器使用一个XML来保持一个对象数据。要知道SOAP更详细的信息,可到http://www.soapwebservices.com。

以下我们举一个有关格式器的简单例子。我们将使用SOAP格式器,由于它使用的是XML,我们可以很容易地读出串行化的数据。

  Soap.cs using System;
  using System.IO;
  using System.Runtime.Serialization.Formatters.Soap;

  public class Person
  {
   public string FirstName = "David";
   public string LastName = "Findley";
   private int Age = 29;
  }

  public class TheApp
  {
   public static void Main()
   {
    Stream stream = File.Create("example.xml");
    SoapFormatter formatter = new SoapFormatter();
    Person p = new Person();

    // persist an integer
    formatter.Serialize(stream, 5);

    file:// persist a string
    formatter.Serialize(stream, "This is a string");

    // persist an object
    formatter.Serialize(stream, p);

    stream.Close();
   }
  }

  对于每个串行化的调用,example.xml的内容将有三个不同的部分:

  Example.xml

  <SOAP-ENV:Body>
  <xsd:int id="ref-1">
  <m_value>5</m_value>
  </xsd:int>
  </SOAP-ENV:Body>

  <SOAP-ENV:Body>
  <SOAP-ENC:string id="ref-1">This is a string</SOAP-ENC:string>
  </SOAP-ENV:Body>

  <SOAP-ENV:Body>
  <a1:Person id="ref-1">
  <FirstName id="ref-3">David</FirstName>
  <LastName id="ref-4">Findley</LastName>
  <Age>29</Age>
  </a1:Person>
  </SOAP-ENV:Body>

你可以看出,它可以串行化基本值类和对象。HTTPChannel使用SOAP格式器在客户和服务器之间传送数据。
wolftop 2004-03-30
  • 打赏
  • 举报
回复
微软的.NET Remoting提供了一种允许对象通过应用程序域与另一对象进行交互的框架。本文将通过两个具体的代码例子介绍它的特性,通过.NET Remoting,两个应用间的通信将变得非常简单,使用上也相当灵活。


  最近几年来,在计算机世界中,关于“向外扩展”(scale-out)与“向上扩展”(scale-up)的讨论不断,并且有向scale-out转化的趋势。在以前,当计算机跟不上应用对性能的要求时,人们将会购买一台更好更昂贵的机器,这就是scale-up,而scale-out则是通过在网络中加入更多的机器来解决这个问题,这样就无需更换整个系统。通过将计算的负担分配在多个系统中,整个系统就有了更高的可用性。

  如果将这个scale-out的意念更推进一步,我们可能会发现:为什么要将诸如信用卡验证和运送跟踪的处理放在我们的系统呢?如果能够调用售卖者的应用来直接得到这些服务,岂不可以做得更为简单吗?如果能做到这一点,我们就可以扩展自己程序的功能,而无需再加入额外的硬件。值得注意的是,这样做将会另ISV(独立软件开发商)的角色由一个软件提供者,转变为一个服务提供者。

  这个想法很好,但是目前的技术要实现它的话,存在着不少的问题。DCOM在跨防火墙工作时会有问题。DCOM是建立在私有协议上的,而CORBA存在有多种不同接口的问题。如果将DCOM和CORBA放在一起工作呢,就更麻烦了。

  为了实现这个scale-out的想法,我们需要一个组件技术,它可以跨越多种类型的网络和多种协议无缝地工作。远程.NET就可以做到这一点。

  这个无缝的交互是通过使用XML和SOAP来实现的。不过,它并没有认定SOAP进行组件交互的唯一方法,它也并没有认定HTTP或者TCP/IP是用来连接这些服务的网络协议。这样可令远程架构更加灵活,并且可适应协议和网络的变化。

  .NET架构是使用channel对象将应用连接在一起的。.NET架构提出了两种channel,它们是:

  System.Runtime.Remoting.Channels.TCP
  System.Runtime.Remoting.Channels.HTTP

  TCP channel和现有的DCOM非常类似,可提供很高的性能,在机器都处在一个内部网络时,可选用TCP channel,而HTTP channel使用HTTP协议,可让应用在Internet上交互。由于它使用的是HTTP协议,它可以很容易地做到负载均衡,并且能通过防火墙。

相关文章

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧