人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

微軟的遠程處理框架.NET Remoting - 2

微軟的遠程處理框架.NET Remoting - 2

更新時間:2022-08-29 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

以下我們將舉一個使用channel的例子。在這個例子中,我們將可以看到使用HTTP channel把兩個應(yīng)用

連接在一起是如此的簡單。以下的服務(wù)器應(yīng)用提供了一個服務(wù),可將一個字符串的字母順序反轉(zhuǎn)。

  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();

    }

   }

  }

  現(xiàn)在我們已經(jīng)擁有了一個字符反向服務(wù),以下我們將建立一個客戶應(yīng)用來使用這個服務(wù):

   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將兩個應(yīng)用連接在一起是多么的簡單。當(dāng)服務(wù)端和客戶端程序放在兩臺不同的機器時,我們可以令兩個程序都運行在80端口。這樣遠程的調(diào)用就可通過一個防火墻。你也可將HTTPChannel改為一個TCPChannel試一下。

  你要注意到,客戶端是通過“Reverser.soap”來標(biāo)識它想連接的對象的。這個名字與服務(wù)器代碼中RegisterWellKnownType的URI參數(shù)符合。“.soap”的擴展是不必要的。URI可以是任何的字符串,只要它能唯一標(biāo)識服務(wù)器的對象就可以了。“.soap”的擴展只是用來提醒我們HTTP channel是使用soap來格式化信息的。

  在上面有關(guān)channel的例子中,你可能會產(chǎn)生這樣的疑問:參數(shù)是如何跨網(wǎng)絡(luò)傳送,返回值又是如何送回的呢?答案是,在參數(shù)被跨網(wǎng)絡(luò)傳送之前,他們必須經(jīng)過串行化處理。對于需要傳送的所有對象或者結(jié)構(gòu),都要經(jīng)過這樣的處理。串行化的處理很簡單,只是以連續(xù)字節(jié)的方式建立變量或者對象中的數(shù)據(jù)的一個持續(xù)拷貝。將這些字節(jié)還原為一個值或者對象實例的處理被稱為反串行化。

  那么參數(shù)是如何串行化的呢?遠程.NET架構(gòu)為我們提供了一個稱為格式器(formatters)的對象集。格式器可將一個對象變成是一個特定的持續(xù)數(shù)據(jù)格式,也可以將該它還原回來。.NET為我們提供了兩種格式器:

  System.Runtime.Serialization.Formatters.Binary

  System.Runtime.Serialization.Formatters.SOAP

binary(二進制)格式器是最簡單的。它只是將數(shù)據(jù)直接轉(zhuǎn)換為一個字節(jié)流。SOAP格式器使用一個XML來保持一個對象數(shù)據(jù)。要知道SOAP更詳細(xì)的信息,可到http://www.soapwebservices.com。

以下我們舉一個有關(guān)格式器的簡單例子。我們將使用SOAP格式器,由于它使用的是XML,我們可以很容易地讀出串行化的數(shù)據(jù)。

  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();

   }

  }

  對于每個串行化的調(diào)用,example.xml的內(nèi)容將有三個不同的部分:

  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格式器在客戶和服務(wù)器之間傳送數(shù)據(jù)。

  總的來說,格式器可以格式和保持值或者對象的數(shù)據(jù)。Channel傳送和接收數(shù)據(jù)。通過channel和格式器的協(xié)同工作,我們將可以使用任何的網(wǎng)絡(luò)和協(xié)議來連接兩個應(yīng)用。


溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 修文县| 万源市| 吴旗县| 富宁县| 融水| 墨江| 太仓市| 高州市| 西畴县| 荆门市| 江永县| 即墨市| 濮阳市| 公主岭市| 将乐县| 沛县| 香格里拉县| 涞源县| 宜兰县| 沙坪坝区| 余江县| 吕梁市| 浦北县| 报价| 舞钢市| 堆龙德庆县| 丰县| 铅山县| 且末县| 乌拉特前旗| 那坡县| 光山县| 田林县| 高青县| 鄂托克前旗| 黄梅县| 祁阳县| 陆川县| 涞水县| 宜君县| 南昌市|