自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Remoting序列化及租約

開(kāi)發(fā) 后端
今天就先談?wù)凴emoting序列化的問(wèn)題,然后會(huì)討論租約的問(wèn)題,租約包含三個(gè)方面:對(duì)象的生命周期、什么是租約、續(xù)約。希望本文對(duì)大家有所幫助。

今天就先談?wù)凴emoting序列化的問(wèn)題:首先,Remoting序列化是為了方便網(wǎng)絡(luò)傳輸,把遠(yuǎn)程對(duì)象轉(zhuǎn)化為網(wǎng)絡(luò)可傳輸?shù)男问?。Remoting序列化可以實(shí)現(xiàn)多種形式:序列化為xml、序列化為二進(jìn)制等等。當(dāng)然使用序列化需要添加一個(gè)標(biāo)記【Serializable】,而且可以用SerializableAttribute,或是實(shí)現(xiàn)ISerializable接口。下面放個(gè)序列化的代碼Demo:

   1:  [Serializable]
   2:      public class SumOf
   3:      {
   4:          public SumOf()
   5:          {
   6:          }
   7:  
   8:          public DecimalList Members = new DecimalList();
   9:  
  10:          public decimal Sum,Avg;
  11:  
  12:          public void Calculate()
  13:          {
  14:              this.Sum = 0;
  15:              foreach (decimal m in Members)
  16:              {
  17:                  Sum += m;
  18:              }
  19:              this.Avg = Sum / Members.Count;
  20:  
  21:          }
  22:  
  23:      }
  24:      [Serializable]
  25:      public class DecimalList : List<decimal>
  26:      {
  27:      }
     

接下來(lái),來(lái)說(shuō)說(shuō)租約的問(wèn)題,租約包含三個(gè)方面:對(duì)象的生命周期、什么是租約、續(xù)約。

1.對(duì)象的生命周期

我們先來(lái)認(rèn)識(shí)一下什么是對(duì)象,對(duì)象就是內(nèi)存里的數(shù)據(jù)結(jié)構(gòu)。先說(shuō)客戶機(jī)檢測(cè)服務(wù)器是否可用,那我們就要驗(yàn)證遠(yuǎn)程對(duì)象是否還存在。這個(gè)很簡(jiǎn)單,我們只要調(diào)用遠(yuǎn)程對(duì)象,如果遠(yuǎn)程對(duì)象不可用,會(huì)出現(xiàn)異常,那說(shuō)明服務(wù)器不可用。接著說(shuō)服務(wù)器檢測(cè)客戶機(jī)是否可用,使用垃圾回收機(jī)制驗(yàn)證,只對(duì)Singleton對(duì)象和客戶端激活對(duì)象有效。

2.什么是租約

租約是在服務(wù)器記錄的一個(gè)時(shí)間,如果超過(guò)這個(gè)時(shí)間,對(duì)象會(huì)被回收。

3、續(xù)約:為了讓對(duì)象繼續(xù)存在就必須使用續(xù)約

(1)隱式續(xù)約:每次調(diào)用遠(yuǎn)程對(duì)象上的方法的時(shí)候自動(dòng)進(jìn)行。

(2)顯示續(xù)約:使用ILease.Renew()方法實(shí)現(xiàn)。

(3)發(fā)起租約:租約管理者自動(dòng)續(xù)約。ILease.Register()

租約的配置:InitialLeaseTime 租約時(shí)間,默認(rèn)值 300 。RenewOnCallTime檢查時(shí)間 默認(rèn)值120 。SponsorshipTimeout過(guò)期時(shí)間 默認(rèn)值120。LeaseManagerPollTime租約管理者臨行時(shí)間 默認(rèn)值10

如何顯示租約信息:

   1:  ILease lease = (ILease)obj.GetLifetimeService();
   2:             
   3:              if (lease != null)
   4:              {
   5:                  Console.WriteLine("Lease Configuration:");
   6:                  Console.WriteLine("InitialLeaseTime: " +
   7:                      lease.InitialLeaseTime);
   8:                  Console.WriteLine("RenewOnCallTime: " +
   9:                      lease.RenewOnCallTime);
  10:                  Console.WriteLine("SponsorshipTimeout: " +
  11:                      lease.SponsorshipTimeout);
  12:                  Console.WriteLine(lease.CurrentLeaseTime);
  13:              }           

如何配置租約信息:

1代碼方式

   1:  public override Object InitializeLifetimeService()
   2:          {
   3:  
   4:              ILease lease = (ILease)base.InitializeLifetimeService();
   5:              // Normally, the initial lease time would be much longer.
   6:              // It is shortened here for demonstration purposes.
   7:              if (lease.CurrentState == LeaseState.Initial)
   8:              {
   9:                  lease.InitialLeaseTime = TimeSpan.FromSeconds(3);
  10:                  lease.SponsorshipTimeout = TimeSpan.FromSeconds(10);
  11:                  lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  12:              }
  13:              return lease;
  14:          }
2配置文件方式

   1:      <lifetime
   2:                 leaseTime="7M"
   3:                 sponsorshipTimeout="7M"
   4:                 renewOnCallTime="7M"
   5:                 />

本文來(lái)自零末博客園文章《Remoting學(xué)習(xí)歷程(2)

【編輯推薦】

  1. Remoting的一些性能測(cè)試情況
  2. NET框架下注意.NET Remoting安全性
  3. Microsoft .NET Remoting技術(shù)概述
  4. Remoting中數(shù)據(jù)序列化
  5. 利用Remoting實(shí)現(xiàn)文件傳輸
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2009-08-06 11:16:25

C#序列化和反序列化

2024-09-25 16:10:05

2009-09-09 15:47:27

XML序列化和反序列化

2022-08-06 08:41:18

序列化反序列化Hessian

2016-12-20 14:55:52

JavaScript鏈?zhǔn)?/a>結(jié)構(gòu)序列

2016-09-21 00:15:27

2024-05-06 00:00:00

C#序列化技術(shù)

2010-02-22 16:00:22

WCF序列化

2011-06-01 15:05:02

序列化反序列化

2009-08-24 17:14:08

C#序列化

2023-12-13 13:49:52

Python序列化模塊

2011-05-18 15:20:13

XML

2018-03-19 10:20:23

Java序列化反序列化

2010-01-05 16:31:45

.NET Framew

2013-03-11 13:55:03

JavaJSON

2009-08-25 14:24:36

C#序列化和反序列化

2011-06-01 14:50:48

2009-06-14 22:01:27

Java對(duì)象序列化反序列化

2019-11-20 10:07:23

web安全PHP序列化反序列化

2011-06-01 14:26:11

序列化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)