本篇內(nèi)容介紹了“服務(wù)端對(duì)WCF數(shù)據(jù)實(shí)例分析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)公司專注于開魯企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。開魯網(wǎng)站建設(shè)公司,為開魯?shù)鹊貐^(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
publicclassMySyncProvider:KnowledgeSyncProvider,IChangeDataRetriever,INotifyingChangeApplierTarget
{
//Thenameofthemetadatastorecustomcolumnthatisusedtosaveatimestampoflastchangeonan
//iteminthemetadatastoresowecandochangedetection.
conststringTIMESTAMP_COLUMNNAME="timestamp";
//Thisisoursampleinmemorydatastorewhichforsimplicty,storessetsofstringname-ProcessChangeBatchpairs
//referencedbyidentifiersofthetype'Guid'
MySimpleDataStore_store;
//UsetheSyncFramework'soptionalmetadatastoretotrackversioninformation
SqlMetadataStore_metadataStore=null;//表示通過使用輕型數(shù)據(jù)庫存儲(chǔ)元數(shù)據(jù)來實(shí)現(xiàn)的元數(shù)據(jù)存儲(chǔ)區(qū)。
ReplicaMetadata_metadata=null;//提供對(duì)元數(shù)據(jù)存儲(chǔ)區(qū)中的副本元數(shù)據(jù)和項(xiàng)元數(shù)據(jù)的訪問。ReplicaMetadata還對(duì)刪除檢測(cè)和用于實(shí)現(xiàn)同步提供程序方法的幫助器提供服務(wù)
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
List
SyncIdid=ReplicaId;
Createoropenthemetadatastore,initializingitwiththeidformatswe'llusetoreferenceouritemsandendpoints
if(!File.Exists(_replicaMetadataFile))
{
fields.Add(newFieldSchema(TIMESTAMP_COLUMNNAME,typeof(System.UInt64)));
//創(chuàng)建一個(gè)具有指定名稱和位置的元數(shù)據(jù)存儲(chǔ)區(qū)文件,然后返回表示該文件的元數(shù)據(jù)存儲(chǔ)區(qū)對(duì)象。
_metadataStore=SqlMetadataStore.CreateStore(_replicaMetadataFile);
//在元數(shù)據(jù)存儲(chǔ)區(qū)創(chuàng)建和初始化副本的元數(shù)據(jù),并返回一個(gè)用于訪問該副本元數(shù)據(jù)的副本元數(shù)據(jù)對(duì)象。
_metadata=_metadataStore.InitializeReplicaMetadata(_idFormats,//提供程序的ID格式架構(gòu)
replicaId,//與此元數(shù)據(jù)相關(guān)聯(lián)的副本ID
fields,//每個(gè)元數(shù)據(jù)項(xiàng)的自定義元數(shù)據(jù)字段的架構(gòu)信息集合。如果不存在自定義元數(shù)據(jù)字段,則可為null引用
null/*Noindexestocreate*/);//可用于更有效地查找元數(shù)據(jù)存儲(chǔ)區(qū)中的項(xiàng)的索引架構(gòu)列表。如果不存在自定義索引,則可以是null引用
}
else
{
_metadataStore=SqlMetadataStore.OpenStore(_replicaMetadataFile);//打開現(xiàn)有的元數(shù)據(jù)存儲(chǔ)區(qū)文件,并返回表示該文件的元數(shù)據(jù)存儲(chǔ)區(qū)對(duì)象
_metadata=_metadataStore.GetReplicaMetadata(_idFormats,_replicaId);//獲取用于訪問元數(shù)據(jù)存儲(chǔ)區(qū)中的副本元數(shù)據(jù)的副本元數(shù)據(jù)對(duì)象。
}
}
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();//對(duì)元數(shù)據(jù)存儲(chǔ)區(qū)啟動(dòng)顯式事務(wù)
metadata.CleanupDeletedItems(timespan);
metadataStore.CommitTransaction();//提交已對(duì)元數(shù)據(jù)存儲(chǔ)區(qū)啟動(dòng)的顯式事務(wù)
CloseMetadataStore();
}
#endregionMetadataStoreRelatedMethods
#regionKnowledgeSyncProviderOverrides
//BeginSessioniscalledatthebeginningofeachsyncoperation. Doinitializationhere. Forexampleupdate
//metadataifitwasnotupdatedastheactualdatawaschanged.
publicoverridevoidBeginSession(SyncProviderPositionposition,SyncSessionContextsyncSessionContext)
{
BeginSession();
currentSessionContext=syncSessionContext;
}
//EndSessioniscalledafterthesyncoperationiscompleted. Cleanuphappenshere.
publicoverridevoidEndSession(SyncSessionContextsyncSessionContext)
{
EndSession();
}
//Simplyaskthemetadatastoretocomputemychangebatchforme,providingthebatchsizeandtheknowledgeoftheotherendpoint!
//Theengineisaskingforthelistofchangesthatthedestinationproviderdoesnotknowabout.
//SyncKnowledge:表示副本所具有的有關(guān)自己項(xiàng)存儲(chǔ)區(qū)的知識(shí)。
//此類型的所有公共靜態(tài)(在VisualBasic中共享)成員都是線程安全的。不保證任何實(shí)例成員的線程安全。
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,以及相應(yīng)的知識(shí)
publicoverridevoidGetSyncBatchParameters(outuintbatchSize,outSyncKnowledgeknowledge)
{
batchSize=10;
knowledge=_metadata.GetKnowledge();
}
//應(yīng)用修改
publicoverridevoidProcessChangeBatch(ConflictResolutionPolicyresolutionPolicy,ChangeBatchsourceChanges,
objectchangeDataRetriever,SyncCallbackssyncCallback,SyncSessionStatisticssessionStatistics)
{
metadataStore.BeginTransaction();
從原數(shù)據(jù)存儲(chǔ)中獲得取得本地所有修改
IEnumerable
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
IEnumerable
Createachangeapplierobjecttomakechangeapplicationeasier(maketheenginecallme
whenitneedsdataandwhenIshouldsavedata)
NotifyingChangeApplierchangeApplier=newNotifyingChangeApplier(_idFormats);
changeApplier.ApplyFullEnumerationChanges(resolutionPolicy,sourceChanges,changeDataRetrieverasIChangeDataRetriever,localChanges,_metadata.GetKnowledge(),
_metadata.GetForgottenKnowledge(),this,_currentSessionContext,syncCallback);
metadataStore.CommitTransaction();
}
“服務(wù)端對(duì)WCF數(shù)據(jù)實(shí)例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!