Share via


SqlMetadataStore.GetReplicaMetadata Method

Gets a replica metadata object that is used to access replica metadata in the metadata store.

Namespace: Microsoft.Synchronization.MetadataStorage
Assembly: Microsoft.Synchronization.MetadataStorage (in microsoft.synchronization.metadatastorage.dll)

Syntax

'Declaration
Public Overrides Function GetReplicaMetadata ( _
    idFormats As SyncIdFormatGroup, _
    replicaId As SyncId _
) As ReplicaMetadata
'Usage
Dim instance As SqlMetadataStore
Dim idFormats As SyncIdFormatGroup
Dim replicaId As SyncId
Dim returnValue As ReplicaMetadata

returnValue = instance.GetReplicaMetadata(idFormats, replicaId)
public override ReplicaMetadata GetReplicaMetadata (
    SyncIdFormatGroup idFormats,
    SyncId replicaId
)
public:
virtual ReplicaMetadata^ GetReplicaMetadata (
    SyncIdFormatGroup^ idFormats, 
    SyncId^ replicaId
) override
public ReplicaMetadata GetReplicaMetadata (
    SyncIdFormatGroup idFormats, 
    SyncId replicaId
)
public override function GetReplicaMetadata (
    idFormats : SyncIdFormatGroup, 
    replicaId : SyncId
) : ReplicaMetadata

Parameters

  • idFormats
    The ID format schema for the provider.
  • replicaId
    The ID of the replica that is associated with this metadata.

Return Value

A replica metadata object that is used to access replica metadata in the metadata store.

Exceptions

Exception type Condition

ObjectDisposedException

This object has been disposed or was not initialized correctly.

ArgumentNullException

idFormats or replicaId is a null reference (Nothing in Visual Basic).

SyncIdFormatMismatchException

The format of replicaId does not match the format specified in idFormats, or idFormats does not match the ID format schema that was used when the replica metadata was initialized.

ArgumentOutOfRangeException

The length of an ID specified by idFormats is more than 8000 bytes.

InvalidOperationException

A metadata store has not been opened or created.

ReplicaMetadataInUseException

An instance of this replica metadata object is already active.

ReplicaMetadataNotFoundException

Replica metadata that has ID replicaId was not found.

Remarks

This method is used to access replica metadata that already exists in the metadata store. To create new replica metadata in a metadata store, use InitializeReplicaMetadata.

This method returns the implementation of the ReplicaMetadata abstract class that is provided by the metadata storage service. This abstract class can be used to access replica metadata that is stored in the Sync Framework database file.

To prevent applications from making concurrent conflicting updates to the metadata store, multiple outstanding instances of ReplicaMetadata for a particular replica ID are not allowed. Applications can access the same ReplicaMetadata object from multiple threads, but multiple processes cannot access the replica metadata at the same time. If an outstanding instance of ReplicaMetadata for a particular replica ID already exists, this method throws ReplicaMetadataInUseException.

Example

The following example gets replica metadata in a SqlMetadataStore object when replica metadata already exists. The example then enumerates the item store and finds metadata for each item.

public void NewStore(string StoreName, MetadataStore metaStore, bool metaStoreIsNew)
{
    // Close the current store. This is necessary to release the ReplicaMetadata object held by this object.
    Close(true);

    // Keep the store name for later use.
    _StoreName = StoreName;

    // The absolute path of the item store is used as the replica ID.
    string StoreAbsPath = Path.GetFullPath(StoreName);

    // Get or initialize replica metadata in the metadata store.
    _ContactMetadataStore = metaStore;
    if (!metaStoreIsNew)
    {
        // The metadata store exists, so open it and get the replica metadata for the current replica.
        // The replica ID is the absolute path of the item store.
        _ContactReplicaMetadata = _ContactMetadataStore.GetReplicaMetadata(ContactIdFormatGroup,
            new SyncId(StoreAbsPath));

        // Read the contacts from the item store and the metadata store and save them in two
        // in-memory lists. These lists are modified in memory by the methods in this object 
        // and committed to the disk when SaveChanges is called.
        StreamReader contactReader;
        contactReader = File.OpenText(StoreName);

        Contact contact = ReadNextContact(contactReader);
        while (null != contact)
        {
            ItemMetadata itemMeta = FindMetadata(contact);

            _ContactList.Add(itemMeta.GlobalId, contact);
            _ContactItemMetaList.Add(itemMeta.GlobalId, itemMeta);

            contact = ReadNextContact(contactReader);
        }

        contactReader.Close();
    }
    else
    {
        // The metadata store does not exist, so create a new one.

        // Create custom fields for First Name, Last Name, and Phone Number. These will be used
        // as unique index fields for identifying items between the metadata store and the item store.
        FieldSchema[] CustomFields = 
        {
            new FieldSchema(FirstNameField, typeof(string), 100),
            new FieldSchema(LastNameField, typeof(string), 100),
            new FieldSchema(PhoneNumberField, typeof(string), 20)
        };

        // Specify the custom fields as a unique index.
        string[] IndexFields = { FirstNameField, LastNameField, PhoneNumberField };
        IndexSchema[] Indexes = 
        {
            new IndexSchema(IndexFields, true)
        };

        // Create the metadata for the replica in the metadata store.
        _ContactReplicaMetadata = _ContactMetadataStore.InitializeReplicaMetadata(
            ContactIdFormatGroup, new SyncId(StoreAbsPath), CustomFields, Indexes);

        // Set the provider version
        _ContactReplicaMetadata.ProviderVersion = (uint)ContactsProviderVersion.ContactsProvider_v1;
    }
}

See Also

Reference

SqlMetadataStore Class
SqlMetadataStore Members
Microsoft.Synchronization.MetadataStorage Namespace