重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“服务端对WCF数据实例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联公司专注于开鲁企业网站建设,成都响应式网站建设公司,商城网站建设。开鲁网站建设公司,为开鲁等地区提供建站服务。全流程按需求定制开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
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
List
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();
从原数据存储中获得取得本地所有修改
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();
}
“服务端对WCF数据实例分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!