服务端对WCF数据举例说明

开发 后端
随着时代的进步科技的发展WCF数据版本也快速的提升与升级,下面我就对升级前和升级后WCF数据做一下自己的解析。希望能够对大家有帮助

到这里服务端的代码就全部列举出来了。当然本文中介绍的是元数据同步,而相应的SyncProvider实现是
通过MySimpleSyncProvider.cs文件提供的, #t#

当然为了不让我的某些错误理解影响大家阅读源码,这里直接将相应的英文注释也放在这里。 publicclassMySyncProvider:KnowledgeSyncProvider,IChangeDataRetriever,INotifyingChangeApplierTarget  

  1. {  
  2. //Thenameofthemetadatastorecustomcolumnthatisusedtosaveatimestampoflastchangeonan  
  3. //iteminthemetadatastoresowecandochangedetection.  
  4. conststringTIMESTAMP_COLUMNNAME="timestamp";  
  5. //Thisisoursampleinmemorydatastorewhichforsimplicty,storessetsofstringname-ProcessChangeBatchpairs  
  6. //referencedbyidentifiersofthetype'Guid'  
  7. MySimpleDataStore_store;  
  8. //UsetheSyncFramework'soptionalmetadatastoretotrackversioninformation  
  9. SqlMetadataStore_metadataStore=null;//表示通过使用轻型数据库存储元数据来实现的元数据存储区。  
  10. ReplicaMetadata_metadata=null;//提供对元数据存储区中的副本元数据和项元数据的访问。ReplicaMetadata还对删除检测和用于实现同步提供程序方法的帮助器提供服务  
  11. privatestring_name=null;  
  12. privatestring_folderPath=null;  
  13. privatestring_replicaMetadataFile=null;  
  14. privatestring_replicaIdFile=null;  
  15. //Theprovider'suniqueidentifier  
  16. SyncId_replicaId=null;  
  17. SyncIdFormatGroup_idFormats=null;  
  18. SyncSessionContext_currentSessionContext=null;  
  19. //Constructadatastorebyprovidinganamefortheendpoint(replica)and  
  20. //afiletowhichwe'llpersistthesyncmetadata(file)  
  21. publicMySyncProvider(stringfolderPath,stringname)  
  22. {  
  23. namename=name;  
  24. folderPathfolderPath=folderPath;  
  25. replicaMetadataFile=_folderPath.ToString()+""+_name.ToString()+".Metadata";  
  26. replicaIdFile=_folderPath.ToString()+""+_name.ToString()+".Replicaid";  
  27. SetItemIdFormatandReplicaIdFormatforusingGuidids.  
  28. idFormats=newSyncIdFormatGroup();  
  29. idFormats.ItemIdFormat.IsVariableLength=false;  
  30. idFormats.ItemIdFormat.Length=16;  
  31. idFormats.ReplicaIdFormat.IsVariableLength=false;  
  32. idFormats.ReplicaIdFormat.Length=16;  
  33. }  
  34. publicSyncIdReplicaId  
  35. {  
  36. get  
  37. {  
  38. if(_replicaId==null)  
  39. {  
  40. replicaId=GetReplicaIdFromFile(_replicaIdFile);  
  41. }  
  42. return_replicaId;  
  43. }  
  44. }  
  45. #regionMetadataStoreRelatedMethods  
  46. privatevoidInitializeMetadataStore()  
  47. {  
  48. Valuesforaddingacustomfieldtothemetadatastore  
  49. List<FieldSchema>fields=newList<FieldSchema>();  
  50. SyncIdid=ReplicaId;  
  51. Createoropenthemetadatastore,initializingitwiththeidformatswe'llusetoreferenceouritemsandendpoints  
  52. if(!File.Exists(_replicaMetadataFile))  
  53. {  
  54. fields.Add(newFieldSchema(TIMESTAMP_COLUMNNAME,typeof(System.UInt64)));  
  55. //创建一个具有指定名称和位置的元数据存储区文件,然后返回表示该文件的元数据存储区对象。  
  56. _metadataStore=SqlMetadataStore.CreateStore(_replicaMetadataFile);  
  57. //在元数据存储区创建和初始化副本的元数据,并返回一个用于访问该副本元数据的副本元数据对象。  
  58. _metadata=_metadataStore.InitializeReplicaMetadata(_idFormats,//提供程序的ID格式架构  
  59. replicaId,//与此元数据相关联的副本ID  
  60. fields,//每个元数据项的自定义元数据字段的架构信息集合。如果不存在自定义元数据字段,则可为null引用  
  61. null/*Noindexestocreate*/);//可用于更有效地查找元数据存储区中的项的索引架构列表。如果不存在自定义索引,则可以是null引用  
  62. }  
  63. else  
  64. {  
  65. _metadataStore=SqlMetadataStore.OpenStore(_replicaMetadataFile);//打开现有的元数据存储区文件,并返回表示该文件的元数据存储区对象  
  66. _metadata=_metadataStore.GetReplicaMetadata(_idFormats,_replicaId);//获取用于访问元数据存储区中的副本元数据的副本元数据对象。  
  67. }  
  68. }  
  69. privatevoidCloseMetadataStore()  
  70. {  
  71. metadataStore.Dispose();  
  72. metadataStore=null;  
  73. }  
  74. //Updatethemetadatastorewithchangesthathaveoccuredonthedatastoresincethelasttimeitwasupdated.  
  75. publicvoidUpdateMetadataStoreWithLocalChanges()  
  76. {  
  77. SyncVersionnewVersion=newSyncVersion(0,_metadata.GetNextTickCount());  
  78. metadata.DeleteDetector.MarkAllItemsUnreported();  
  79. foreach(Guididin_store.Ids)  
  80. {  
  81. ItemDatadata=_store.Get(id);  
  82. ItemMetadataitem=null;  
  83. //Lookupanitem'smetadatabyitsID  
  84. item=_metadata.FindItemMetadataById(newSyncId(id));  
  85. if(null==item)  
  86. {  
  87. Newitem,musthavebeencreatedsincethatlasttimethemetadatawasupdated.  
  88. Createtheitemmetadatarequiredforsync(givingitaSyncIDandaversion,definedtobeaDWORDandaULONGLONG  
  89. Forcreates,simplyprovidetherelativereplicaID(0)andthetickcountfortheprovider(everincreasing)  
  90. item=_metadata.CreateItemMetadata(newSyncId(id),newVersion);  
  91. item.ChangeVersion=newVersion;  
  92. SaveItemMetadata(item,data.TimeStamp);  
  93. }  
  94. else  
  95. {  
  96. if(data.TimeStamp>item.GetUInt64Field(TIMESTAMP_COLUMNNAME))//theitemhaschangedsincethelastsyncoperation.  
  97. {  
  98. //ChangedItem,thisitemhaschangedsincethelasttimethemetadatawasupdated.  
  99. //Assignanewversionbysimplystating"who"modifiedthisitem(0=local/me)and"when"(tickcountforthestore)  
  100. item.ChangeVersion=newVersion;  
  101. SaveItemMetadata(item,data.TimeStamp);  
  102. }  
  103. else  
  104. {  
  105. //Unchangeditem,nothinghaschangessojustmarkitaslivesothatthemetadataknowsithasnotbeendeleted.  
  106. _metadata.DeleteDetector.ReportLiveItemById(newSyncId(id));  
  107. }  
  108. }  
  109. }  
  110. Nowgobackthroughtheitemsthatarenolongerinthestoreandmarkthemasdeletedinthemetadata.   
  111. Thissetstheitemasatombstone.  
  112. foreach(ItemMetadataitemin_metadata.DeleteDetector.FindUnreportedItems())  
  113. {  
  114. item.MarkAsDeleted(newVersion);  
  115. SaveItemMetadata(item,0);//settimestampto0fortombstones  
  116. }  
  117. }  
  118. privatevoidSaveItemMetadata(ItemMetadataitem,ulongtimeStamp)  
  119. {  
  120. item.SetCustomField(TIMESTAMP_COLUMNNAME,timeStamp);  
  121. SaveItemMetadata(item);  
  122. }  
  123. privatevoidSaveItemMetadata(ItemMetadataitem)  
  124. {  
  125. metadata.SaveItemMetadata(item);  
  126. }  
  127. //MethodforcleaninguptombstonesolderthanacertainTimeSpan  
  128. publicvoidCleanupTombstones(TimeSpantimespan)  
  129. {  
  130. InitializeMetadataStore();  
  131. metadataStore.BeginTransaction();//对元数据存储区启动显式事务  
  132. metadata.CleanupDeletedItems(timespan);  
  133. metadataStore.CommitTransaction();//提交已对元数据存储区启动的显式事务  
  134. CloseMetadataStore();  
  135. }  
  136. #endregionMetadataStoreRelatedMethods  
  137. #regionKnowledgeSyncProviderOverrides  
  138. //BeginSessioniscalledatthebeginningofeachsyncoperation. Doinitializationhere. Forexampleupdate  
  139. //metadataifitwasnotupdatedastheactualdatawaschanged.  
  140. publicoverridevoidBeginSession(SyncProviderPositionposition,SyncSessionContextsyncSessionContext)  
  141. {  
  142. BeginSession();  
  143. currentSessionContext=syncSessionContext;  
  144. }  
  145. //EndSessioniscalledafterthesyncoperationiscompleted. Cleanuphappenshere.  
  146. publicoverridevoidEndSession(SyncSessionContextsyncSessionContext)  
  147. {  
  148. EndSession();  
  149. }  
  150. //Simplyaskthemetadatastoretocomputemychangebatchforme,providingthebatchsizeandtheknowledgeoftheotherendpoint!  
  151. //Theengineisaskingforthelistofchangesthatthedestinationproviderdoesnotknowabout.  
  152. //SyncKnowledge:表示副本所具有的有关自己项存储区的知识。  
  153. //此类型的所有公共静态(在VisualBasic中共享)成员都是线程安全的。不保证任何实例成员的线程安全。  
  154. publicoverrideChangeBatchGetChangeBatch(uintbatchSize,SyncKnowledgedestinationKnowledge,outobjectchangeDataRetriever)  
  155. {  
  156. ChangeBatchbatch=_metadata.GetChangeBatch(batchSize,destinationKnowledge);  
  157. changeDataRetriever=this;//thisiswherethetransfermechanism/protocolwouldgo.Foraninmemoryprovider,thisissufficient  
  158. returnbatch;  
  159. }  
  160. //ThisisonlycalledwhentheenginehasdetectedthatthedestinationisoutofdateduetoTombstonecleanup.  
  161. publicoverrideFullEnumerationChangeBatchGetFullEnumerationChangeBatch(uintbatchSize,SyncIdlowerEnumerationBound,SyncKnowledgeknowledgeForDataRetrieval,outobjectchangeDataRetriever)  
  162. {  
  163. FullEnumerationChangeBatchbatch=_metadata.GetFullEnumerationChangeBatch(batchSize,lowerEnumerationBound,knowledgeForDataRetrieval);  
  164. changeDataRetriever=this;//thisiswherethetransfermechanism/protocolwouldgo.Foraninmemoryprovider,thisissufficient  
  165. returnbatch;  
  166. }  
  167. //指定batchSize为10,以及相应的知识  
  168. publicoverridevoidGetSyncBatchParameters(outuintbatchSize,outSyncKnowledgeknowledge)  
  169. {  
  170. batchSize=10;  
  171. knowledge=_metadata.GetKnowledge();  
  172. }  
  173. //应用修改  
  174. publicoverridevoidProcessChangeBatch(ConflictResolutionPolicyresolutionPolicy,ChangeBatchsourceChanges,  
  175. objectchangeDataRetriever,SyncCallbackssyncCallback,SyncSessionStatisticssessionStatistics)  
  176. {  
  177. metadataStore.BeginTransaction();  
  178. 从原数据存储中获得取得本地所有修改  
  179. IEnumerable<ItemChange>localChanges=_metadata.GetLocalVersions(sourceChanges);  
  180. Createachangeapplierobjecttomakechangeapplicationeasier(maketheenginecallme  
  181. whenitneedsdataandwhenIshouldsavedata)  
  182. NotifyingChangeApplierchangeApplier=newNotifyingChangeApplier(_idFormats);  
  183. changeApplier.ApplyChanges(resolutionPolicy,sourceChanges,changeDataRetrieverasIChangeDataRetriever,localChanges,_metadata.GetKnowledge(),  
  184. _metadata.GetForgottenKnowledge(),this,_currentSessionContext,syncCallback);  
  185. metadataStore.CommitTransaction();  
  186. }  
  187. //Iffullenumerationisneededbecause thisproviderisoutofdateduetotombstonecleanup,thenthismethodwillbecalledbytheengine.  
  188. publicoverridevoidProcessFullEnumerationChangeBatch(ConflictResolutionPolicyresolutionPolicy,FullEnumerationChangeBatchsourceChanges,objectchangeDataRetriever,SyncCallbackssyncCallback,SyncSessionStatisticssessionStatistics)  
  189. {  
  190. metadataStore.BeginTransaction();  
  191. Getallmylocalchangeversionsfromthemetadatastore  
  192. IEnumerable<ItemChange>localChanges=_metadata.GetFullEnumerationLocalVersions(sourceChanges);  
  193. Createachangeapplierobjecttomakechangeapplicationeasier(maketheenginecallme  
  194. whenitneedsdataandwhenIshouldsavedata)  
  195. NotifyingChangeApplierchangeApplier=newNotifyingChangeApplier(_idFormats);  
  196. changeApplier.ApplyFullEnumerationChanges(resolutionPolicy,sourceChanges,changeDataRetrieverasIChangeDataRetriever,localChanges,_metadata.GetKnowledge(),  
  197. _metadata.GetForgottenKnowledge(),this,_currentSessionContext,syncCallback);  
  198. metadataStore.CommitTransaction();  
责任编辑:chenqingxiang 来源: 机械工业出版社
相关推荐

2013-11-26 11:08:23

Linux命令diff

2010-01-06 10:35:02

Json_Decode

2010-06-22 13:08:42

Linux At命令

2018-11-28 08:20:15

Linuxalias命令

2009-09-25 09:30:33

Hibernate持久

2009-12-07 18:26:36

WCF客户端

2010-01-18 17:31:54

C++编写程序

2010-06-18 10:24:51

Linux acces

2010-01-12 15:56:25

C++软件

2010-03-04 13:21:32

linux压缩命令

2010-01-21 09:53:23

C++操作符

2010-03-03 10:55:39

2010-02-24 15:42:03

WCF服务端安全

2009-12-08 18:08:43

WCF服务

2010-01-06 16:54:07

.Net Framew

2010-03-04 13:45:37

Linux压缩命令

2010-01-08 17:06:52

C++代码

2011-09-09 09:44:23

WCF

2010-04-09 16:52:36

Unix操作系统

2009-11-05 15:25:36

WCF服务端配置
点赞
收藏

51CTO技术栈公众号