到这里服务端的代码就全部列举出来了。当然本文中介绍的是元数据同步,而相应的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://www.shufengxianlan.com/qtweb/news42/114842.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
广告
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源:
创新互联