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

成都创新互联于2013年成立,先为曲周等服务建站,曲周等地企业,进行企业商务咨询服务。为曲周企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
当然为了不让我的某些错误理解影响大家阅读源码,这里直接将相应的英文注释也放在这里。 publicclassMySyncProvider:KnowledgeSyncProvider,IChangeDataRetriever,INotifyingChangeApplierTarget
- {   
- //Thenameofthemetadatastorecustomcolumnthatisusedtosaveatimestampoflastchangeonan   
- //iteminthemetadatastoresowecandochangedetection.   
- conststringTIMESTAMP_COLUMNNAME="timestamp";   
- //Thisisoursampleinmemorydatastorewhichforsimplicty,storessetsofstringname-ProcessChangeBatchpairs   
- //referencedbyidentifiersofthetype'Guid'   
- MySimpleDataStore_store;   
- //UsetheSyncFramework'soptionalmetadatastoretotrackversioninformation   
- SqlMetadataStore_metadataStore=null;//表示通过使用轻型数据库存储元数据来实现的元数据存储区。   
- ReplicaMetadata_metadata=null;//提供对元数据存储区中的副本元数据和项元数据的访问。ReplicaMetadata还对删除检测和用于实现同步提供程序方法的帮助器提供服务   
- privatestring_name=null;   
- privatestring_folderPath=null;   
- privatestring_replicaMetadataFile=null;   
- privatestring_replicaIdFile=null;   
- //Theprovider'suniqueidentifier   
- SyncId_replicaId=null;   
- SyncIdFormatGroup_idFormats=null;   
- SyncSessionContext_currentSessionContext=null;   
- //Constructadatastorebyprovidinganamefortheendpoint(replica)and   
- //afiletowhichwe'llpersistthesyncmetadata(file)   
- publicMySyncProvider(stringfolderPath,stringname)   
- {   
- namename=name;   
- folderPathfolderPath=folderPath;   
- replicaMetadataFile=_folderPath.ToString()+""+_name.ToString()+".Metadata";   
- replicaIdFile=_folderPath.ToString()+""+_name.ToString()+".Replicaid";   
- SetItemIdFormatandReplicaIdFormatforusingGuidids.   
- idFormats=newSyncIdFormatGroup();   
- idFormats.ItemIdFormat.IsVariableLength=false;   
- idFormats.ItemIdFormat.Length=16;   
- idFormats.ReplicaIdFormat.IsVariableLength=false;   
- idFormats.ReplicaIdFormat.Length=16;   
- }   
- publicSyncIdReplicaId   
- {   
- get   
- {   
- if(_replicaId==null)   
- {   
- replicaId=GetReplicaIdFromFile(_replicaIdFile);   
- }   
- return_replicaId;   
- }   
- }   
- #regionMetadataStoreRelatedMethods   
- privatevoidInitializeMetadataStore()   
- {   
- Valuesforaddingacustomfieldtothemetadatastore   
- Listfields=newList();   
- SyncIdid=ReplicaId;   
- Createoropenthemetadatastore,initializingitwiththeidformatswe'llusetoreferenceouritemsandendpoints   
- if(!File.Exists(_replicaMetadataFile))   
- {   
- fields.Add(newFieldSchema(TIMESTAMP_COLUMNNAME,typeof(System.UInt64)));   
- //创建一个具有指定名称和位置的元数据存储区文件,然后返回表示该文件的元数据存储区对象。   
- _metadataStore=SqlMetadataStore.CreateStore(_replicaMetadataFile);   
- //在元数据存储区创建和初始化副本的元数据,并返回一个用于访问该副本元数据的副本元数据对象。   
- _metadata=_metadataStore.InitializeReplicaMetadata(_idFormats,//提供程序的ID格式架构   
- replicaId,//与此元数据相关联的副本ID   
- fields,//每个元数据项的自定义元数据字段的架构信息集合。如果不存在自定义元数据字段,则可为null引用   
- null/*Noindexestocreate*/);//可用于更有效地查找元数据存储区中的项的索引架构列表。如果不存在自定义索引,则可以是null引用   
- }   
- else   
- {   
- _metadataStore=SqlMetadataStore.OpenStore(_replicaMetadataFile);//打开现有的元数据存储区文件,并返回表示该文件的元数据存储区对象   
- _metadata=_metadataStore.GetReplicaMetadata(_idFormats,_replicaId);//获取用于访问元数据存储区中的副本元数据的副本元数据对象。   
- }   
- }   
- privatevoidCloseMetadataStore()   
- {   
- metadataStore.Dispose();   
- metadataStore=null;   
- }   
- //Updatethemetadatastorewithchangesthathaveoccuredonthedatastoresincethelasttimeitwasupdated.   
- publicvoidUpdateMetadataStoreWithLocalChanges()   
- {   
- SyncVersionnewVersion=newSyncVersion(0,_metadata.GetNextTickCount());   
- metadata.DeleteDetector.MarkAllItemsUnreported();   
- foreach(Guididin_store.Ids)   
- {   
- ItemDatadata=_store.Get(id);   
- ItemMetadataitem=null;   
- //Lookupanitem'smetadatabyitsID   
- item=_metadata.FindItemMetadataById(newSyncId(id));   
- if(null==item)   
- {   
- Newitem,musthavebeencreatedsincethatlasttimethemetadatawasupdated.   
- Createtheitemmetadatarequiredforsync(givingitaSyncIDandaversion,definedtobeaDWORDandaULONGLONG   
- Forcreates,simplyprovidetherelativereplicaID(0)andthetickcountfortheprovider(everincreasing)   
- item=_metadata.CreateItemMetadata(newSyncId(id),newVersion);   
- item.ChangeVersion=newVersion;   
- SaveItemMetadata(item,data.TimeStamp);   
- }   
- else   
- {   
- if(data.TimeStamp>item.GetUInt64Field(TIMESTAMP_COLUMNNAME))//theitemhaschangedsincethelastsyncoperation.   
- {   
- //ChangedItem,thisitemhaschangedsincethelasttimethemetadatawasupdated.   
- //Assignanewversionbysimplystating"who"modifiedthisitem(0=local/me)and"when"(tickcountforthestore)   
- item.ChangeVersion=newVersion;   
- SaveItemMetadata(item,data.TimeStamp);   
- }   
- else   
- {   
- //Unchangeditem,nothinghaschangessojustmarkitaslivesothatthemetadataknowsithasnotbeendeleted.   
- _metadata.DeleteDetector.ReportLiveItemById(newSyncId(id));   
- }   
- }   
- }   
- Nowgobackthroughtheitemsthatarenolongerinthestoreandmarkthemasdeletedinthemetadata.    
- Thissetstheitemasatombstone.   
- foreach(ItemMetadataitemin_metadata.DeleteDetector.FindUnreportedItems())   
- {   
- item.MarkAsDeleted(newVersion);   
- SaveItemMetadata(item,0);//settimestampto0fortombstones   
- }   
- }   
- privatevoidSaveItemMetadata(ItemMetadataitem,ulongtimeStamp)   
- {   
- item.SetCustomField(TIMESTAMP_COLUMNNAME,timeStamp);   
- SaveItemMetadata(item);   
- }   
- privatevoidSaveItemMetadata(ItemMetadataitem)   
- {   
- metadata.SaveItemMetadata(item);   
- }   
- //MethodforcleaninguptombstonesolderthanacertainTimeSpan   
- publicvoidCleanupTombstones(TimeSpantimespan)   
- {   
- InitializeMetadataStore();   
- metadataStore.BeginTransaction();//对元数据存储区启动显式事务   
- metadata.CleanupDeletedItems(timespan);   
- metadataStore.CommitTransaction();//提交已对元数据存储区启动的显式事务   
- CloseMetadataStore();   
- }   
- #endregionMetadataStoreRelatedMethods   
- #regionKnowledgeSyncProviderOverrides   
- //BeginSessioniscalledatthebeginningofeachsyncoperation. Doinitializationhere. Forexampleupdate   
- //metadataifitwasnotupdatedastheactualdatawaschanged.   
- publicoverridevoidBeginSession(SyncProviderPositionposition,SyncSessionContextsyncSessionContext)   
- {   
- BeginSession();   
- currentSessionContext=syncSessionContext;   
- }   
- //EndSessioniscalledafterthesyncoperationiscompleted. Cleanuphappenshere.   
- publicoverridevoidEndSession(SyncSessionContextsyncSessionContext)   
- {   
- EndSession();   
- }   
- //Simplyaskthemetadatastoretocomputemychangebatchforme,providingthebatchsizeandtheknowledgeoftheotherendpoint!   
- //Theengineisaskingforthelistofchangesthatthedestinationproviderdoesnotknowabout.   
- //SyncKnowledge:表示副本所具有的有关自己项存储区的知识。   
- //此类型的所有公共静态(在VisualBasic中共享)成员都是线程安全的。不保证任何实例成员的线程安全。   
- publicoverrideChangeBatchGetChangeBatch(uintbatchSize,SyncKnowledgedestinationKnowledge,outobjectchangeDataRetriever)   
- {   
- ChangeBatchbatch=_metadata.GetChangeBatch(batchSize,destinationKnowledge);   
- changeDataRetriever=this;//thisiswherethetransfermechanism/protocolwouldgo.Foraninmemoryprovider,thisissufficient   
- returnbatch;   
- }   
- //ThisisonlycalledwhentheenginehasdetectedthatthedestinationisoutofdateduetoTombstonecleanup.   
- publicoverrideFullEnumerationChangeBatchGetFullEnumerationChangeBatch(uintbatchSize,SyncIdlowerEnumerationBound,SyncKnowledgeknowledgeForDataRetrieval,outobjectchangeDataRetriever)   
- {   
- FullEnumerationChangeBatchbatch=_metadata.GetFullEnumerationChangeBatch(batchSize,lowerEnumerationBound,knowledgeForDataRetrieval);   
- changeDataRetriever=this;//thisiswherethetransfermechanism/protocolwouldgo.Foraninmemoryprovider,thisissufficient   
- returnbatch;   
- }   
- //指定batchSize为10,以及相应的知识   
- publicoverridevoidGetSyncBatchParameters(outuintbatchSize,outSyncKnowledgeknowledge)   
- {   
- batchSize=10;   
- knowledge=_metadata.GetKnowledge();   
- }   
- //应用修改   
- publicoverridevoidProcessChangeBatch(ConflictResolutionPolicyresolutionPolicy,ChangeBatchsourceChanges,   
- objectchangeDataRetriever,SyncCallbackssyncCallback,SyncSessionStatisticssessionStatistics)   
- {   
- metadataStore.BeginTransaction();   
- 从原数据存储中获得取得本地所有修改   
- IEnumerablelocalChanges=_metadata.GetLocalVersions(sourceChanges);   
- Createachangeapplierobjecttomakechangeapplicationeasier(maketheenginecallme   
- whenitneedsdataandwhenIshouldsavedata)   
- NotifyingChangeApplierchangeApplier=newNotifyingChangeApplier(_idFormats);   
- changeApplier.ApplyChanges(resolutionPolicy,sourceChanges,changeDataRetrieverasIChangeDataRetriever,localChanges,_metadata.GetKnowledge(),   
- _metadata.GetForgottenKnowledge(),this,_currentSessionContext,syncCallback);   
- metadataStore.CommitTransaction();   
- }   
- //Iffullenumerationisneededbecause thisproviderisoutofdateduetotombstonecleanup,thenthismethodwillbecalledbytheengine.   
- publicoverridevoidProcessFullEnumerationChangeBatch(ConflictResolutionPolicyresolutionPolicy,FullEnumerationChangeBatchsourceChanges,objectchangeDataRetriever,SyncCallbackssyncCallback,SyncSessionStatisticssessionStatistics)   
- {   
- metadataStore.BeginTransaction();   
- Getallmylocalchangeversionsfromthemetadatastore   
- IEnumerablelocalChanges=_metadata.GetFullEnumerationLocalVersions(sourceChanges);   
- Createachangeapplierobjecttomakechangeapplicationeasier(maketheenginecallme   
- whenitneedsdataandwhenIshouldsavedata)   
- NotifyingChangeApplierchangeApplier=newNotifyingChangeApplier(_idFormats);   
- changeApplier.ApplyFullEnumerationChanges(resolutionPolicy,sourceChanges,changeDataRetrieverasIChangeDataRetriever,localChanges,_metadata.GetKnowledge(),   
- _metadata.GetForgottenKnowledge(),this,_currentSessionContext,syncCallback);   
- metadataStore.CommitTransaction();   
- }
            分享标题:服务端对WCF数据举例说明            
            网站路径:
http://zsjierui.cn/article/dpepjig.html