国产毛片午夜福利,国产黄网,国产亚洲天堂,97国产精品

  •  
    微軟的遠(yuǎn)程處理框架.NET Remoting - 2
    發(fā)布時間:2005-03-17   瀏覽次數(shù):1185340
    以下我們將舉一個使用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!"));

         }

        }

    看,通過遠(yuǎn)程.NET將兩個應(yīng)用連接在一起是多么的簡單。當(dāng)服務(wù)端和客戶端程序放在兩臺不同的機器時,我們可以令兩個程序都運行在80端口。這樣遠(yuǎn)程的調(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ù)是如何串行化的呢?遠(yuǎn)程.NET架構(gòu)為我們提供了一個稱為格式器(formatters)的對象集。格式器可將一個對象變成是一個特定的持續(xù)數(shù)據(jù)格式,也可以將該它還原回來。.NET為我們提供了兩種格式器:

      System.Runtime.Serialization.Formatters.Binary

      System.Runtime.Serialization.Formatters.SOAP

    binary(二進(jìn)制)格式器是最簡單的。它只是將數(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

     。糞OAP-ENV:Body>

      <xsd:int id="ref-1">

     。糾_value>5</m_value>

      </xsd:int>

     。/SOAP-ENV:Body>

     。糞OAP-ENV:Body>

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

     。/SOAP-ENV:Body>

     。糞OAP-ENV:Body>

     。糰1:Person id="ref-1">

     。糉irstName id="ref-3">David</FirstName>

      <LastName id="ref-4">Findley</LastName>

     。糀ge>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)用。

    來源:http://edu.chinaz.com
    立即預(yù)約